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