blob: 8329687eadaf43252e2f42bdd30720eb04108c49 [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.
15 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossum79f25d91997-04-29 20:08:16 +000017#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021#include "ast.h"
22#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000024#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000026
Guido van Rossum8e793d91997-03-03 19:13:14 +000027int Py_OptimizeFlag = 0;
28
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029/*
30 ISSUES:
Guido van Rossum8861b741996-07-30 16:49:37 +000031
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032 character encodings aren't handled
Jeremy Hyltone36f7782001-01-19 03:21:30 +000033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034 ref leaks in interpreter when press return on empty line
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036 opcode_stack_effect() function should be reviewed since stack depth bugs
37 could be really hard to find later.
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000038
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039 Dead code is being generated (i.e. after unconditional jumps).
40*/
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042#define DEFAULT_BLOCK_SIZE 16
43#define DEFAULT_BLOCKS 8
44#define DEFAULT_CODE_SIZE 128
45#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047struct instr {
48 int i_jabs : 1;
49 int i_jrel : 1;
50 int i_hasarg : 1;
51 unsigned char i_opcode;
52 int i_oparg;
53 struct basicblock_ *i_target; /* target block (if jump instruction) */
54 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055};
56
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057typedef struct basicblock_ {
58 /* next block in the list of blocks for a unit (don't confuse with
59 * b_next) */
60 struct basicblock_ *b_list;
61 /* number of instructions used */
62 int b_iused;
63 /* length of instruction array (b_instr) */
64 int b_ialloc;
65 /* pointer to an array of instructions, initially NULL */
66 struct instr *b_instr;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_ *b_next;
70 /* b_seen is used to perform a DFS of basicblocks. */
71 int b_seen : 1;
72 /* b_return is true if a RETURN_VALUE opcode is inserted. */
73 int b_return : 1;
74 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
77 int b_offset;
78} basicblock;
79
80/* fblockinfo tracks the current frame block.
81
82 A frame block is used to handle loops, try/except, and try/finally.
83 It's called a frame block to distinguish it from a basic block in the
84 compiler IR.
85*/
86
87enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
88
89struct fblockinfo {
90 enum fblocktype fb_type;
91 basicblock *fb_block;
92};
93
94/* The following items change on entry and exit of code blocks.
95 They must be saved and restored when returning to a block.
96*/
97struct compiler_unit {
98 PySTEntryObject *u_ste;
99
100 PyObject *u_name;
101 /* The following fields are dicts that map objects to
102 the index of them in co_XXX. The index is used as
103 the argument for opcodes that refer to those collections.
104 */
105 PyObject *u_consts; /* all constants */
106 PyObject *u_names; /* all names */
107 PyObject *u_varnames; /* local variables */
108 PyObject *u_cellvars; /* cell variables */
109 PyObject *u_freevars; /* free variables */
110
111 PyObject *u_private; /* for private name mangling */
112
113 int u_argcount; /* number of arguments for block */
114 basicblock *u_blocks; /* pointer to list of blocks */
115 basicblock *u_curblock; /* pointer to current block */
116 int u_tmpname; /* temporary variables for list comps */
117
118 int u_nfblocks;
119 struct fblockinfo u_fblock[CO_MAXBLOCKS];
120
121 int u_firstlineno; /* the first lineno of the block */
122 int u_lineno; /* the lineno for the current stmt */
123 bool u_lineno_set; /* boolean to indicate whether instr
124 has been generated with current lineno */
125};
126
127/* This struct captures the global state of a compilation.
128
129 The u pointer points to the current compilation unit, while units
130 for enclosing blocks are stored in c_stack. The u and c_stack are
131 managed by compiler_enter_scope() and compiler_exit_scope().
132*/
133
134struct compiler {
135 const char *c_filename;
136 struct symtable *c_st;
137 PyFutureFeatures *c_future; /* pointer to module's __future__ */
138 PyCompilerFlags *c_flags;
139
140 int c_interactive;
141 int c_nestlevel;
142
143 struct compiler_unit *u; /* compiler state for current block */
144 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
145 char *c_encoding; /* source encoding (a borrowed reference) */
146};
147
148struct assembler {
149 PyObject *a_bytecode; /* string containing bytecode */
150 int a_offset; /* offset into bytecode */
151 int a_nblocks; /* number of reachable blocks */
152 basicblock **a_postorder; /* list of blocks in dfs postorder */
153 PyObject *a_lnotab; /* string containing lnotab */
154 int a_lnotab_off; /* offset into lnotab */
155 int a_lineno; /* last lineno of emitted instruction */
156 int a_lineno_off; /* bytecode offset of last lineno */
157};
158
159static int compiler_enter_scope(struct compiler *, identifier, void *, int);
160static void compiler_free(struct compiler *);
161static basicblock *compiler_new_block(struct compiler *);
162static int compiler_next_instr(struct compiler *, basicblock *);
163static int compiler_addop(struct compiler *, int);
164static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
165static int compiler_addop_i(struct compiler *, int, int);
166static int compiler_addop_j(struct compiler *, int, basicblock *, int);
167static void compiler_use_block(struct compiler *, basicblock *);
168static basicblock *compiler_use_new_block(struct compiler *);
169static int compiler_error(struct compiler *, const char *);
170static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
171
172static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
173static int compiler_visit_stmt(struct compiler *, stmt_ty);
174static int compiler_visit_keyword(struct compiler *, keyword_ty);
175static int compiler_visit_expr(struct compiler *, expr_ty);
176static int compiler_augassign(struct compiler *, stmt_ty);
177static int compiler_visit_slice(struct compiler *, slice_ty,
178 expr_context_ty);
179
180static int compiler_push_fblock(struct compiler *, enum fblocktype,
181 basicblock *);
182static void compiler_pop_fblock(struct compiler *, enum fblocktype,
183 basicblock *);
184
185static int inplace_binop(struct compiler *, operator_ty);
186static int expr_constant(expr_ty e);
187
188static PyCodeObject *assemble(struct compiler *, int addNone);
189static PyObject *__doc__;
190
191PyObject *
192_Py_Mangle(PyObject *private, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000193{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 /* Name mangling: __private becomes _classname__private.
195 This is independent from how the name is used. */
196 const char *p, *name = PyString_AsString(ident);
197 char *buffer;
198 size_t nlen, plen;
199 if (private == NULL || name == NULL || name[0] != '_' || name[1] != '_') {
200 Py_INCREF(ident);
201 return ident;
202 }
203 p = PyString_AsString(private);
204 nlen = strlen(name);
205 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
206 Py_INCREF(ident);
207 return ident; /* Don't mangle __whatever__ */
208 }
209 /* Strip leading underscores from class name */
210 while (*p == '_')
211 p++;
212 if (*p == '\0') {
213 Py_INCREF(ident);
214 return ident; /* Don't mangle if class is just underscores */
215 }
216 plen = strlen(p);
217 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
218 if (!ident)
219 return 0;
220 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
221 buffer = PyString_AS_STRING(ident);
222 buffer[0] = '_';
223 strncpy(buffer+1, p, plen);
224 strcpy(buffer+1+plen, name);
225 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000226}
227
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228static int
229compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000230{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 c->c_stack = PyList_New(0);
234 if (!c->c_stack)
235 return 0;
236
237 return 1;
238}
239
240PyCodeObject *
241PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags)
242{
243 struct compiler c;
244 PyCodeObject *co = NULL;
245 PyCompilerFlags local_flags;
246 int merged;
247
248 if (!__doc__) {
249 __doc__ = PyString_InternFromString("__doc__");
250 if (!__doc__)
251 goto error;
252 }
253
254 if (!compiler_init(&c))
255 goto error;
256 c.c_filename = filename;
257 c.c_future = PyFuture_FromAST(mod, filename);
258 if (c.c_future == NULL)
259 goto error;
260 if (!flags) {
261 local_flags.cf_flags = 0;
262 flags = &local_flags;
263 }
264 merged = c.c_future->ff_features | flags->cf_flags;
265 c.c_future->ff_features = merged;
266 flags->cf_flags = merged;
267 c.c_flags = flags;
268 c.c_nestlevel = 0;
269
270 c.c_st = PySymtable_Build(mod, filename, c.c_future);
271 if (c.c_st == NULL) {
272 if (!PyErr_Occurred())
273 PyErr_SetString(PyExc_SystemError, "no symtable");
274 goto error;
275 }
276
277 /* XXX initialize to NULL for now, need to handle */
278 c.c_encoding = NULL;
279
280 co = compiler_mod(&c, mod);
281
282 error:
283 compiler_free(&c);
284 return co;
285}
286
287PyCodeObject *
288PyNode_Compile(struct _node *n, const char *filename)
289{
290 PyCodeObject *co;
291 mod_ty mod = PyAST_FromNode(n, NULL, filename);
292 if (!mod)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000293 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294 co = PyAST_Compile(mod, filename, NULL);
295 free_mod(mod);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000296 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000297}
298
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000299static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000301{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302 if (c->c_st)
303 PySymtable_Free(c->c_st);
304 if (c->c_future)
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000305 PyMem_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000307}
308
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000311{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312 int i, n;
313 PyObject *v, *k, *dict = PyDict_New();
Guido van Rossumd076c731998-10-07 19:42:25 +0000314
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315 n = PyList_Size(list);
316 for (i = 0; i < n; i++) {
317 v = PyInt_FromLong(i);
318 if (!v) {
319 Py_DECREF(dict);
320 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000321 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 k = PyList_GET_ITEM(list, i);
323 k = Py_BuildValue("(OO)", k, k->ob_type);
324 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
325 Py_XDECREF(k);
326 Py_DECREF(v);
327 Py_DECREF(dict);
328 return NULL;
329 }
330 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000331 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 return dict;
333}
334
335/* Return new dict containing names from src that match scope(s).
336
337 src is a symbol table dictionary. If the scope of a name matches
338 either scope_type or flag is set, insert it into the new dict. The
339 values are integers, starting at offset and increasing by one for
340 each key.
341*/
342
343static PyObject *
344dictbytype(PyObject *src, int scope_type, int flag, int offset)
345{
346 int pos = 0, i = offset, scope;
347 PyObject *k, *v, *dest = PyDict_New();
348
349 assert(offset >= 0);
350 if (dest == NULL)
351 return NULL;
352
353 while (PyDict_Next(src, &pos, &k, &v)) {
354 /* XXX this should probably be a macro in symtable.h */
355 assert(PyInt_Check(v));
356 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
357
358 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
359 PyObject *tuple, *item = PyInt_FromLong(i);
360 if (item == NULL) {
361 Py_DECREF(dest);
362 return NULL;
363 }
364 i++;
365 tuple = Py_BuildValue("(OO)", k, k->ob_type);
366 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
367 Py_DECREF(item);
368 Py_DECREF(dest);
369 Py_XDECREF(tuple);
370 return NULL;
371 }
372 Py_DECREF(item);
373 Py_DECREF(tuple);
374 }
375 }
376 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000377}
378
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000379/* Begin: Peephole optimizations ----------------------------------------- */
380
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000381#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
382#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000383#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
384#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000385#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000386#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
387#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
388
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000389/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
390 with LOAD_CONST (c1, c2, ... cn).
391 The consts table must still be in list form so that the
392 new constant (c1, c2, ... cn) can be appended.
393 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000394 Bails out with no change if one or more of the LOAD_CONSTs is missing.
395 Also works for BUILD_LIST when followed by an "in" or "not in" test.
396*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000397static int
398tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
399{
400 PyObject *newconst, *constant;
401 int i, arg, len_consts;
402
403 /* Pre-conditions */
404 assert(PyList_CheckExact(consts));
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000405 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000406 assert(GETARG(codestr, (n*3)) == n);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000407 for (i=0 ; i<n ; i++)
Raymond Hettingereffb3932004-10-30 08:55:08 +0000408 assert(codestr[i*3] == LOAD_CONST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000409
410 /* Buildup new tuple of constants */
411 newconst = PyTuple_New(n);
412 if (newconst == NULL)
413 return 0;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000414 len_consts = PyList_GET_SIZE(consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000415 for (i=0 ; i<n ; i++) {
416 arg = GETARG(codestr, (i*3));
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000417 assert(arg < len_consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000418 constant = PyList_GET_ITEM(consts, arg);
419 Py_INCREF(constant);
420 PyTuple_SET_ITEM(newconst, i, constant);
421 }
422
423 /* Append folded constant onto consts */
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000424 if (PyList_Append(consts, newconst)) {
425 Py_DECREF(newconst);
426 return 0;
427 }
428 Py_DECREF(newconst);
429
430 /* Write NOPs over old LOAD_CONSTS and
431 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
432 memset(codestr, NOP, n*3);
433 codestr[n*3] = LOAD_CONST;
434 SETARG(codestr, (n*3), len_consts);
435 return 1;
436}
437
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000438/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
439 with LOAD_CONST binop(c1,c2)
440 The consts table must still be in list form so that the
441 new constant can be appended.
442 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000443 Abandons the transformation if the folding fails (i.e. 1+'a').
444 If the new constant is a sequence, only folds when the size
445 is below a threshold value. That keeps pyc files from
446 becoming large in the presence of code like: (None,)*1000.
447*/
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000448static int
449fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
450{
451 PyObject *newconst, *v, *w;
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000452 int len_consts, opcode, size;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000453
454 /* Pre-conditions */
455 assert(PyList_CheckExact(consts));
456 assert(codestr[0] == LOAD_CONST);
457 assert(codestr[3] == LOAD_CONST);
458
459 /* Create new constant */
460 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
461 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
462 opcode = codestr[6];
463 switch (opcode) {
464 case BINARY_POWER:
465 newconst = PyNumber_Power(v, w, Py_None);
466 break;
467 case BINARY_MULTIPLY:
468 newconst = PyNumber_Multiply(v, w);
469 break;
470 case BINARY_DIVIDE:
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000471 /* Cannot fold this operation statically since
472 the result can depend on the run-time presence of the -Qnew flag */
Armin Rigo664b43b2005-01-07 18:10:51 +0000473 return 0;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000474 case BINARY_TRUE_DIVIDE:
475 newconst = PyNumber_TrueDivide(v, w);
476 break;
477 case BINARY_FLOOR_DIVIDE:
478 newconst = PyNumber_FloorDivide(v, w);
479 break;
480 case BINARY_MODULO:
481 newconst = PyNumber_Remainder(v, w);
482 break;
483 case BINARY_ADD:
484 newconst = PyNumber_Add(v, w);
485 break;
486 case BINARY_SUBTRACT:
487 newconst = PyNumber_Subtract(v, w);
488 break;
489 case BINARY_SUBSCR:
490 newconst = PyObject_GetItem(v, w);
491 break;
492 case BINARY_LSHIFT:
493 newconst = PyNumber_Lshift(v, w);
494 break;
495 case BINARY_RSHIFT:
496 newconst = PyNumber_Rshift(v, w);
497 break;
498 case BINARY_AND:
499 newconst = PyNumber_And(v, w);
500 break;
501 case BINARY_XOR:
502 newconst = PyNumber_Xor(v, w);
503 break;
504 case BINARY_OR:
505 newconst = PyNumber_Or(v, w);
506 break;
507 default:
508 /* Called with an unknown opcode */
509 assert(0);
510 return 0;
511 }
512 if (newconst == NULL) {
513 PyErr_Clear();
514 return 0;
515 }
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000516 size = PyObject_Size(newconst);
517 if (size == -1)
518 PyErr_Clear();
519 else if (size > 20) {
520 Py_DECREF(newconst);
521 return 0;
522 }
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000523
524 /* Append folded constant into consts table */
525 len_consts = PyList_GET_SIZE(consts);
526 if (PyList_Append(consts, newconst)) {
527 Py_DECREF(newconst);
528 return 0;
529 }
530 Py_DECREF(newconst);
531
532 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
533 memset(codestr, NOP, 4);
534 codestr[4] = LOAD_CONST;
535 SETARG(codestr, 4, len_consts);
536 return 1;
537}
538
Raymond Hettinger80121492005-02-20 12:41:32 +0000539static int
540fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
541{
Raymond Hettingere63a0782005-02-23 13:37:55 +0000542 PyObject *newconst=NULL, *v;
Raymond Hettinger80121492005-02-20 12:41:32 +0000543 int len_consts, opcode;
544
545 /* Pre-conditions */
546 assert(PyList_CheckExact(consts));
547 assert(codestr[0] == LOAD_CONST);
548
549 /* Create new constant */
550 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
551 opcode = codestr[3];
552 switch (opcode) {
553 case UNARY_NEGATIVE:
Raymond Hettingere63a0782005-02-23 13:37:55 +0000554 /* Preserve the sign of -0.0 */
555 if (PyObject_IsTrue(v) == 1)
556 newconst = PyNumber_Negative(v);
Raymond Hettinger80121492005-02-20 12:41:32 +0000557 break;
558 case UNARY_CONVERT:
559 newconst = PyObject_Repr(v);
560 break;
561 case UNARY_INVERT:
562 newconst = PyNumber_Invert(v);
563 break;
564 default:
565 /* Called with an unknown opcode */
566 assert(0);
567 return 0;
568 }
569 if (newconst == NULL) {
570 PyErr_Clear();
571 return 0;
572 }
573
574 /* Append folded constant into consts table */
575 len_consts = PyList_GET_SIZE(consts);
576 if (PyList_Append(consts, newconst)) {
577 Py_DECREF(newconst);
578 return 0;
579 }
580 Py_DECREF(newconst);
581
582 /* Write NOP LOAD_CONST newconst */
583 codestr[0] = NOP;
584 codestr[1] = LOAD_CONST;
585 SETARG(codestr, 1, len_consts);
586 return 1;
587}
588
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000589static unsigned int *
590markblocks(unsigned char *code, int len)
591{
592 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000593 int i,j, opcode, blockcnt = 0;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000594
595 if (blocks == NULL)
596 return NULL;
597 memset(blocks, 0, len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000598
599 /* Mark labels in the first pass */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000600 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
601 opcode = code[i];
602 switch (opcode) {
603 case FOR_ITER:
604 case JUMP_FORWARD:
605 case JUMP_IF_FALSE:
606 case JUMP_IF_TRUE:
607 case JUMP_ABSOLUTE:
608 case CONTINUE_LOOP:
609 case SETUP_LOOP:
610 case SETUP_EXCEPT:
611 case SETUP_FINALLY:
612 j = GETJUMPTGT(code, i);
Raymond Hettingereffb3932004-10-30 08:55:08 +0000613 blocks[j] = 1;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000614 break;
615 }
616 }
Raymond Hettingereffb3932004-10-30 08:55:08 +0000617 /* Build block numbers in the second pass */
618 for (i=0 ; i<len ; i++) {
619 blockcnt += blocks[i]; /* increment blockcnt over labels */
620 blocks[i] = blockcnt;
621 }
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000622 return blocks;
623}
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000624
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000625/* Perform basic peephole optimizations to components of a code object.
626 The consts object should still be in list form to allow new constants
627 to be appended.
628
629 To keep the optimizer simple, it bails out (does nothing) for code
630 containing extended arguments or that has a length over 32,700. That
631 allows us to avoid overflow and sign issues. Likewise, it bails when
632 the lineno table has complex encoding for gaps >= 255.
633
634 Optimizations are restricted to simple transformations occuring within a
635 single basic block. All transformations keep the code size the same or
636 smaller. For those that reduce size, the gaps are initially filled with
637 NOPs. Later those NOPs are removed and the jump addresses retargeted in
638 a single pass. Line numbering is adjusted accordingly. */
639
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000640static PyObject *
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000641optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000642{
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000643 int i, j, codelen, nops, h, adj;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000644 int tgt, tgttgt, opcode;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000645 unsigned char *codestr = NULL;
646 unsigned char *lineno;
647 int *addrmap = NULL;
648 int new_line, cum_orig_line, last_line, tabsiz;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000649 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
Raymond Hettingereffb3932004-10-30 08:55:08 +0000650 unsigned int *blocks = NULL;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000651 char *name;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000652
Raymond Hettingereffb3932004-10-30 08:55:08 +0000653 /* Bail out if an exception is set */
654 if (PyErr_Occurred())
655 goto exitUnchanged;
656
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000657 /* Bypass optimization when the lineno table is too complex */
658 assert(PyString_Check(lineno_obj));
Brett Cannonc9371d42005-06-25 08:23:41 +0000659 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000660 tabsiz = PyString_GET_SIZE(lineno_obj);
661 if (memchr(lineno, 255, tabsiz) != NULL)
662 goto exitUnchanged;
663
Raymond Hettingera12fa142004-08-24 04:34:16 +0000664 /* Avoid situations where jump retargeting could overflow */
Raymond Hettinger06cc9732004-09-28 17:22:12 +0000665 assert(PyString_Check(code));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000666 codelen = PyString_Size(code);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000667 if (codelen > 32700)
Raymond Hettingera12fa142004-08-24 04:34:16 +0000668 goto exitUnchanged;
669
670 /* Make a modifiable copy of the code string */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000671 codestr = PyMem_Malloc(codelen);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000672 if (codestr == NULL)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000673 goto exitUnchanged;
674 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
Raymond Hettinger98bd1812004-08-06 19:46:34 +0000675
Raymond Hettinger07359a72005-02-21 20:03:14 +0000676 /* Verify that RETURN_VALUE terminates the codestring. This allows
677 the various transformation patterns to look ahead several
678 instructions without additional checks to make sure they are not
679 looking beyond the end of the code string.
680 */
681 if (codestr[codelen-1] != RETURN_VALUE)
682 goto exitUnchanged;
683
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000684 /* Mapping to new jump targets after NOPs are removed */
685 addrmap = PyMem_Malloc(codelen * sizeof(int));
686 if (addrmap == NULL)
687 goto exitUnchanged;
688
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000689 blocks = markblocks(codestr, codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000690 if (blocks == NULL)
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000691 goto exitUnchanged;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000692 assert(PyList_Check(consts));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000693
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000694 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000695 opcode = codestr[i];
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000696
697 lastlc = cumlc;
698 cumlc = 0;
699
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000700 switch (opcode) {
701
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000702 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000703 with JUMP_IF_TRUE POP_TOP */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000704 case UNARY_NOT:
705 if (codestr[i+1] != JUMP_IF_FALSE ||
706 codestr[i+4] != POP_TOP ||
707 !ISBASICBLOCK(blocks,i,5))
708 continue;
709 tgt = GETJUMPTGT(codestr, (i+1));
710 if (codestr[tgt] != POP_TOP)
711 continue;
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000712 j = GETARG(codestr, i+1) + 1;
713 codestr[i] = JUMP_IF_TRUE;
714 SETARG(codestr, i, j);
715 codestr[i+3] = POP_TOP;
716 codestr[i+4] = NOP;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000717 break;
718
719 /* not a is b --> a is not b
720 not a in b --> a not in b
721 not a is not b --> a is b
Raymond Hettingerb615bf02005-02-10 01:42:32 +0000722 not a not in b --> a in b
Raymond Hettingera1645742005-02-06 22:05:42 +0000723 */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000724 case COMPARE_OP:
725 j = GETARG(codestr, i);
726 if (j < 6 || j > 9 ||
727 codestr[i+3] != UNARY_NOT ||
728 !ISBASICBLOCK(blocks,i,4))
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000729 continue;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000730 SETARG(codestr, i, (j^1));
731 codestr[i+3] = NOP;
Tim Petersdb5860b2004-07-17 05:00:52 +0000732 break;
733
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000734 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
735 case LOAD_NAME:
736 case LOAD_GLOBAL:
737 j = GETARG(codestr, i);
738 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
739 if (name == NULL || strcmp(name, "None") != 0)
740 continue;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000741 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
742 if (PyList_GET_ITEM(consts, j) == Py_None) {
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000743 codestr[i] = LOAD_CONST;
744 SETARG(codestr, i, j);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000745 cumlc = lastlc + 1;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000746 break;
747 }
748 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000749 break;
750
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000751 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000752 case LOAD_CONST:
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000753 cumlc = lastlc + 1;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000754 j = GETARG(codestr, i);
755 if (codestr[i+3] != JUMP_IF_FALSE ||
756 codestr[i+6] != POP_TOP ||
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000757 !ISBASICBLOCK(blocks,i,7) ||
758 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000759 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000760 memset(codestr+i, NOP, 7);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000761 cumlc = 0;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000762 break;
763
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000764 /* Try to fold tuples of constants (includes a case for lists
765 which are only used for "in" and "not in" tests).
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000766 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000767 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
768 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000769 case BUILD_TUPLE:
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000770 case BUILD_LIST:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000771 j = GETARG(codestr, i);
772 h = i - 3 * j;
773 if (h >= 0 &&
Raymond Hettingereffb3932004-10-30 08:55:08 +0000774 j <= lastlc &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000775 ((opcode == BUILD_TUPLE &&
776 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
777 (opcode == BUILD_LIST &&
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000778 codestr[i+3]==COMPARE_OP &&
779 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000780 (GETARG(codestr,i+3)==6 ||
781 GETARG(codestr,i+3)==7))) &&
782 tuple_of_constants(&codestr[h], j, consts)) {
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000783 assert(codestr[i] == LOAD_CONST);
784 cumlc = 1;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000785 break;
786 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000787 if (codestr[i+3] != UNPACK_SEQUENCE ||
788 !ISBASICBLOCK(blocks,i,6) ||
789 j != GETARG(codestr, i+3))
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000790 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000791 if (j == 1) {
792 memset(codestr+i, NOP, 6);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000793 } else if (j == 2) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000794 codestr[i] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000795 memset(codestr+i+1, NOP, 5);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000796 } else if (j == 3) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000797 codestr[i] = ROT_THREE;
798 codestr[i+1] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000799 memset(codestr+i+2, NOP, 4);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000800 }
801 break;
802
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000803 /* Fold binary ops on constants.
804 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
805 case BINARY_POWER:
806 case BINARY_MULTIPLY:
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000807 case BINARY_TRUE_DIVIDE:
808 case BINARY_FLOOR_DIVIDE:
809 case BINARY_MODULO:
810 case BINARY_ADD:
811 case BINARY_SUBTRACT:
812 case BINARY_SUBSCR:
813 case BINARY_LSHIFT:
814 case BINARY_RSHIFT:
815 case BINARY_AND:
816 case BINARY_XOR:
817 case BINARY_OR:
818 if (lastlc >= 2 &&
819 ISBASICBLOCK(blocks, i-6, 7) &&
820 fold_binops_on_constants(&codestr[i-6], consts)) {
821 i -= 2;
822 assert(codestr[i] == LOAD_CONST);
823 cumlc = 1;
824 }
825 break;
826
Raymond Hettinger80121492005-02-20 12:41:32 +0000827 /* Fold unary ops on constants.
828 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
829 case UNARY_NEGATIVE:
830 case UNARY_CONVERT:
831 case UNARY_INVERT:
832 if (lastlc >= 1 &&
833 ISBASICBLOCK(blocks, i-3, 4) &&
834 fold_unaryops_on_constants(&codestr[i-3], consts)) {
835 i -= 2;
836 assert(codestr[i] == LOAD_CONST);
837 cumlc = 1;
838 }
839 break;
840
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000841 /* Simplify conditional jump to conditional jump where the
842 result of the first test implies the success of a similar
843 test or the failure of the opposite test.
844 Arises in code like:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000845 "if a and b:"
846 "if a or b:"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000847 "a and b or c"
Armin Rigod7bcf4d2004-10-30 21:08:59 +0000848 "(a and b) and c"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000849 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
Raymond Hettinger65d3c052004-08-25 15:15:56 +0000850 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
851 where y+3 is the instruction following the second test.
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000852 */
853 case JUMP_IF_FALSE:
854 case JUMP_IF_TRUE:
855 tgt = GETJUMPTGT(codestr, i);
856 j = codestr[tgt];
857 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
858 if (j == opcode) {
859 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
860 SETARG(codestr, i, tgttgt);
861 } else {
862 tgt -= i;
863 SETARG(codestr, i, tgt);
864 }
865 break;
866 }
867 /* Intentional fallthrough */
868
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000869 /* Replace jumps to unconditional jumps */
Raymond Hettinger255a3d02003-04-15 10:35:07 +0000870 case FOR_ITER:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000871 case JUMP_FORWARD:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000872 case JUMP_ABSOLUTE:
873 case CONTINUE_LOOP:
874 case SETUP_LOOP:
875 case SETUP_EXCEPT:
876 case SETUP_FINALLY:
877 tgt = GETJUMPTGT(codestr, i);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000878 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000879 continue;
880 tgttgt = GETJUMPTGT(codestr, tgt);
881 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
882 opcode = JUMP_ABSOLUTE;
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000883 if (!ABSOLUTE_JUMP(opcode))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000884 tgttgt -= i + 3; /* Calc relative jump addr */
885 if (tgttgt < 0) /* No backward relative jumps */
886 continue;
887 codestr[i] = opcode;
888 SETARG(codestr, i, tgttgt);
889 break;
890
891 case EXTENDED_ARG:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000892 goto exitUnchanged;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000893
894 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
895 case RETURN_VALUE:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000896 if (i+4 >= codelen ||
897 codestr[i+4] != RETURN_VALUE ||
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000898 !ISBASICBLOCK(blocks,i,5))
899 continue;
900 memset(codestr+i+1, NOP, 4);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000901 break;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000902 }
903 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000904
905 /* Fixup linenotab */
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000906 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
907 addrmap[i] = i - nops;
908 if (codestr[i] == NOP)
909 nops++;
910 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000911 cum_orig_line = 0;
912 last_line = 0;
913 for (i=0 ; i < tabsiz ; i+=2) {
914 cum_orig_line += lineno[i];
915 new_line = addrmap[cum_orig_line];
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000916 assert (new_line - last_line < 255);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000917 lineno[i] =((unsigned char)(new_line - last_line));
918 last_line = new_line;
919 }
920
921 /* Remove NOPs and fixup jump targets */
922 for (i=0, h=0 ; i<codelen ; ) {
923 opcode = codestr[i];
924 switch (opcode) {
925 case NOP:
926 i++;
927 continue;
928
929 case JUMP_ABSOLUTE:
930 case CONTINUE_LOOP:
931 j = addrmap[GETARG(codestr, i)];
932 SETARG(codestr, i, j);
933 break;
934
935 case FOR_ITER:
936 case JUMP_FORWARD:
937 case JUMP_IF_FALSE:
938 case JUMP_IF_TRUE:
939 case SETUP_LOOP:
940 case SETUP_EXCEPT:
941 case SETUP_FINALLY:
942 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
943 SETARG(codestr, i, j);
944 break;
945 }
946 adj = CODESIZE(opcode);
947 while (adj--)
948 codestr[h++] = codestr[i++];
949 }
Raymond Hettingera12fa142004-08-24 04:34:16 +0000950 assert(h + nops == codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000951
952 code = PyString_FromStringAndSize((char *)codestr, h);
953 PyMem_Free(addrmap);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000954 PyMem_Free(codestr);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000955 PyMem_Free(blocks);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000956 return code;
957
958exitUnchanged:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000959 if (blocks != NULL)
960 PyMem_Free(blocks);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000961 if (addrmap != NULL)
962 PyMem_Free(addrmap);
963 if (codestr != NULL)
964 PyMem_Free(codestr);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000965 Py_INCREF(code);
966 return code;
967}
968
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000969/* End: Peephole optimizations ----------------------------------------- */
970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971/*
972
973Leave this debugging code for just a little longer.
974
975static void
976compiler_display_symbols(PyObject *name, PyObject *symbols)
977{
978 PyObject *key, *value;
979 int flags, pos = 0;
980
981 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
982 while (PyDict_Next(symbols, &pos, &key, &value)) {
983 flags = PyInt_AsLong(value);
984 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
985 if (flags & DEF_GLOBAL)
986 fprintf(stderr, " declared_global");
987 if (flags & DEF_LOCAL)
988 fprintf(stderr, " local");
989 if (flags & DEF_PARAM)
990 fprintf(stderr, " param");
991 if (flags & DEF_STAR)
992 fprintf(stderr, " stararg");
993 if (flags & DEF_DOUBLESTAR)
994 fprintf(stderr, " starstar");
995 if (flags & DEF_INTUPLE)
996 fprintf(stderr, " tuple");
997 if (flags & DEF_FREE)
998 fprintf(stderr, " free");
999 if (flags & DEF_FREE_GLOBAL)
1000 fprintf(stderr, " global");
1001 if (flags & DEF_FREE_CLASS)
1002 fprintf(stderr, " free/class");
1003 if (flags & DEF_IMPORT)
1004 fprintf(stderr, " import");
1005 fprintf(stderr, "\n");
1006 }
1007 fprintf(stderr, "\n");
1008}
1009*/
1010
1011static void
1012compiler_unit_check(struct compiler_unit *u)
1013{
1014 basicblock *block;
1015 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1016 assert(block != (void *)0xcbcbcbcb);
1017 assert(block != (void *)0xfbfbfbfb);
1018 assert(block != (void *)0xdbdbdbdb);
1019 if (block->b_instr != NULL) {
1020 assert(block->b_ialloc > 0);
1021 assert(block->b_iused > 0);
1022 assert(block->b_ialloc >= block->b_iused);
1023 }
1024 else {
1025 assert (block->b_iused == 0);
1026 assert (block->b_ialloc == 0);
1027 }
1028 }
1029}
1030
1031static void
1032compiler_unit_free(struct compiler_unit *u)
1033{
1034 basicblock *b, *next;
1035
1036 compiler_unit_check(u);
1037 b = u->u_blocks;
1038 while (b != NULL) {
1039 if (b->b_instr)
1040 PyObject_Free((void *)b->b_instr);
1041 next = b->b_list;
1042 PyObject_Free((void *)b);
1043 b = next;
1044 }
1045 Py_XDECREF(u->u_ste);
1046 Py_XDECREF(u->u_name);
1047 Py_XDECREF(u->u_consts);
1048 Py_XDECREF(u->u_names);
1049 Py_XDECREF(u->u_varnames);
1050 Py_XDECREF(u->u_freevars);
1051 Py_XDECREF(u->u_cellvars);
1052 Py_XDECREF(u->u_private);
1053 PyObject_Free(u);
1054}
1055
1056static int
1057compiler_enter_scope(struct compiler *c, identifier name, void *key,
1058 int lineno)
1059{
1060 struct compiler_unit *u;
1061
1062 u = PyObject_Malloc(sizeof(struct compiler_unit));
1063 memset(u, 0, sizeof(struct compiler_unit));
1064 u->u_argcount = 0;
1065 u->u_ste = PySymtable_Lookup(c->c_st, key);
1066 if (!u->u_ste) {
1067 compiler_unit_free(u);
1068 return 0;
1069 }
1070 Py_INCREF(name);
1071 u->u_name = name;
1072 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1073 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1074 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1075 PyDict_Size(u->u_cellvars));
1076
1077 u->u_blocks = NULL;
1078 u->u_tmpname = 0;
1079 u->u_nfblocks = 0;
1080 u->u_firstlineno = lineno;
1081 u->u_lineno = 0;
1082 u->u_lineno_set = false;
1083 u->u_consts = PyDict_New();
1084 if (!u->u_consts) {
1085 compiler_unit_free(u);
1086 return 0;
1087 }
1088 u->u_names = PyDict_New();
1089 if (!u->u_names) {
1090 compiler_unit_free(u);
1091 return 0;
1092 }
1093
1094 u->u_private = NULL;
1095
1096 /* Push the old compiler_unit on the stack. */
1097 if (c->u) {
1098 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1099 if (PyList_Append(c->c_stack, wrapper) < 0) {
1100 compiler_unit_free(u);
1101 return 0;
1102 }
1103 Py_DECREF(wrapper);
1104 u->u_private = c->u->u_private;
1105 Py_XINCREF(u->u_private);
1106 }
1107 c->u = u;
1108
1109 c->c_nestlevel++;
1110 if (compiler_use_new_block(c) < 0)
1111 return 0;
1112
1113 return 1;
1114}
1115
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001116static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117compiler_exit_scope(struct compiler *c)
1118{
1119 int n;
1120 PyObject *wrapper;
1121
1122 c->c_nestlevel--;
1123 compiler_unit_free(c->u);
1124 /* Restore c->u to the parent unit. */
1125 n = PyList_GET_SIZE(c->c_stack) - 1;
1126 if (n >= 0) {
1127 wrapper = PyList_GET_ITEM(c->c_stack, n);
1128 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001129 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001131 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 compiler_unit_check(c->u);
1133 }
1134 else
1135 c->u = NULL;
1136
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137}
1138
1139/* Allocate a new block and return a pointer to it.
1140 Returns NULL on error.
1141*/
1142
1143static basicblock *
1144compiler_new_block(struct compiler *c)
1145{
1146 basicblock *b;
1147 struct compiler_unit *u;
1148
1149 u = c->u;
1150 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
1151 if (b == NULL)
1152 return NULL;
1153 memset((void *)b, 0, sizeof(basicblock));
1154 assert (b->b_next == NULL);
1155 b->b_list = u->u_blocks;
1156 u->u_blocks = b;
1157 return b;
1158}
1159
1160static void
1161compiler_use_block(struct compiler *c, basicblock *block)
1162{
1163 assert (block != NULL);
1164 c->u->u_curblock = block;
1165}
1166
1167static basicblock *
1168compiler_use_new_block(struct compiler *c)
1169{
1170 basicblock *block = compiler_new_block(c);
1171 if (block == NULL)
1172 return NULL;
1173 c->u->u_curblock = block;
1174 return block;
1175}
1176
1177static basicblock *
1178compiler_next_block(struct compiler *c)
1179{
1180 basicblock *block = compiler_new_block(c);
1181 if (block == NULL)
1182 return NULL;
1183 c->u->u_curblock->b_next = block;
1184 c->u->u_curblock = block;
1185 return block;
1186}
1187
1188static basicblock *
1189compiler_use_next_block(struct compiler *c, basicblock *block)
1190{
1191 assert(block != NULL);
1192 c->u->u_curblock->b_next = block;
1193 c->u->u_curblock = block;
1194 return block;
1195}
1196
1197/* Returns the offset of the next instruction in the current block's
1198 b_instr array. Resizes the b_instr as necessary.
1199 Returns -1 on failure.
1200 */
1201
1202static int
1203compiler_next_instr(struct compiler *c, basicblock *b)
1204{
1205 assert(b != NULL);
1206 if (b->b_instr == NULL) {
1207 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1208 DEFAULT_BLOCK_SIZE);
1209 if (b->b_instr == NULL) {
1210 PyErr_NoMemory();
1211 return -1;
1212 }
1213 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1214 memset((char *)b->b_instr, 0,
1215 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1216 }
1217 else if (b->b_iused == b->b_ialloc) {
1218 size_t oldsize, newsize;
1219 oldsize = b->b_ialloc * sizeof(struct instr);
1220 newsize = oldsize << 1;
1221 if (newsize == 0) {
1222 PyErr_NoMemory();
1223 return -1;
1224 }
1225 b->b_ialloc <<= 1;
1226 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1227 if (b->b_instr == NULL)
1228 return -1;
1229 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1230 }
1231 return b->b_iused++;
1232}
1233
1234static void
1235compiler_set_lineno(struct compiler *c, int off)
1236{
1237 basicblock *b;
1238 if (c->u->u_lineno_set)
1239 return;
1240 c->u->u_lineno_set = true;
1241 b = c->u->u_curblock;
1242 b->b_instr[off].i_lineno = c->u->u_lineno;
1243}
1244
1245static int
1246opcode_stack_effect(int opcode, int oparg)
1247{
1248 switch (opcode) {
1249 case POP_TOP:
1250 return -1;
1251 case ROT_TWO:
1252 case ROT_THREE:
1253 return 0;
1254 case DUP_TOP:
1255 return 1;
1256 case ROT_FOUR:
1257 return 0;
1258
1259 case UNARY_POSITIVE:
1260 case UNARY_NEGATIVE:
1261 case UNARY_NOT:
1262 case UNARY_CONVERT:
1263 case UNARY_INVERT:
1264 return 0;
1265
1266 case BINARY_POWER:
1267 case BINARY_MULTIPLY:
1268 case BINARY_DIVIDE:
1269 case BINARY_MODULO:
1270 case BINARY_ADD:
1271 case BINARY_SUBTRACT:
1272 case BINARY_SUBSCR:
1273 case BINARY_FLOOR_DIVIDE:
1274 case BINARY_TRUE_DIVIDE:
1275 return -1;
1276 case INPLACE_FLOOR_DIVIDE:
1277 case INPLACE_TRUE_DIVIDE:
1278 return -1;
1279
1280 case SLICE+0:
1281 return 1;
1282 case SLICE+1:
1283 return 0;
1284 case SLICE+2:
1285 return 0;
1286 case SLICE+3:
1287 return -1;
1288
1289 case STORE_SLICE+0:
1290 return -2;
1291 case STORE_SLICE+1:
1292 return -3;
1293 case STORE_SLICE+2:
1294 return -3;
1295 case STORE_SLICE+3:
1296 return -4;
1297
1298 case DELETE_SLICE+0:
1299 return -1;
1300 case DELETE_SLICE+1:
1301 return -2;
1302 case DELETE_SLICE+2:
1303 return -2;
1304 case DELETE_SLICE+3:
1305 return -3;
1306
1307 case INPLACE_ADD:
1308 case INPLACE_SUBTRACT:
1309 case INPLACE_MULTIPLY:
1310 case INPLACE_DIVIDE:
1311 case INPLACE_MODULO:
1312 return -1;
1313 case STORE_SUBSCR:
1314 return -3;
1315 case DELETE_SUBSCR:
1316 return -2;
1317
1318 case BINARY_LSHIFT:
1319 case BINARY_RSHIFT:
1320 case BINARY_AND:
1321 case BINARY_XOR:
1322 case BINARY_OR:
1323 return -1;
1324 case INPLACE_POWER:
1325 return -1;
1326 case GET_ITER:
1327 return 0;
1328
1329 case PRINT_EXPR:
1330 return -1;
1331 case PRINT_ITEM:
1332 return -1;
1333 case PRINT_NEWLINE:
1334 return 0;
1335 case PRINT_ITEM_TO:
1336 return -2;
1337 case PRINT_NEWLINE_TO:
1338 return -1;
1339 case INPLACE_LSHIFT:
1340 case INPLACE_RSHIFT:
1341 case INPLACE_AND:
1342 case INPLACE_XOR:
1343 case INPLACE_OR:
1344 return -1;
1345 case BREAK_LOOP:
1346 return 0;
1347
1348 case LOAD_LOCALS:
1349 return 1;
1350 case RETURN_VALUE:
1351 return -1;
1352 case IMPORT_STAR:
1353 return -1;
1354 case EXEC_STMT:
1355 return -3;
1356 case YIELD_VALUE:
1357 return 0;
1358
1359 case POP_BLOCK:
1360 return 0;
1361 case END_FINALLY:
1362 return -1; /* or -2 or -3 if exception occurred */
1363 case BUILD_CLASS:
1364 return -2;
1365
1366 case STORE_NAME:
1367 return -1;
1368 case DELETE_NAME:
1369 return 0;
1370 case UNPACK_SEQUENCE:
1371 return oparg-1;
1372 case FOR_ITER:
1373 return 1;
1374
1375 case STORE_ATTR:
1376 return -2;
1377 case DELETE_ATTR:
1378 return -1;
1379 case STORE_GLOBAL:
1380 return -1;
1381 case DELETE_GLOBAL:
1382 return 0;
1383 case DUP_TOPX:
1384 return oparg;
1385 case LOAD_CONST:
1386 return 1;
1387 case LOAD_NAME:
1388 return 1;
1389 case BUILD_TUPLE:
1390 case BUILD_LIST:
1391 return 1-oparg;
1392 case BUILD_MAP:
1393 return 1;
1394 case LOAD_ATTR:
1395 return 0;
1396 case COMPARE_OP:
1397 return -1;
1398 case IMPORT_NAME:
1399 return 0;
1400 case IMPORT_FROM:
1401 return 1;
1402
1403 case JUMP_FORWARD:
1404 case JUMP_IF_FALSE:
1405 case JUMP_IF_TRUE:
1406 case JUMP_ABSOLUTE:
1407 return 0;
1408
1409 case LOAD_GLOBAL:
1410 return 1;
1411
1412 case CONTINUE_LOOP:
1413 return 0;
1414 case SETUP_LOOP:
1415 return 0;
1416 case SETUP_EXCEPT:
1417 case SETUP_FINALLY:
1418 return 3; /* actually pushed by an exception */
1419
1420 case LOAD_FAST:
1421 return 1;
1422 case STORE_FAST:
1423 return -1;
1424 case DELETE_FAST:
1425 return 0;
1426
1427 case RAISE_VARARGS:
1428 return -oparg;
1429#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1430 case CALL_FUNCTION:
1431 return -NARGS(oparg);
1432 case CALL_FUNCTION_VAR:
1433 case CALL_FUNCTION_KW:
1434 return -NARGS(oparg)-1;
1435 case CALL_FUNCTION_VAR_KW:
1436 return -NARGS(oparg)-2;
1437#undef NARGS
1438 case MAKE_FUNCTION:
1439 return -oparg;
1440 case BUILD_SLICE:
1441 if (oparg == 3)
1442 return -2;
1443 else
1444 return -1;
1445
1446 case MAKE_CLOSURE:
1447 return -oparg;
1448 case LOAD_CLOSURE:
1449 return 1;
1450 case LOAD_DEREF:
1451 return 1;
1452 case STORE_DEREF:
1453 return -1;
1454 default:
1455 fprintf(stderr, "opcode = %d\n", opcode);
1456 Py_FatalError("opcode_stack_effect()");
1457
1458 }
1459 return 0; /* not reachable */
1460}
1461
1462/* Add an opcode with no argument.
1463 Returns 0 on failure, 1 on success.
1464*/
1465
1466static int
1467compiler_addop(struct compiler *c, int opcode)
1468{
1469 basicblock *b;
1470 struct instr *i;
1471 int off;
1472 off = compiler_next_instr(c, c->u->u_curblock);
1473 if (off < 0)
1474 return 0;
1475 b = c->u->u_curblock;
1476 i = &b->b_instr[off];
1477 i->i_opcode = opcode;
1478 i->i_hasarg = 0;
1479 if (opcode == RETURN_VALUE)
1480 b->b_return = 1;
1481 compiler_set_lineno(c, off);
1482 return 1;
1483}
1484
1485static int
1486compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1487{
1488 PyObject *t, *v;
1489 int arg;
1490
1491 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001492 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 if (t == NULL)
1494 return -1;
1495
1496 v = PyDict_GetItem(dict, t);
1497 if (!v) {
1498 arg = PyDict_Size(dict);
1499 v = PyInt_FromLong(arg);
1500 if (!v) {
1501 Py_DECREF(t);
1502 return -1;
1503 }
1504 if (PyDict_SetItem(dict, t, v) < 0) {
1505 Py_DECREF(t);
1506 Py_DECREF(v);
1507 return -1;
1508 }
1509 Py_DECREF(v);
1510 }
1511 else
1512 arg = PyInt_AsLong(v);
1513 Py_DECREF(t);
1514 return arg;
1515}
1516
1517static int
1518compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1519 PyObject *o)
1520{
1521 int arg = compiler_add_o(c, dict, o);
1522 if (arg < 0)
1523 return 0;
1524 return compiler_addop_i(c, opcode, arg);
1525}
1526
1527static int
1528compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1529 PyObject *o)
1530{
1531 int arg;
1532 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1533 if (!mangled)
1534 return 0;
1535 arg = compiler_add_o(c, dict, mangled);
1536 Py_DECREF(mangled);
1537 if (arg < 0)
1538 return 0;
1539 return compiler_addop_i(c, opcode, arg);
1540}
1541
1542/* Add an opcode with an integer argument.
1543 Returns 0 on failure, 1 on success.
1544*/
1545
1546static int
1547compiler_addop_i(struct compiler *c, int opcode, int oparg)
1548{
1549 struct instr *i;
1550 int off;
1551 off = compiler_next_instr(c, c->u->u_curblock);
1552 if (off < 0)
1553 return 0;
1554 i = &c->u->u_curblock->b_instr[off];
1555 i->i_opcode = opcode;
1556 i->i_oparg = oparg;
1557 i->i_hasarg = 1;
1558 compiler_set_lineno(c, off);
1559 return 1;
1560}
1561
1562static int
1563compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1564{
1565 struct instr *i;
1566 int off;
1567
1568 assert(b != NULL);
1569 off = compiler_next_instr(c, c->u->u_curblock);
1570 if (off < 0)
1571 return 0;
1572 compiler_set_lineno(c, off);
1573 i = &c->u->u_curblock->b_instr[off];
1574 i->i_opcode = opcode;
1575 i->i_target = b;
1576 i->i_hasarg = 1;
1577 if (absolute)
1578 i->i_jabs = 1;
1579 else
1580 i->i_jrel = 1;
1581 return 1;
1582}
1583
1584/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1585 like to find better names.) NEW_BLOCK() creates a new block and sets
1586 it as the current block. NEXT_BLOCK() also creates an implicit jump
1587 from the current block to the new block.
1588*/
1589
1590/* XXX The returns inside these macros make it impossible to decref
1591 objects created in the local function.
1592*/
1593
1594
1595#define NEW_BLOCK(C) { \
1596 if (compiler_use_new_block((C)) == NULL) \
1597 return 0; \
1598}
1599
1600#define NEXT_BLOCK(C) { \
1601 if (compiler_next_block((C)) == NULL) \
1602 return 0; \
1603}
1604
1605#define ADDOP(C, OP) { \
1606 if (!compiler_addop((C), (OP))) \
1607 return 0; \
1608}
1609
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001610#define ADDOP_IN_SCOPE(C, OP) { \
1611 if (!compiler_addop((C), (OP))) { \
1612 compiler_exit_scope(c); \
1613 return 0; \
1614 } \
1615}
1616
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617#define ADDOP_O(C, OP, O, TYPE) { \
1618 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1619 return 0; \
1620}
1621
1622#define ADDOP_NAME(C, OP, O, TYPE) { \
1623 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1624 return 0; \
1625}
1626
1627#define ADDOP_I(C, OP, O) { \
1628 if (!compiler_addop_i((C), (OP), (O))) \
1629 return 0; \
1630}
1631
1632#define ADDOP_JABS(C, OP, O) { \
1633 if (!compiler_addop_j((C), (OP), (O), 1)) \
1634 return 0; \
1635}
1636
1637#define ADDOP_JREL(C, OP, O) { \
1638 if (!compiler_addop_j((C), (OP), (O), 0)) \
1639 return 0; \
1640}
1641
1642/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1643 the ASDL name to synthesize the name of the C type and the visit function.
1644*/
1645
1646#define VISIT(C, TYPE, V) {\
1647 if (!compiler_visit_ ## TYPE((C), (V))) \
1648 return 0; \
1649}
1650
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001651#define VISIT_IN_SCOPE(C, TYPE, V) {\
1652 if (!compiler_visit_ ## TYPE((C), (V))) { \
1653 compiler_exit_scope(c); \
1654 return 0; \
1655 } \
1656}
1657
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658#define VISIT_SLICE(C, V, CTX) {\
1659 if (!compiler_visit_slice((C), (V), (CTX))) \
1660 return 0; \
1661}
1662
1663#define VISIT_SEQ(C, TYPE, SEQ) { \
1664 int i; \
1665 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1666 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1667 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1668 if (!compiler_visit_ ## TYPE((C), elt)) \
1669 return 0; \
1670 } \
1671}
1672
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001673#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1674 int i; \
1675 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1676 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1677 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1678 if (!compiler_visit_ ## TYPE((C), elt)) { \
1679 compiler_exit_scope(c); \
1680 return 0; \
1681 } \
1682 } \
1683}
1684
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685static int
1686compiler_isdocstring(stmt_ty s)
1687{
1688 if (s->kind != Expr_kind)
1689 return 0;
1690 return s->v.Expr.value->kind == Str_kind;
1691}
1692
1693/* Compile a sequence of statements, checking for a docstring. */
1694
1695static int
1696compiler_body(struct compiler *c, asdl_seq *stmts)
1697{
1698 int i = 0;
1699 stmt_ty st;
1700
1701 if (!asdl_seq_LEN(stmts))
1702 return 1;
1703 st = asdl_seq_GET(stmts, 0);
1704 if (compiler_isdocstring(st)) {
1705 i = 1;
1706 VISIT(c, expr, st->v.Expr.value);
1707 if (!compiler_nameop(c, __doc__, Store))
1708 return 0;
1709 }
1710 for (; i < asdl_seq_LEN(stmts); i++)
1711 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1712 return 1;
1713}
1714
1715static PyCodeObject *
1716compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001717{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001718 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 int addNone = 1;
1720 static PyObject *module;
1721 if (!module) {
1722 module = PyString_FromString("<module>");
1723 if (!module)
1724 return NULL;
1725 }
1726 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 switch (mod->kind) {
1729 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001730 if (!compiler_body(c, mod->v.Module.body)) {
1731 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001733 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 break;
1735 case Interactive_kind:
1736 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001737 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 break;
1739 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001740 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 addNone = 0;
1742 break;
1743 case Suite_kind:
1744 assert(0); /* XXX: what should we do here? */
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001745 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Suite.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 break;
1747 default:
1748 assert(0);
Guido van Rossumd076c731998-10-07 19:42:25 +00001749 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750 co = assemble(c, addNone);
1751 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001752 return co;
1753}
1754
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755/* The test for LOCAL must come before the test for FREE in order to
1756 handle classes where name is both local and free. The local var is
1757 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001758*/
1759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760static int
1761get_ref_type(struct compiler *c, PyObject *name)
1762{
1763 int scope = PyST_GetScope(c->u->u_ste, name);
1764 if (scope == 0) {
1765 char buf[350];
1766 PyOS_snprintf(buf, sizeof(buf),
1767 "unknown scope for %.100s in %.100s(%s) in %s\n"
1768 "symbols: %s\nlocals: %s\nglobals: %s\n",
1769 PyString_AS_STRING(name),
1770 PyString_AS_STRING(c->u->u_name),
1771 PyObject_REPR(c->u->u_ste->ste_id),
1772 c->c_filename,
1773 PyObject_REPR(c->u->u_ste->ste_symbols),
1774 PyObject_REPR(c->u->u_varnames),
1775 PyObject_REPR(c->u->u_names)
1776 );
1777 Py_FatalError(buf);
1778 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 return scope;
1781}
1782
1783static int
1784compiler_lookup_arg(PyObject *dict, PyObject *name)
1785{
1786 PyObject *k, *v;
1787 k = Py_BuildValue("(OO)", name, name->ob_type);
1788 if (k == NULL)
1789 return -1;
1790 v = PyDict_GetItem(dict, k);
1791 if (v == NULL)
1792 return -1;
1793 return PyInt_AS_LONG(v);
1794}
1795
1796static int
1797compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1798{
1799 int i, free = PyCode_GetNumFree(co);
1800 if (free == 0) {
1801 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1802 ADDOP_I(c, MAKE_FUNCTION, args);
1803 return 1;
1804 }
1805 for (i = 0; i < free; ++i) {
1806 /* Bypass com_addop_varname because it will generate
1807 LOAD_DEREF but LOAD_CLOSURE is needed.
1808 */
1809 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1810 int arg, reftype;
1811
1812 /* Special case: If a class contains a method with a
1813 free variable that has the same name as a method,
1814 the name will be considered free *and* local in the
1815 class. It should be handled by the closure, as
1816 well as by the normal name loookup logic.
1817 */
1818 reftype = get_ref_type(c, name);
1819 if (reftype == CELL)
1820 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1821 else /* (reftype == FREE) */
1822 arg = compiler_lookup_arg(c->u->u_freevars, name);
1823 if (arg == -1) {
1824 printf("lookup %s in %s %d %d\n"
1825 "freevars of %s: %s\n",
1826 PyObject_REPR(name),
1827 PyString_AS_STRING(c->u->u_name),
1828 reftype, arg,
1829 PyString_AS_STRING(co->co_name),
1830 PyObject_REPR(co->co_freevars));
1831 Py_FatalError("compiler_make_closure()");
1832 }
1833 ADDOP_I(c, LOAD_CLOSURE, arg);
1834 }
1835 ADDOP_I(c, BUILD_TUPLE, free);
1836 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1837 ADDOP_I(c, MAKE_CLOSURE, args);
1838 return 1;
1839}
1840
1841static int
1842compiler_decorators(struct compiler *c, asdl_seq* decos)
1843{
1844 int i;
1845
1846 if (!decos)
1847 return 1;
1848
1849 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1850 VISIT(c, expr, asdl_seq_GET(decos, i));
1851 }
1852 return 1;
1853}
1854
1855static int
1856compiler_arguments(struct compiler *c, arguments_ty args)
1857{
1858 int i;
1859 int n = asdl_seq_LEN(args->args);
1860 /* Correctly handle nested argument lists */
1861 for (i = 0; i < n; i++) {
1862 expr_ty arg = asdl_seq_GET(args->args, i);
1863 if (arg->kind == Tuple_kind) {
1864 PyObject *id = PyString_FromFormat(".%d", i);
1865 if (id == NULL) {
1866 return 0;
1867 }
1868 if (!compiler_nameop(c, id, Load)) {
1869 Py_DECREF(id);
1870 return 0;
1871 }
1872 Py_DECREF(id);
1873 VISIT(c, expr, arg);
1874 }
1875 }
1876 return 1;
1877}
1878
1879static int
1880compiler_function(struct compiler *c, stmt_ty s)
1881{
1882 PyCodeObject *co;
1883 PyObject *first_const = Py_None;
1884 arguments_ty args = s->v.FunctionDef.args;
1885 asdl_seq* decos = s->v.FunctionDef.decorators;
1886 stmt_ty st;
1887 int i, n, docstring;
1888
1889 assert(s->kind == FunctionDef_kind);
1890
1891 if (!compiler_decorators(c, decos))
1892 return 0;
1893 if (args->defaults)
1894 VISIT_SEQ(c, expr, args->defaults);
1895 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1896 s->lineno))
1897 return 0;
1898
1899 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1900 docstring = compiler_isdocstring(st);
1901 if (docstring)
1902 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001903 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1904 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907
1908 /* unpack nested arguments */
1909 compiler_arguments(c, args);
1910
1911 c->u->u_argcount = asdl_seq_LEN(args->args);
1912 n = asdl_seq_LEN(s->v.FunctionDef.body);
1913 /* if there was a docstring, we need to skip the first statement */
1914 for (i = docstring; i < n; i++) {
1915 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1916 if (i == 0 && s2->kind == Expr_kind &&
1917 s2->v.Expr.value->kind == Str_kind)
1918 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001919 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 }
1921 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001922 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923 if (co == NULL)
1924 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925
1926 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1927
1928 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1929 ADDOP_I(c, CALL_FUNCTION, 1);
1930 }
1931
1932 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1933}
1934
1935static int
1936compiler_class(struct compiler *c, stmt_ty s)
1937{
1938 int n;
1939 PyCodeObject *co;
1940 PyObject *str;
1941 /* push class name on stack, needed by BUILD_CLASS */
1942 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1943 /* push the tuple of base classes on the stack */
1944 n = asdl_seq_LEN(s->v.ClassDef.bases);
1945 if (n > 0)
1946 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1947 ADDOP_I(c, BUILD_TUPLE, n);
1948 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1949 s->lineno))
1950 return 0;
1951 c->u->u_private = s->v.ClassDef.name;
1952 Py_INCREF(c->u->u_private);
1953 str = PyString_InternFromString("__name__");
1954 if (!str || !compiler_nameop(c, str, Load)) {
1955 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001956 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 return 0;
1958 }
1959
1960 Py_DECREF(str);
1961 str = PyString_InternFromString("__module__");
1962 if (!str || !compiler_nameop(c, str, Store)) {
1963 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001964 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 return 0;
1966 }
1967 Py_DECREF(str);
1968
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001969 if (!compiler_body(c, s->v.ClassDef.body)) {
1970 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001972 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001974 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1975 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001977 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 if (co == NULL)
1979 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980
1981 compiler_make_closure(c, co, 0);
1982 ADDOP_I(c, CALL_FUNCTION, 0);
1983 ADDOP(c, BUILD_CLASS);
1984 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1985 return 0;
1986 return 1;
1987}
1988
1989static int
1990compiler_lambda(struct compiler *c, expr_ty e)
1991{
1992 PyCodeObject *co;
1993 identifier name;
1994 arguments_ty args = e->v.Lambda.args;
1995 assert(e->kind == Lambda_kind);
1996
Neil Schemenauerccd19212005-10-21 18:09:19 +00001997 name = PyString_InternFromString("<lambda>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 if (!name)
1999 return 0;
2000
2001 if (args->defaults)
2002 VISIT_SEQ(c, expr, args->defaults);
2003 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2004 return 0;
2005
2006 /* unpack nested arguments */
2007 compiler_arguments(c, args);
2008
2009 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002010 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2011 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002013 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 if (co == NULL)
2015 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016
2017 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
2018 Py_DECREF(name);
2019
2020 return 1;
2021}
2022
2023static int
2024compiler_print(struct compiler *c, stmt_ty s)
2025{
2026 int i, n;
2027 bool dest;
2028
2029 assert(s->kind == Print_kind);
2030 n = asdl_seq_LEN(s->v.Print.values);
2031 dest = false;
2032 if (s->v.Print.dest) {
2033 VISIT(c, expr, s->v.Print.dest);
2034 dest = true;
2035 }
2036 for (i = 0; i < n; i++) {
2037 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2038 if (dest) {
2039 ADDOP(c, DUP_TOP);
2040 VISIT(c, expr, e);
2041 ADDOP(c, ROT_TWO);
2042 ADDOP(c, PRINT_ITEM_TO);
2043 }
2044 else {
2045 VISIT(c, expr, e);
2046 ADDOP(c, PRINT_ITEM);
2047 }
2048 }
2049 if (s->v.Print.nl) {
2050 if (dest)
2051 ADDOP(c, PRINT_NEWLINE_TO)
2052 else
2053 ADDOP(c, PRINT_NEWLINE)
2054 }
2055 else if (dest)
2056 ADDOP(c, POP_TOP);
2057 return 1;
2058}
2059
2060static int
2061compiler_if(struct compiler *c, stmt_ty s)
2062{
2063 basicblock *end, *next;
2064
2065 assert(s->kind == If_kind);
2066 end = compiler_new_block(c);
2067 if (end == NULL)
2068 return 0;
2069 next = compiler_new_block(c);
2070 if (next == NULL)
2071 return 0;
2072 VISIT(c, expr, s->v.If.test);
2073 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2074 ADDOP(c, POP_TOP);
2075 VISIT_SEQ(c, stmt, s->v.If.body);
2076 ADDOP_JREL(c, JUMP_FORWARD, end);
2077 compiler_use_next_block(c, next);
2078 ADDOP(c, POP_TOP);
2079 if (s->v.If.orelse)
2080 VISIT_SEQ(c, stmt, s->v.If.orelse);
2081 compiler_use_next_block(c, end);
2082 return 1;
2083}
2084
2085static int
2086compiler_for(struct compiler *c, stmt_ty s)
2087{
2088 basicblock *start, *cleanup, *end;
2089
2090 start = compiler_new_block(c);
2091 cleanup = compiler_new_block(c);
2092 end = compiler_new_block(c);
2093 if (start == NULL || end == NULL || cleanup == NULL)
2094 return 0;
2095 ADDOP_JREL(c, SETUP_LOOP, end);
2096 if (!compiler_push_fblock(c, LOOP, start))
2097 return 0;
2098 VISIT(c, expr, s->v.For.iter);
2099 ADDOP(c, GET_ITER);
2100 compiler_use_next_block(c, start);
2101 ADDOP_JREL(c, FOR_ITER, cleanup);
2102 VISIT(c, expr, s->v.For.target);
2103 VISIT_SEQ(c, stmt, s->v.For.body);
2104 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2105 compiler_use_next_block(c, cleanup);
2106 ADDOP(c, POP_BLOCK);
2107 compiler_pop_fblock(c, LOOP, start);
2108 VISIT_SEQ(c, stmt, s->v.For.orelse);
2109 compiler_use_next_block(c, end);
2110 return 1;
2111}
2112
2113static int
2114compiler_while(struct compiler *c, stmt_ty s)
2115{
2116 basicblock *loop, *orelse, *end, *anchor = NULL;
2117 int constant = expr_constant(s->v.While.test);
2118
2119 if (constant == 0)
2120 return 1;
2121 loop = compiler_new_block(c);
2122 end = compiler_new_block(c);
2123 if (constant == -1) {
2124 anchor = compiler_new_block(c);
2125 if (anchor == NULL)
2126 return 0;
2127 }
2128 if (loop == NULL || end == NULL)
2129 return 0;
2130 if (s->v.While.orelse) {
2131 orelse = compiler_new_block(c);
2132 if (orelse == NULL)
2133 return 0;
2134 }
2135 else
2136 orelse = NULL;
2137
2138 ADDOP_JREL(c, SETUP_LOOP, end);
2139 compiler_use_next_block(c, loop);
2140 if (!compiler_push_fblock(c, LOOP, loop))
2141 return 0;
2142 if (constant == -1) {
2143 VISIT(c, expr, s->v.While.test);
2144 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2145 ADDOP(c, POP_TOP);
2146 }
2147 VISIT_SEQ(c, stmt, s->v.While.body);
2148 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2149
2150 /* XXX should the two POP instructions be in a separate block
2151 if there is no else clause ?
2152 */
2153
2154 if (constant == -1) {
2155 compiler_use_next_block(c, anchor);
2156 ADDOP(c, POP_TOP);
2157 ADDOP(c, POP_BLOCK);
2158 }
2159 compiler_pop_fblock(c, LOOP, loop);
2160 if (orelse != NULL)
2161 VISIT_SEQ(c, stmt, s->v.While.orelse);
2162 compiler_use_next_block(c, end);
2163
2164 return 1;
2165}
2166
2167static int
2168compiler_continue(struct compiler *c)
2169{
2170 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2171 int i;
2172
2173 if (!c->u->u_nfblocks)
2174 return compiler_error(c, LOOP_ERROR_MSG);
2175 i = c->u->u_nfblocks - 1;
2176 switch (c->u->u_fblock[i].fb_type) {
2177 case LOOP:
2178 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2179 break;
2180 case EXCEPT:
2181 case FINALLY_TRY:
2182 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2183 ;
2184 if (i == -1)
2185 return compiler_error(c, LOOP_ERROR_MSG);
2186 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2187 break;
2188 case FINALLY_END:
2189 return compiler_error(c,
2190 "'continue' not supported inside 'finally' clause");
2191 }
2192
2193 return 1;
2194}
2195
2196/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2197
2198 SETUP_FINALLY L
2199 <code for body>
2200 POP_BLOCK
2201 LOAD_CONST <None>
2202 L: <code for finalbody>
2203 END_FINALLY
2204
2205 The special instructions use the block stack. Each block
2206 stack entry contains the instruction that created it (here
2207 SETUP_FINALLY), the level of the value stack at the time the
2208 block stack entry was created, and a label (here L).
2209
2210 SETUP_FINALLY:
2211 Pushes the current value stack level and the label
2212 onto the block stack.
2213 POP_BLOCK:
2214 Pops en entry from the block stack, and pops the value
2215 stack until its level is the same as indicated on the
2216 block stack. (The label is ignored.)
2217 END_FINALLY:
2218 Pops a variable number of entries from the *value* stack
2219 and re-raises the exception they specify. The number of
2220 entries popped depends on the (pseudo) exception type.
2221
2222 The block stack is unwound when an exception is raised:
2223 when a SETUP_FINALLY entry is found, the exception is pushed
2224 onto the value stack (and the exception condition is cleared),
2225 and the interpreter jumps to the label gotten from the block
2226 stack.
2227*/
2228
2229static int
2230compiler_try_finally(struct compiler *c, stmt_ty s)
2231{
2232 basicblock *body, *end;
2233 body = compiler_new_block(c);
2234 end = compiler_new_block(c);
2235 if (body == NULL || end == NULL)
2236 return 0;
2237
2238 ADDOP_JREL(c, SETUP_FINALLY, end);
2239 compiler_use_next_block(c, body);
2240 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2241 return 0;
2242 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2243 ADDOP(c, POP_BLOCK);
2244 compiler_pop_fblock(c, FINALLY_TRY, body);
2245
2246 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2247 compiler_use_next_block(c, end);
2248 if (!compiler_push_fblock(c, FINALLY_END, end))
2249 return 0;
2250 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2251 ADDOP(c, END_FINALLY);
2252 compiler_pop_fblock(c, FINALLY_END, end);
2253
2254 return 1;
2255}
2256
2257/*
2258 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2259 (The contents of the value stack is shown in [], with the top
2260 at the right; 'tb' is trace-back info, 'val' the exception's
2261 associated value, and 'exc' the exception.)
2262
2263 Value stack Label Instruction Argument
2264 [] SETUP_EXCEPT L1
2265 [] <code for S>
2266 [] POP_BLOCK
2267 [] JUMP_FORWARD L0
2268
2269 [tb, val, exc] L1: DUP )
2270 [tb, val, exc, exc] <evaluate E1> )
2271 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2272 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2273 [tb, val, exc, 1] POP )
2274 [tb, val, exc] POP
2275 [tb, val] <assign to V1> (or POP if no V1)
2276 [tb] POP
2277 [] <code for S1>
2278 JUMP_FORWARD L0
2279
2280 [tb, val, exc, 0] L2: POP
2281 [tb, val, exc] DUP
2282 .............................etc.......................
2283
2284 [tb, val, exc, 0] Ln+1: POP
2285 [tb, val, exc] END_FINALLY # re-raise exception
2286
2287 [] L0: <next statement>
2288
2289 Of course, parts are not generated if Vi or Ei is not present.
2290*/
2291static int
2292compiler_try_except(struct compiler *c, stmt_ty s)
2293{
2294 basicblock *body, *orelse, *except, *end;
2295 int i, n;
2296
2297 body = compiler_new_block(c);
2298 except = compiler_new_block(c);
2299 orelse = compiler_new_block(c);
2300 end = compiler_new_block(c);
2301 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2302 return 0;
2303 ADDOP_JREL(c, SETUP_EXCEPT, except);
2304 compiler_use_next_block(c, body);
2305 if (!compiler_push_fblock(c, EXCEPT, body))
2306 return 0;
2307 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2308 ADDOP(c, POP_BLOCK);
2309 compiler_pop_fblock(c, EXCEPT, body);
2310 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2311 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2312 compiler_use_next_block(c, except);
2313 for (i = 0; i < n; i++) {
2314 excepthandler_ty handler = asdl_seq_GET(
2315 s->v.TryExcept.handlers, i);
2316 if (!handler->type && i < n-1)
2317 return compiler_error(c, "default 'except:' must be last");
2318 except = compiler_new_block(c);
2319 if (except == NULL)
2320 return 0;
2321 if (handler->type) {
2322 ADDOP(c, DUP_TOP);
2323 VISIT(c, expr, handler->type);
2324 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2325 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2326 ADDOP(c, POP_TOP);
2327 }
2328 ADDOP(c, POP_TOP);
2329 if (handler->name) {
2330 VISIT(c, expr, handler->name);
2331 }
2332 else {
2333 ADDOP(c, POP_TOP);
2334 }
2335 ADDOP(c, POP_TOP);
2336 VISIT_SEQ(c, stmt, handler->body);
2337 ADDOP_JREL(c, JUMP_FORWARD, end);
2338 compiler_use_next_block(c, except);
2339 if (handler->type)
2340 ADDOP(c, POP_TOP);
2341 }
2342 ADDOP(c, END_FINALLY);
2343 compiler_use_next_block(c, orelse);
2344 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2345 compiler_use_next_block(c, end);
2346 return 1;
2347}
2348
2349static int
2350compiler_import_as(struct compiler *c, identifier name, identifier asname)
2351{
2352 /* The IMPORT_NAME opcode was already generated. This function
2353 merely needs to bind the result to a name.
2354
2355 If there is a dot in name, we need to split it and emit a
2356 LOAD_ATTR for each name.
2357 */
2358 const char *src = PyString_AS_STRING(name);
2359 const char *dot = strchr(src, '.');
2360 if (dot) {
2361 /* Consume the base module name to get the first attribute */
2362 src = dot + 1;
2363 while (dot) {
2364 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002365 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002367 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 dot ? dot - src : strlen(src));
2369 ADDOP_O(c, LOAD_ATTR, attr, names);
2370 src = dot + 1;
2371 }
2372 }
2373 return compiler_nameop(c, asname, Store);
2374}
2375
2376static int
2377compiler_import(struct compiler *c, stmt_ty s)
2378{
2379 /* The Import node stores a module name like a.b.c as a single
2380 string. This is convenient for all cases except
2381 import a.b.c as d
2382 where we need to parse that string to extract the individual
2383 module names.
2384 XXX Perhaps change the representation to make this case simpler?
2385 */
2386 int i, n = asdl_seq_LEN(s->v.Import.names);
2387 for (i = 0; i < n; i++) {
2388 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2389 int r;
2390
2391 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2392 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2393
2394 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002395 r = compiler_import_as(c, alias->name, alias->asname);
2396 if (!r)
2397 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 }
2399 else {
2400 identifier tmp = alias->name;
2401 const char *base = PyString_AS_STRING(alias->name);
2402 char *dot = strchr(base, '.');
2403 if (dot)
2404 tmp = PyString_FromStringAndSize(base,
2405 dot - base);
2406 r = compiler_nameop(c, tmp, Store);
2407 if (dot) {
2408 Py_DECREF(tmp);
2409 }
2410 if (!r)
2411 return r;
2412 }
2413 }
2414 return 1;
2415}
2416
2417static int
2418compiler_from_import(struct compiler *c, stmt_ty s)
2419{
2420 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2421 int star = 0;
2422
2423 PyObject *names = PyTuple_New(n);
2424 if (!names)
2425 return 0;
2426
2427 /* build up the names */
2428 for (i = 0; i < n; i++) {
2429 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2430 Py_INCREF(alias->name);
2431 PyTuple_SET_ITEM(names, i, alias->name);
2432 }
2433
2434 if (s->lineno > c->c_future->ff_lineno) {
2435 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2436 "__future__")) {
2437 Py_DECREF(names);
2438 return compiler_error(c,
2439 "from __future__ imports must occur "
2440 "at the beginning of the file");
2441
2442 }
2443 }
2444
2445 ADDOP_O(c, LOAD_CONST, names, consts);
2446 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2447 for (i = 0; i < n; i++) {
2448 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2449 identifier store_name;
2450
2451 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2452 assert(n == 1);
2453 ADDOP(c, IMPORT_STAR);
2454 star = 1;
2455 break;
2456 }
2457
2458 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2459 store_name = alias->name;
2460 if (alias->asname)
2461 store_name = alias->asname;
2462
2463 if (!compiler_nameop(c, store_name, Store)) {
2464 Py_DECREF(names);
2465 return 0;
2466 }
2467 }
2468 if (!star)
2469 /* remove imported module */
2470 ADDOP(c, POP_TOP);
2471 return 1;
2472}
2473
2474static int
2475compiler_assert(struct compiler *c, stmt_ty s)
2476{
2477 static PyObject *assertion_error = NULL;
2478 basicblock *end;
2479
2480 if (Py_OptimizeFlag)
2481 return 1;
2482 if (assertion_error == NULL) {
2483 assertion_error = PyString_FromString("AssertionError");
2484 if (assertion_error == NULL)
2485 return 0;
2486 }
2487 VISIT(c, expr, s->v.Assert.test);
2488 end = compiler_new_block(c);
2489 if (end == NULL)
2490 return 0;
2491 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2492 ADDOP(c, POP_TOP);
2493 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2494 if (s->v.Assert.msg) {
2495 VISIT(c, expr, s->v.Assert.msg);
2496 ADDOP_I(c, RAISE_VARARGS, 2);
2497 }
2498 else {
2499 ADDOP_I(c, RAISE_VARARGS, 1);
2500 }
2501 compiler_use_block(c, end);
2502 ADDOP(c, POP_TOP);
2503 return 1;
2504}
2505
2506static int
2507compiler_visit_stmt(struct compiler *c, stmt_ty s)
2508{
2509 int i, n;
2510
2511 c->u->u_lineno = s->lineno;
2512 c->u->u_lineno_set = false;
2513 switch (s->kind) {
2514 case FunctionDef_kind:
2515 return compiler_function(c, s);
2516 case ClassDef_kind:
2517 return compiler_class(c, s);
2518 case Return_kind:
2519 if (c->u->u_ste->ste_type != FunctionBlock)
2520 return compiler_error(c, "'return' outside function");
2521 if (s->v.Return.value) {
2522 if (c->u->u_ste->ste_generator) {
2523 return compiler_error(c,
2524 "'return' with argument inside generator");
2525 }
2526 VISIT(c, expr, s->v.Return.value);
2527 }
2528 else
2529 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2530 ADDOP(c, RETURN_VALUE);
2531 break;
2532 case Delete_kind:
2533 VISIT_SEQ(c, expr, s->v.Delete.targets)
2534 break;
2535 case Assign_kind:
2536 n = asdl_seq_LEN(s->v.Assign.targets);
2537 VISIT(c, expr, s->v.Assign.value);
2538 for (i = 0; i < n; i++) {
2539 if (i < n - 1)
2540 ADDOP(c, DUP_TOP);
2541 VISIT(c, expr,
2542 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2543 }
2544 break;
2545 case AugAssign_kind:
2546 return compiler_augassign(c, s);
2547 case Print_kind:
2548 return compiler_print(c, s);
2549 case For_kind:
2550 return compiler_for(c, s);
2551 case While_kind:
2552 return compiler_while(c, s);
2553 case If_kind:
2554 return compiler_if(c, s);
2555 case Raise_kind:
2556 n = 0;
2557 if (s->v.Raise.type) {
2558 VISIT(c, expr, s->v.Raise.type);
2559 n++;
2560 if (s->v.Raise.inst) {
2561 VISIT(c, expr, s->v.Raise.inst);
2562 n++;
2563 if (s->v.Raise.tback) {
2564 VISIT(c, expr, s->v.Raise.tback);
2565 n++;
2566 }
2567 }
2568 }
2569 ADDOP_I(c, RAISE_VARARGS, n);
2570 break;
2571 case TryExcept_kind:
2572 return compiler_try_except(c, s);
2573 case TryFinally_kind:
2574 return compiler_try_finally(c, s);
2575 case Assert_kind:
2576 return compiler_assert(c, s);
2577 case Import_kind:
2578 return compiler_import(c, s);
2579 case ImportFrom_kind:
2580 return compiler_from_import(c, s);
2581 case Exec_kind:
2582 VISIT(c, expr, s->v.Exec.body);
2583 if (s->v.Exec.globals) {
2584 VISIT(c, expr, s->v.Exec.globals);
2585 if (s->v.Exec.locals) {
2586 VISIT(c, expr, s->v.Exec.locals);
2587 } else {
2588 ADDOP(c, DUP_TOP);
2589 }
2590 } else {
2591 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2592 ADDOP(c, DUP_TOP);
2593 }
2594 ADDOP(c, EXEC_STMT);
2595 break;
2596 case Global_kind:
2597 break;
2598 case Expr_kind:
2599 VISIT(c, expr, s->v.Expr.value);
2600 if (c->c_interactive && c->c_nestlevel <= 1) {
2601 ADDOP(c, PRINT_EXPR);
2602 }
2603 else {
2604 ADDOP(c, POP_TOP);
2605 }
2606 break;
2607 case Pass_kind:
2608 break;
2609 case Break_kind:
2610 if (!c->u->u_nfblocks)
2611 return compiler_error(c, "'break' outside loop");
2612 ADDOP(c, BREAK_LOOP);
2613 break;
2614 case Continue_kind:
2615 return compiler_continue(c);
2616 }
2617 return 1;
2618}
2619
2620static int
2621unaryop(unaryop_ty op)
2622{
2623 switch (op) {
2624 case Invert:
2625 return UNARY_INVERT;
2626 case Not:
2627 return UNARY_NOT;
2628 case UAdd:
2629 return UNARY_POSITIVE;
2630 case USub:
2631 return UNARY_NEGATIVE;
2632 }
2633 return 0;
2634}
2635
2636static int
2637binop(struct compiler *c, operator_ty op)
2638{
2639 switch (op) {
2640 case Add:
2641 return BINARY_ADD;
2642 case Sub:
2643 return BINARY_SUBTRACT;
2644 case Mult:
2645 return BINARY_MULTIPLY;
2646 case Div:
2647 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2648 return BINARY_TRUE_DIVIDE;
2649 else
2650 return BINARY_DIVIDE;
2651 case Mod:
2652 return BINARY_MODULO;
2653 case Pow:
2654 return BINARY_POWER;
2655 case LShift:
2656 return BINARY_LSHIFT;
2657 case RShift:
2658 return BINARY_RSHIFT;
2659 case BitOr:
2660 return BINARY_OR;
2661 case BitXor:
2662 return BINARY_XOR;
2663 case BitAnd:
2664 return BINARY_AND;
2665 case FloorDiv:
2666 return BINARY_FLOOR_DIVIDE;
2667 }
2668 return 0;
2669}
2670
2671static int
2672cmpop(cmpop_ty op)
2673{
2674 switch (op) {
2675 case Eq:
2676 return PyCmp_EQ;
2677 case NotEq:
2678 return PyCmp_NE;
2679 case Lt:
2680 return PyCmp_LT;
2681 case LtE:
2682 return PyCmp_LE;
2683 case Gt:
2684 return PyCmp_GT;
2685 case GtE:
2686 return PyCmp_GE;
2687 case Is:
2688 return PyCmp_IS;
2689 case IsNot:
2690 return PyCmp_IS_NOT;
2691 case In:
2692 return PyCmp_IN;
2693 case NotIn:
2694 return PyCmp_NOT_IN;
2695 }
2696 return PyCmp_BAD;
2697}
2698
2699static int
2700inplace_binop(struct compiler *c, operator_ty op)
2701{
2702 switch (op) {
2703 case Add:
2704 return INPLACE_ADD;
2705 case Sub:
2706 return INPLACE_SUBTRACT;
2707 case Mult:
2708 return INPLACE_MULTIPLY;
2709 case Div:
2710 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2711 return INPLACE_TRUE_DIVIDE;
2712 else
2713 return INPLACE_DIVIDE;
2714 case Mod:
2715 return INPLACE_MODULO;
2716 case Pow:
2717 return INPLACE_POWER;
2718 case LShift:
2719 return INPLACE_LSHIFT;
2720 case RShift:
2721 return INPLACE_RSHIFT;
2722 case BitOr:
2723 return INPLACE_OR;
2724 case BitXor:
2725 return INPLACE_XOR;
2726 case BitAnd:
2727 return INPLACE_AND;
2728 case FloorDiv:
2729 return INPLACE_FLOOR_DIVIDE;
2730 }
2731 assert(0);
2732 return 0;
2733}
2734
2735static int
2736compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2737{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002738 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2740
2741 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002742 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 /* XXX AugStore isn't used anywhere! */
2744
2745 /* First check for assignment to __debug__. Param? */
2746 if ((ctx == Store || ctx == AugStore || ctx == Del)
2747 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2748 return compiler_error(c, "can not assign to __debug__");
2749 }
2750
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002751 mangled = _Py_Mangle(c->u->u_private, name);
2752 if (!mangled)
2753 return 0;
2754
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 op = 0;
2756 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002757 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 switch (scope) {
2759 case FREE:
2760 dict = c->u->u_freevars;
2761 optype = OP_DEREF;
2762 break;
2763 case CELL:
2764 dict = c->u->u_cellvars;
2765 optype = OP_DEREF;
2766 break;
2767 case LOCAL:
2768 if (c->u->u_ste->ste_type == FunctionBlock)
2769 optype = OP_FAST;
2770 break;
2771 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002772 if (c->u->u_ste->ste_type == FunctionBlock &&
2773 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 optype = OP_GLOBAL;
2775 break;
2776 case GLOBAL_EXPLICIT:
2777 optype = OP_GLOBAL;
2778 break;
2779 }
2780
2781 /* XXX Leave assert here, but handle __doc__ and the like better */
2782 assert(scope || PyString_AS_STRING(name)[0] == '_');
2783
2784 switch (optype) {
2785 case OP_DEREF:
2786 switch (ctx) {
2787 case Load: op = LOAD_DEREF; break;
2788 case Store: op = STORE_DEREF; break;
2789 case AugLoad:
2790 case AugStore:
2791 break;
2792 case Del:
2793 PyErr_Format(PyExc_SyntaxError,
2794 "can not delete variable '%s' referenced "
2795 "in nested scope",
2796 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002797 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 return 0;
2799 break;
2800 case Param:
2801 assert(0); /* impossible */
2802 }
2803 break;
2804 case OP_FAST:
2805 switch (ctx) {
2806 case Load: op = LOAD_FAST; break;
2807 case Store: op = STORE_FAST; break;
2808 case Del: op = DELETE_FAST; break;
2809 case AugLoad:
2810 case AugStore:
2811 break;
2812 case Param:
2813 assert(0); /* impossible */
2814 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002815 ADDOP_O(c, op, mangled, varnames);
2816 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 return 1;
2818 case OP_GLOBAL:
2819 switch (ctx) {
2820 case Load: op = LOAD_GLOBAL; break;
2821 case Store: op = STORE_GLOBAL; break;
2822 case Del: op = DELETE_GLOBAL; break;
2823 case AugLoad:
2824 case AugStore:
2825 break;
2826 case Param:
2827 assert(0); /* impossible */
2828 }
2829 break;
2830 case OP_NAME:
2831 switch (ctx) {
2832 case Load: op = LOAD_NAME; break;
2833 case Store: op = STORE_NAME; break;
2834 case Del: op = DELETE_NAME; break;
2835 case AugLoad:
2836 case AugStore:
2837 break;
2838 case Param:
2839 assert(0); /* impossible */
2840 }
2841 break;
2842 }
2843
2844 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002845 arg = compiler_add_o(c, dict, mangled);
2846 if (arg < 0)
2847 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002848 Py_DECREF(mangled);
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002849 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850}
2851
2852static int
2853compiler_boolop(struct compiler *c, expr_ty e)
2854{
2855 basicblock *end;
2856 int jumpi, i, n;
2857 asdl_seq *s;
2858
2859 assert(e->kind == BoolOp_kind);
2860 if (e->v.BoolOp.op == And)
2861 jumpi = JUMP_IF_FALSE;
2862 else
2863 jumpi = JUMP_IF_TRUE;
2864 end = compiler_new_block(c);
2865 if (end < 0)
2866 return 0;
2867 s = e->v.BoolOp.values;
2868 n = asdl_seq_LEN(s) - 1;
2869 for (i = 0; i < n; ++i) {
2870 VISIT(c, expr, asdl_seq_GET(s, i));
2871 ADDOP_JREL(c, jumpi, end);
2872 ADDOP(c, POP_TOP)
2873 }
2874 VISIT(c, expr, asdl_seq_GET(s, n));
2875 compiler_use_next_block(c, end);
2876 return 1;
2877}
2878
2879static int
2880compiler_list(struct compiler *c, expr_ty e)
2881{
2882 int n = asdl_seq_LEN(e->v.List.elts);
2883 if (e->v.List.ctx == Store) {
2884 ADDOP_I(c, UNPACK_SEQUENCE, n);
2885 }
2886 VISIT_SEQ(c, expr, e->v.List.elts);
2887 if (e->v.List.ctx == Load) {
2888 ADDOP_I(c, BUILD_LIST, n);
2889 }
2890 return 1;
2891}
2892
2893static int
2894compiler_tuple(struct compiler *c, expr_ty e)
2895{
2896 int n = asdl_seq_LEN(e->v.Tuple.elts);
2897 if (e->v.Tuple.ctx == Store) {
2898 ADDOP_I(c, UNPACK_SEQUENCE, n);
2899 }
2900 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2901 if (e->v.Tuple.ctx == Load) {
2902 ADDOP_I(c, BUILD_TUPLE, n);
2903 }
2904 return 1;
2905}
2906
2907static int
2908compiler_compare(struct compiler *c, expr_ty e)
2909{
2910 int i, n;
2911 basicblock *cleanup = NULL;
2912
2913 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2914 VISIT(c, expr, e->v.Compare.left);
2915 n = asdl_seq_LEN(e->v.Compare.ops);
2916 assert(n > 0);
2917 if (n > 1) {
2918 cleanup = compiler_new_block(c);
2919 if (cleanup == NULL)
2920 return 0;
2921 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2922 }
2923 for (i = 1; i < n; i++) {
2924 ADDOP(c, DUP_TOP);
2925 ADDOP(c, ROT_THREE);
2926 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2927 ADDOP_I(c, COMPARE_OP,
2928 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2929 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2930 NEXT_BLOCK(c);
2931 ADDOP(c, POP_TOP);
2932 if (i < (n - 1))
2933 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2934 }
2935 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2936 ADDOP_I(c, COMPARE_OP,
2937 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2938 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2939 if (n > 1) {
2940 basicblock *end = compiler_new_block(c);
2941 if (end == NULL)
2942 return 0;
2943 ADDOP_JREL(c, JUMP_FORWARD, end);
2944 compiler_use_next_block(c, cleanup);
2945 ADDOP(c, ROT_TWO);
2946 ADDOP(c, POP_TOP);
2947 compiler_use_next_block(c, end);
2948 }
2949 return 1;
2950}
2951
2952static int
2953compiler_call(struct compiler *c, expr_ty e)
2954{
2955 int n, code = 0;
2956
2957 VISIT(c, expr, e->v.Call.func);
2958 n = asdl_seq_LEN(e->v.Call.args);
2959 VISIT_SEQ(c, expr, e->v.Call.args);
2960 if (e->v.Call.keywords) {
2961 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2962 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2963 }
2964 if (e->v.Call.starargs) {
2965 VISIT(c, expr, e->v.Call.starargs);
2966 code |= 1;
2967 }
2968 if (e->v.Call.kwargs) {
2969 VISIT(c, expr, e->v.Call.kwargs);
2970 code |= 2;
2971 }
2972 switch (code) {
2973 case 0:
2974 ADDOP_I(c, CALL_FUNCTION, n);
2975 break;
2976 case 1:
2977 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2978 break;
2979 case 2:
2980 ADDOP_I(c, CALL_FUNCTION_KW, n);
2981 break;
2982 case 3:
2983 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2984 break;
2985 }
2986 return 1;
2987}
2988
2989static int
2990compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
2991 asdl_seq *generators, int gen_index,
2992 expr_ty elt)
2993{
2994 /* generate code for the iterator, then each of the ifs,
2995 and then write to the element */
2996
2997 comprehension_ty l;
2998 basicblock *start, *anchor, *skip, *if_cleanup;
2999 int i, n;
3000
3001 start = compiler_new_block(c);
3002 skip = compiler_new_block(c);
3003 if_cleanup = compiler_new_block(c);
3004 anchor = compiler_new_block(c);
3005
3006 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3007 anchor == NULL)
3008 return 0;
3009
3010 l = asdl_seq_GET(generators, gen_index);
3011 VISIT(c, expr, l->iter);
3012 ADDOP(c, GET_ITER);
3013 compiler_use_next_block(c, start);
3014 ADDOP_JREL(c, FOR_ITER, anchor);
3015 NEXT_BLOCK(c);
3016 VISIT(c, expr, l->target);
3017
3018 /* XXX this needs to be cleaned up...a lot! */
3019 n = asdl_seq_LEN(l->ifs);
3020 for (i = 0; i < n; i++) {
3021 expr_ty e = asdl_seq_GET(l->ifs, i);
3022 VISIT(c, expr, e);
3023 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3024 NEXT_BLOCK(c);
3025 ADDOP(c, POP_TOP);
3026 }
3027
3028 if (++gen_index < asdl_seq_LEN(generators))
3029 if (!compiler_listcomp_generator(c, tmpname,
3030 generators, gen_index, elt))
3031 return 0;
3032
3033 /* only append after the last for generator */
3034 if (gen_index >= asdl_seq_LEN(generators)) {
3035 if (!compiler_nameop(c, tmpname, Load))
3036 return 0;
3037 VISIT(c, expr, elt);
3038 ADDOP_I(c, CALL_FUNCTION, 1);
3039 ADDOP(c, POP_TOP);
3040
3041 compiler_use_next_block(c, skip);
3042 }
3043 for (i = 0; i < n; i++) {
3044 ADDOP_I(c, JUMP_FORWARD, 1);
3045 if (i == 0)
3046 compiler_use_next_block(c, if_cleanup);
3047 ADDOP(c, POP_TOP);
3048 }
3049 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3050 compiler_use_next_block(c, anchor);
3051 /* delete the append method added to locals */
3052 if (gen_index == 1)
3053 if (!compiler_nameop(c, tmpname, Del))
3054 return 0;
3055
3056 return 1;
3057}
3058
3059static int
3060compiler_listcomp(struct compiler *c, expr_ty e)
3061{
3062 char tmpname[256];
3063 identifier tmp;
3064 int rc = 0;
3065 static identifier append;
3066 asdl_seq *generators = e->v.ListComp.generators;
3067
3068 assert(e->kind == ListComp_kind);
3069 if (!append) {
3070 append = PyString_InternFromString("append");
3071 if (!append)
3072 return 0;
3073 }
3074 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3075 tmp = PyString_FromString(tmpname);
3076 if (!tmp)
3077 return 0;
3078 ADDOP_I(c, BUILD_LIST, 0);
3079 ADDOP(c, DUP_TOP);
3080 ADDOP_O(c, LOAD_ATTR, append, names);
3081 if (compiler_nameop(c, tmp, Store))
3082 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3083 e->v.ListComp.elt);
3084 Py_DECREF(tmp);
3085 return rc;
3086}
3087
3088static int
3089compiler_genexp_generator(struct compiler *c,
3090 asdl_seq *generators, int gen_index,
3091 expr_ty elt)
3092{
3093 /* generate code for the iterator, then each of the ifs,
3094 and then write to the element */
3095
3096 comprehension_ty ge;
3097 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3098 int i, n;
3099
3100 start = compiler_new_block(c);
3101 skip = compiler_new_block(c);
3102 if_cleanup = compiler_new_block(c);
3103 anchor = compiler_new_block(c);
3104 end = compiler_new_block(c);
3105
3106 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3107 anchor == NULL || end == NULL)
3108 return 0;
3109
3110 ge = asdl_seq_GET(generators, gen_index);
3111 ADDOP_JREL(c, SETUP_LOOP, end);
3112 if (!compiler_push_fblock(c, LOOP, start))
3113 return 0;
3114
3115 if (gen_index == 0) {
3116 /* Receive outermost iter as an implicit argument */
3117 c->u->u_argcount = 1;
3118 ADDOP_I(c, LOAD_FAST, 0);
3119 }
3120 else {
3121 /* Sub-iter - calculate on the fly */
3122 VISIT(c, expr, ge->iter);
3123 ADDOP(c, GET_ITER);
3124 }
3125 compiler_use_next_block(c, start);
3126 ADDOP_JREL(c, FOR_ITER, anchor);
3127 NEXT_BLOCK(c);
3128 VISIT(c, expr, ge->target);
3129
3130 /* XXX this needs to be cleaned up...a lot! */
3131 n = asdl_seq_LEN(ge->ifs);
3132 for (i = 0; i < n; i++) {
3133 expr_ty e = asdl_seq_GET(ge->ifs, i);
3134 VISIT(c, expr, e);
3135 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3136 NEXT_BLOCK(c);
3137 ADDOP(c, POP_TOP);
3138 }
3139
3140 if (++gen_index < asdl_seq_LEN(generators))
3141 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3142 return 0;
3143
3144 /* only append after the last 'for' generator */
3145 if (gen_index >= asdl_seq_LEN(generators)) {
3146 VISIT(c, expr, elt);
3147 ADDOP(c, YIELD_VALUE);
3148 ADDOP(c, POP_TOP);
3149
3150 compiler_use_next_block(c, skip);
3151 }
3152 for (i = 0; i < n; i++) {
3153 ADDOP_I(c, JUMP_FORWARD, 1);
3154 if (i == 0)
3155 compiler_use_next_block(c, if_cleanup);
3156
3157 ADDOP(c, POP_TOP);
3158 }
3159 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3160 compiler_use_next_block(c, anchor);
3161 ADDOP(c, POP_BLOCK);
3162 compiler_pop_fblock(c, LOOP, start);
3163 compiler_use_next_block(c, end);
3164
3165 return 1;
3166}
3167
3168static int
3169compiler_genexp(struct compiler *c, expr_ty e)
3170{
3171 PyObject *name;
3172 PyCodeObject *co;
3173 expr_ty outermost_iter = ((comprehension_ty)
3174 (asdl_seq_GET(e->v.GeneratorExp.generators,
3175 0)))->iter;
3176
3177 name = PyString_FromString("<generator expression>");
3178 if (!name)
3179 return 0;
3180
3181 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3182 return 0;
3183 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3184 e->v.GeneratorExp.elt);
3185 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003186 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187 if (co == NULL)
3188 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189
3190 compiler_make_closure(c, co, 0);
3191 VISIT(c, expr, outermost_iter);
3192 ADDOP(c, GET_ITER);
3193 ADDOP_I(c, CALL_FUNCTION, 1);
3194 Py_DECREF(name);
3195 Py_DECREF(co);
3196
3197 return 1;
3198}
3199
3200static int
3201compiler_visit_keyword(struct compiler *c, keyword_ty k)
3202{
3203 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3204 VISIT(c, expr, k->value);
3205 return 1;
3206}
3207
3208/* Test whether expression is constant. For constants, report
3209 whether they are true or false.
3210
3211 Return values: 1 for true, 0 for false, -1 for non-constant.
3212 */
3213
3214static int
3215expr_constant(expr_ty e)
3216{
3217 switch (e->kind) {
3218 case Num_kind:
3219 return PyObject_IsTrue(e->v.Num.n);
3220 case Str_kind:
3221 return PyObject_IsTrue(e->v.Str.s);
3222 default:
3223 return -1;
3224 }
3225}
3226
3227static int
3228compiler_visit_expr(struct compiler *c, expr_ty e)
3229{
3230 int i, n;
3231
3232 if (e->lineno > c->u->u_lineno) {
3233 c->u->u_lineno = e->lineno;
3234 c->u->u_lineno_set = false;
3235 }
3236 switch (e->kind) {
3237 case BoolOp_kind:
3238 return compiler_boolop(c, e);
3239 case BinOp_kind:
3240 VISIT(c, expr, e->v.BinOp.left);
3241 VISIT(c, expr, e->v.BinOp.right);
3242 ADDOP(c, binop(c, e->v.BinOp.op));
3243 break;
3244 case UnaryOp_kind:
3245 VISIT(c, expr, e->v.UnaryOp.operand);
3246 ADDOP(c, unaryop(e->v.UnaryOp.op));
3247 break;
3248 case Lambda_kind:
3249 return compiler_lambda(c, e);
3250 case Dict_kind:
3251 /* XXX get rid of arg? */
3252 ADDOP_I(c, BUILD_MAP, 0);
3253 n = asdl_seq_LEN(e->v.Dict.values);
3254 /* We must arrange things just right for STORE_SUBSCR.
3255 It wants the stack to look like (value) (dict) (key) */
3256 for (i = 0; i < n; i++) {
3257 ADDOP(c, DUP_TOP);
3258 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3259 ADDOP(c, ROT_TWO);
3260 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3261 ADDOP(c, STORE_SUBSCR);
3262 }
3263 break;
3264 case ListComp_kind:
3265 return compiler_listcomp(c, e);
3266 case GeneratorExp_kind:
3267 return compiler_genexp(c, e);
3268 case Yield_kind:
3269 if (c->u->u_ste->ste_type != FunctionBlock)
3270 return compiler_error(c, "'yield' outside function");
3271 /*
3272 for (i = 0; i < c->u->u_nfblocks; i++) {
3273 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3274 return compiler_error(
3275 c, "'yield' not allowed in a 'try' "
3276 "block with a 'finally' clause");
3277 }
3278 */
3279 if (e->v.Yield.value) {
3280 VISIT(c, expr, e->v.Yield.value);
3281 }
3282 else {
3283 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3284 }
3285 ADDOP(c, YIELD_VALUE);
3286 break;
3287 case Compare_kind:
3288 return compiler_compare(c, e);
3289 case Call_kind:
3290 return compiler_call(c, e);
3291 case Repr_kind:
3292 VISIT(c, expr, e->v.Repr.value);
3293 ADDOP(c, UNARY_CONVERT);
3294 break;
3295 case Num_kind:
3296 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3297 break;
3298 case Str_kind:
3299 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3300 break;
3301 /* The following exprs can be assignment targets. */
3302 case Attribute_kind:
3303 if (e->v.Attribute.ctx != AugStore)
3304 VISIT(c, expr, e->v.Attribute.value);
3305 switch (e->v.Attribute.ctx) {
3306 case AugLoad:
3307 ADDOP(c, DUP_TOP);
3308 /* Fall through to load */
3309 case Load:
3310 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3311 break;
3312 case AugStore:
3313 ADDOP(c, ROT_TWO);
3314 /* Fall through to save */
3315 case Store:
3316 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3317 break;
3318 case Del:
3319 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3320 break;
3321 case Param:
3322 assert(0);
3323 break;
3324 }
3325 break;
3326 case Subscript_kind:
3327 switch (e->v.Subscript.ctx) {
3328 case AugLoad:
3329 VISIT(c, expr, e->v.Subscript.value);
3330 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3331 break;
3332 case Load:
3333 VISIT(c, expr, e->v.Subscript.value);
3334 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3335 break;
3336 case AugStore:
3337 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3338 break;
3339 case Store:
3340 VISIT(c, expr, e->v.Subscript.value);
3341 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3342 break;
3343 case Del:
3344 VISIT(c, expr, e->v.Subscript.value);
3345 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3346 break;
3347 case Param:
3348 assert(0);
3349 break;
3350 }
3351 break;
3352 case Name_kind:
3353 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3354 /* child nodes of List and Tuple will have expr_context set */
3355 case List_kind:
3356 return compiler_list(c, e);
3357 case Tuple_kind:
3358 return compiler_tuple(c, e);
3359 }
3360 return 1;
3361}
3362
3363static int
3364compiler_augassign(struct compiler *c, stmt_ty s)
3365{
3366 expr_ty e = s->v.AugAssign.target;
3367 expr_ty auge;
3368
3369 assert(s->kind == AugAssign_kind);
3370
3371 switch (e->kind) {
3372 case Attribute_kind:
3373 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3374 AugLoad, e->lineno);
3375 if (auge == NULL)
3376 return 0;
3377 VISIT(c, expr, auge);
3378 VISIT(c, expr, s->v.AugAssign.value);
3379 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3380 auge->v.Attribute.ctx = AugStore;
3381 VISIT(c, expr, auge);
3382 free(auge);
3383 break;
3384 case Subscript_kind:
3385 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3386 AugLoad, e->lineno);
3387 if (auge == NULL)
3388 return 0;
3389 VISIT(c, expr, auge);
3390 VISIT(c, expr, s->v.AugAssign.value);
3391 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3392 auge->v.Subscript.ctx = AugStore;
3393 VISIT(c, expr, auge);
3394 free(auge);
3395 break;
3396 case Name_kind:
3397 VISIT(c, expr, s->v.AugAssign.target);
3398 VISIT(c, expr, s->v.AugAssign.value);
3399 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3400 return compiler_nameop(c, e->v.Name.id, Store);
3401 default:
3402 fprintf(stderr,
3403 "invalid node type for augmented assignment\n");
3404 return 0;
3405 }
3406 return 1;
3407}
3408
3409static int
3410compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3411{
3412 struct fblockinfo *f;
3413 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3414 return 0;
3415 f = &c->u->u_fblock[c->u->u_nfblocks++];
3416 f->fb_type = t;
3417 f->fb_block = b;
3418 return 1;
3419}
3420
3421static void
3422compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3423{
3424 struct compiler_unit *u = c->u;
3425 assert(u->u_nfblocks > 0);
3426 u->u_nfblocks--;
3427 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3428 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3429}
3430
3431/* Raises a SyntaxError and returns 0.
3432 If something goes wrong, a different exception may be raised.
3433*/
3434
3435static int
3436compiler_error(struct compiler *c, const char *errstr)
3437{
3438 PyObject *loc;
3439 PyObject *u = NULL, *v = NULL;
3440
3441 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3442 if (!loc) {
3443 Py_INCREF(Py_None);
3444 loc = Py_None;
3445 }
3446 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3447 Py_None, loc);
3448 if (!u)
3449 goto exit;
3450 v = Py_BuildValue("(zO)", errstr, u);
3451 if (!v)
3452 goto exit;
3453 PyErr_SetObject(PyExc_SyntaxError, v);
3454 exit:
3455 Py_DECREF(loc);
3456 Py_XDECREF(u);
3457 Py_XDECREF(v);
3458 return 0;
3459}
3460
3461static int
3462compiler_handle_subscr(struct compiler *c, const char *kind,
3463 expr_context_ty ctx)
3464{
3465 int op = 0;
3466
3467 /* XXX this code is duplicated */
3468 switch (ctx) {
3469 case AugLoad: /* fall through to Load */
3470 case Load: op = BINARY_SUBSCR; break;
3471 case AugStore:/* fall through to Store */
3472 case Store: op = STORE_SUBSCR; break;
3473 case Del: op = DELETE_SUBSCR; break;
3474 case Param:
3475 fprintf(stderr,
3476 "invalid %s kind %d in subscript\n",
3477 kind, ctx);
3478 return 0;
3479 }
3480 if (ctx == AugLoad) {
3481 ADDOP_I(c, DUP_TOPX, 2);
3482 }
3483 else if (ctx == AugStore) {
3484 ADDOP(c, ROT_THREE);
3485 }
3486 ADDOP(c, op);
3487 return 1;
3488}
3489
3490static int
3491compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3492{
3493 int n = 2;
3494 assert(s->kind == Slice_kind);
3495
3496 /* only handles the cases where BUILD_SLICE is emitted */
3497 if (s->v.Slice.lower) {
3498 VISIT(c, expr, s->v.Slice.lower);
3499 }
3500 else {
3501 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3502 }
3503
3504 if (s->v.Slice.upper) {
3505 VISIT(c, expr, s->v.Slice.upper);
3506 }
3507 else {
3508 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3509 }
3510
3511 if (s->v.Slice.step) {
3512 n++;
3513 VISIT(c, expr, s->v.Slice.step);
3514 }
3515 ADDOP_I(c, BUILD_SLICE, n);
3516 return 1;
3517}
3518
3519static int
3520compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3521{
3522 int op = 0, slice_offset = 0, stack_count = 0;
3523
3524 assert(s->v.Slice.step == NULL);
3525 if (s->v.Slice.lower) {
3526 slice_offset++;
3527 stack_count++;
3528 if (ctx != AugStore)
3529 VISIT(c, expr, s->v.Slice.lower);
3530 }
3531 if (s->v.Slice.upper) {
3532 slice_offset += 2;
3533 stack_count++;
3534 if (ctx != AugStore)
3535 VISIT(c, expr, s->v.Slice.upper);
3536 }
3537
3538 if (ctx == AugLoad) {
3539 switch (stack_count) {
3540 case 0: ADDOP(c, DUP_TOP); break;
3541 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3542 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3543 }
3544 }
3545 else if (ctx == AugStore) {
3546 switch (stack_count) {
3547 case 0: ADDOP(c, ROT_TWO); break;
3548 case 1: ADDOP(c, ROT_THREE); break;
3549 case 2: ADDOP(c, ROT_FOUR); break;
3550 }
3551 }
3552
3553 switch (ctx) {
3554 case AugLoad: /* fall through to Load */
3555 case Load: op = SLICE; break;
3556 case AugStore:/* fall through to Store */
3557 case Store: op = STORE_SLICE; break;
3558 case Del: op = DELETE_SLICE; break;
3559 case Param: /* XXX impossible? */
3560 fprintf(stderr, "param invalid\n");
3561 assert(0);
3562 }
3563
3564 ADDOP(c, op + slice_offset);
3565 return 1;
3566}
3567
3568static int
3569compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3570 expr_context_ty ctx)
3571{
3572 switch (s->kind) {
3573 case Ellipsis_kind:
3574 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3575 break;
3576 case Slice_kind:
3577 return compiler_slice(c, s, ctx);
3578 break;
3579 case Index_kind:
3580 VISIT(c, expr, s->v.Index.value);
3581 break;
3582 case ExtSlice_kind:
3583 assert(0);
3584 break;
3585 }
3586 return 1;
3587}
3588
3589
3590static int
3591compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3592{
3593 switch (s->kind) {
3594 case Ellipsis_kind:
3595 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3596 break;
3597 case Slice_kind:
3598 if (!s->v.Slice.step)
3599 return compiler_simple_slice(c, s, ctx);
3600 if (!compiler_slice(c, s, ctx))
3601 return 0;
3602 if (ctx == AugLoad) {
3603 ADDOP_I(c, DUP_TOPX, 2);
3604 }
3605 else if (ctx == AugStore) {
3606 ADDOP(c, ROT_THREE);
3607 }
3608 return compiler_handle_subscr(c, "slice", ctx);
3609 break;
3610 case ExtSlice_kind: {
3611 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3612 for (i = 0; i < n; i++) {
3613 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3614 if (!compiler_visit_nested_slice(c, sub, ctx))
3615 return 0;
3616 }
3617 ADDOP_I(c, BUILD_TUPLE, n);
3618 return compiler_handle_subscr(c, "extended slice", ctx);
3619 break;
3620 }
3621 case Index_kind:
3622 if (ctx != AugStore)
3623 VISIT(c, expr, s->v.Index.value);
3624 return compiler_handle_subscr(c, "index", ctx);
3625 }
3626 return 1;
3627}
3628
3629/* do depth-first search of basic block graph, starting with block.
3630 post records the block indices in post-order.
3631
3632 XXX must handle implicit jumps from one block to next
3633*/
3634
3635static void
3636dfs(struct compiler *c, basicblock *b, struct assembler *a)
3637{
3638 int i;
3639 struct instr *instr = NULL;
3640
3641 if (b->b_seen)
3642 return;
3643 b->b_seen = 1;
3644 if (b->b_next != NULL)
3645 dfs(c, b->b_next, a);
3646 for (i = 0; i < b->b_iused; i++) {
3647 instr = &b->b_instr[i];
3648 if (instr->i_jrel || instr->i_jabs)
3649 dfs(c, instr->i_target, a);
3650 }
3651 a->a_postorder[a->a_nblocks++] = b;
3652}
3653
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003654static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3656{
3657 int i;
3658 struct instr *instr;
3659 if (b->b_seen || b->b_startdepth >= depth)
3660 return maxdepth;
3661 b->b_seen = 1;
3662 b->b_startdepth = depth;
3663 for (i = 0; i < b->b_iused; i++) {
3664 instr = &b->b_instr[i];
3665 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3666 if (depth > maxdepth)
3667 maxdepth = depth;
3668 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3669 if (instr->i_jrel || instr->i_jabs) {
3670 maxdepth = stackdepth_walk(c, instr->i_target,
3671 depth, maxdepth);
3672 if (instr->i_opcode == JUMP_ABSOLUTE ||
3673 instr->i_opcode == JUMP_FORWARD) {
3674 goto out; /* remaining code is dead */
3675 }
3676 }
3677 }
3678 if (b->b_next)
3679 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3680out:
3681 b->b_seen = 0;
3682 return maxdepth;
3683}
3684
3685/* Find the flow path that needs the largest stack. We assume that
3686 * cycles in the flow graph have no net effect on the stack depth.
3687 */
3688static int
3689stackdepth(struct compiler *c)
3690{
3691 basicblock *b, *entryblock;
3692 entryblock = NULL;
3693 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3694 b->b_seen = 0;
3695 b->b_startdepth = INT_MIN;
3696 entryblock = b;
3697 }
3698 return stackdepth_walk(c, entryblock, 0, 0);
3699}
3700
3701static int
3702assemble_init(struct assembler *a, int nblocks, int firstlineno)
3703{
3704 memset(a, 0, sizeof(struct assembler));
3705 a->a_lineno = firstlineno;
3706 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3707 if (!a->a_bytecode)
3708 return 0;
3709 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3710 if (!a->a_lnotab)
3711 return 0;
3712 a->a_postorder = (basicblock **)PyObject_Malloc(
3713 sizeof(basicblock *) * nblocks);
3714 if (!a->a_postorder)
3715 return 0;
3716 return 1;
3717}
3718
3719static void
3720assemble_free(struct assembler *a)
3721{
3722 Py_XDECREF(a->a_bytecode);
3723 Py_XDECREF(a->a_lnotab);
3724 if (a->a_postorder)
3725 PyObject_Free(a->a_postorder);
3726}
3727
3728/* Return the size of a basic block in bytes. */
3729
3730static int
3731instrsize(struct instr *instr)
3732{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003733 if (!instr->i_hasarg)
3734 return 1;
3735 if (instr->i_oparg > 0xffff)
3736 return 6;
3737 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738}
3739
3740static int
3741blocksize(basicblock *b)
3742{
3743 int i;
3744 int size = 0;
3745
3746 for (i = 0; i < b->b_iused; i++)
3747 size += instrsize(&b->b_instr[i]);
3748 return size;
3749}
3750
3751/* All about a_lnotab.
3752
3753c_lnotab is an array of unsigned bytes disguised as a Python string.
3754It is used to map bytecode offsets to source code line #s (when needed
3755for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003756
Tim Peters2a7f3842001-06-09 09:26:21 +00003757The array is conceptually a list of
3758 (bytecode offset increment, line number increment)
3759pairs. The details are important and delicate, best illustrated by example:
3760
3761 byte code offset source code line number
3762 0 1
3763 6 2
3764 50 7
3765 350 307
3766 361 308
3767
3768The first trick is that these numbers aren't stored, only the increments
3769from one row to the next (this doesn't really work, but it's a start):
3770
3771 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3772
3773The second trick is that an unsigned byte can't hold negative values, or
3774values larger than 255, so (a) there's a deep assumption that byte code
3775offsets and their corresponding line #s both increase monotonically, and (b)
3776if at least one column jumps by more than 255 from one row to the next, more
3777than one pair is written to the table. In case #b, there's no way to know
3778from looking at the table later how many were written. That's the delicate
3779part. A user of c_lnotab desiring to find the source line number
3780corresponding to a bytecode address A should do something like this
3781
3782 lineno = addr = 0
3783 for addr_incr, line_incr in c_lnotab:
3784 addr += addr_incr
3785 if addr > A:
3786 return lineno
3787 lineno += line_incr
3788
3789In order for this to work, when the addr field increments by more than 255,
3790the line # increment in each pair generated must be 0 until the remaining addr
3791increment is < 256. So, in the example above, com_set_lineno should not (as
3792was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3793255, 0, 45, 255, 0, 45.
3794*/
3795
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003796static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003798{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799 int d_bytecode, d_lineno;
3800 int len;
3801 char *lnotab;
3802
3803 d_bytecode = a->a_offset - a->a_lineno_off;
3804 d_lineno = i->i_lineno - a->a_lineno;
3805
3806 assert(d_bytecode >= 0);
3807 assert(d_lineno >= 0);
3808
3809 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003810 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003811
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 if (d_bytecode > 255) {
3813 int i, nbytes, ncodes = d_bytecode / 255;
3814 nbytes = a->a_lnotab_off + 2 * ncodes;
3815 len = PyString_GET_SIZE(a->a_lnotab);
3816 if (nbytes >= len) {
3817 if (len * 2 < nbytes)
3818 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003819 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 len *= 2;
3821 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3822 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3825 for (i = 0; i < ncodes; i++) {
3826 *lnotab++ = 255;
3827 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 d_bytecode -= ncodes * 255;
3830 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 assert(d_bytecode <= 255);
3833 if (d_lineno > 255) {
3834 int i, nbytes, ncodes = d_lineno / 255;
3835 nbytes = a->a_lnotab_off + 2 * ncodes;
3836 len = PyString_GET_SIZE(a->a_lnotab);
3837 if (nbytes >= len) {
3838 if (len * 2 < nbytes)
3839 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003840 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841 len *= 2;
3842 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3843 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003844 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3846 *lnotab++ = 255;
3847 *lnotab++ = d_bytecode;
3848 d_bytecode = 0;
3849 for (i = 1; i < ncodes; i++) {
3850 *lnotab++ = 255;
3851 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003852 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 d_lineno -= ncodes * 255;
3854 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003855 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003856
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 len = PyString_GET_SIZE(a->a_lnotab);
3858 if (a->a_lnotab_off + 2 >= len) {
3859 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003860 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003861 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864 a->a_lnotab_off += 2;
3865 if (d_bytecode) {
3866 *lnotab++ = d_bytecode;
3867 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003868 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003869 else { /* First line of a block; def stmt, etc. */
3870 *lnotab++ = 0;
3871 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003872 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 a->a_lineno = i->i_lineno;
3874 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003875 return 1;
3876}
3877
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878/* assemble_emit()
3879 Extend the bytecode with a new instruction.
3880 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003881*/
3882
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003883static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003885{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003886 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887 int len = PyString_GET_SIZE(a->a_bytecode);
3888 char *code;
3889
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003890 size = instrsize(i);
3891 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003893 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003894 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003896 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003897 if (a->a_offset + size >= len) {
3898 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003899 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003900 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3902 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003903 if (size == 6) {
3904 assert(i->i_hasarg);
3905 *code++ = (char)EXTENDED_ARG;
3906 *code++ = ext & 0xff;
3907 *code++ = ext >> 8;
3908 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003909 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003911 if (i->i_hasarg) {
3912 assert(size == 3 || size == 6);
3913 *code++ = arg & 0xff;
3914 *code++ = arg >> 8;
3915 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003917}
3918
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003919static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003921{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003923 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003924 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003925
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926 /* Compute the size of each block and fixup jump args.
3927 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003928start:
3929 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003931 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 bsize = blocksize(b);
3933 b->b_offset = totsize;
3934 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003935 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003936 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3938 bsize = b->b_offset;
3939 for (i = 0; i < b->b_iused; i++) {
3940 struct instr *instr = &b->b_instr[i];
3941 /* Relative jumps are computed relative to
3942 the instruction pointer after fetching
3943 the jump instruction.
3944 */
3945 bsize += instrsize(instr);
3946 if (instr->i_jabs)
3947 instr->i_oparg = instr->i_target->b_offset;
3948 else if (instr->i_jrel) {
3949 int delta = instr->i_target->b_offset - bsize;
3950 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003951 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003952 else
3953 continue;
3954 if (instr->i_oparg > 0xffff)
3955 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003956 }
3957 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003958
3959 /* XXX: This is an awful hack that could hurt performance, but
3960 on the bright side it should work until we come up
3961 with a better solution.
3962
3963 In the meantime, should the goto be dropped in favor
3964 of a loop?
3965
3966 The issue is that in the first loop blocksize() is called
3967 which calls instrsize() which requires i_oparg be set
3968 appropriately. There is a bootstrap problem because
3969 i_oparg is calculated in the second loop above.
3970
3971 So we loop until we stop seeing new EXTENDED_ARGs.
3972 The only EXTENDED_ARGs that could be popping up are
3973 ones in jump instructions. So this should converge
3974 fairly quickly.
3975 */
3976 if (last_extended_arg_count != extended_arg_count) {
3977 last_extended_arg_count = extended_arg_count;
3978 goto start;
3979 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003980}
3981
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003982static PyObject *
3983dict_keys_inorder(PyObject *dict, int offset)
3984{
3985 PyObject *tuple, *k, *v;
3986 int i, pos = 0, size = PyDict_Size(dict);
3987
3988 tuple = PyTuple_New(size);
3989 if (tuple == NULL)
3990 return NULL;
3991 while (PyDict_Next(dict, &pos, &k, &v)) {
3992 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003994 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003995 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003997 PyTuple_SET_ITEM(tuple, i - offset, k);
3998 }
3999 return tuple;
4000}
4001
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004002static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004004{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 PySTEntryObject *ste = c->u->u_ste;
4006 int flags = 0, n;
4007 if (ste->ste_type != ModuleBlock)
4008 flags |= CO_NEWLOCALS;
4009 if (ste->ste_type == FunctionBlock) {
4010 if (!ste->ste_unoptimized)
4011 flags |= CO_OPTIMIZED;
4012 if (ste->ste_nested)
4013 flags |= CO_NESTED;
4014 if (ste->ste_generator)
4015 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004016 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 if (ste->ste_varargs)
4018 flags |= CO_VARARGS;
4019 if (ste->ste_varkeywords)
4020 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004021 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 flags |= CO_GENERATOR;
4023 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4024 flags |= CO_FUTURE_DIVISION;
4025 n = PyDict_Size(c->u->u_freevars);
4026 if (n < 0)
4027 return -1;
4028 if (n == 0) {
4029 n = PyDict_Size(c->u->u_cellvars);
4030 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004031 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004032 if (n == 0) {
4033 flags |= CO_NOFREE;
4034 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004035 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004036
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004037 return flags;
4038}
4039
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040static PyCodeObject *
4041makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004042{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 PyObject *tmp;
4044 PyCodeObject *co = NULL;
4045 PyObject *consts = NULL;
4046 PyObject *names = NULL;
4047 PyObject *varnames = NULL;
4048 PyObject *filename = NULL;
4049 PyObject *name = NULL;
4050 PyObject *freevars = NULL;
4051 PyObject *cellvars = NULL;
4052 PyObject *bytecode = NULL;
4053 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004055 tmp = dict_keys_inorder(c->u->u_consts, 0);
4056 if (!tmp)
4057 goto error;
4058 consts = PySequence_List(tmp); /* optimize_code requires a list */
4059 Py_DECREF(tmp);
4060
4061 names = dict_keys_inorder(c->u->u_names, 0);
4062 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4063 if (!consts || !names || !varnames)
4064 goto error;
4065
4066 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4067 if (!cellvars)
4068 goto error;
4069 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4070 if (!freevars)
4071 goto error;
4072 filename = PyString_FromString(c->c_filename);
4073 if (!filename)
4074 goto error;
4075
4076 nlocals = PyDict_Size(c->u->u_varnames);
4077 flags = compute_code_flags(c);
4078 if (flags < 0)
4079 goto error;
4080
4081 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4082 if (!bytecode)
4083 goto error;
4084
4085 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4086 if (!tmp)
4087 goto error;
4088 Py_DECREF(consts);
4089 consts = tmp;
4090
4091 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4092 bytecode, consts, names, varnames,
4093 freevars, cellvars,
4094 filename, c->u->u_name,
4095 c->u->u_firstlineno,
4096 a->a_lnotab);
4097 error:
4098 Py_XDECREF(consts);
4099 Py_XDECREF(names);
4100 Py_XDECREF(varnames);
4101 Py_XDECREF(filename);
4102 Py_XDECREF(name);
4103 Py_XDECREF(freevars);
4104 Py_XDECREF(cellvars);
4105 Py_XDECREF(bytecode);
4106 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004107}
4108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109static PyCodeObject *
4110assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004111{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112 basicblock *b, *entryblock;
4113 struct assembler a;
4114 int i, j, nblocks;
4115 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004116
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117 /* Make sure every block that falls off the end returns None.
4118 XXX NEXT_BLOCK() isn't quite right, because if the last
4119 block ends with a jump or return b_next shouldn't set.
4120 */
4121 if (!c->u->u_curblock->b_return) {
4122 NEXT_BLOCK(c);
4123 if (addNone)
4124 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4125 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004126 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004127
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 nblocks = 0;
4129 entryblock = NULL;
4130 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4131 nblocks++;
4132 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004133 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004134
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004135 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4136 goto error;
4137 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004138
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004140 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004141
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142 /* Emit code in reverse postorder from dfs. */
4143 for (i = a.a_nblocks - 1; i >= 0; i--) {
4144 basicblock *b = a.a_postorder[i];
4145 for (j = 0; j < b->b_iused; j++)
4146 if (!assemble_emit(&a, &b->b_instr[j]))
4147 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004148 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004149
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004150 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4151 goto error;
4152 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4153 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004154
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155 co = makecode(c, &a);
4156 error:
4157 assemble_free(&a);
4158 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004159}