blob: 4d637ae62af8ad059e84e9cd315e70775e8d5c0e [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));
2387 ADDOP_O(c, LOAD_ATTR, attr, names);
2388 src = dot + 1;
2389 }
2390 }
2391 return compiler_nameop(c, asname, Store);
2392}
2393
2394static int
2395compiler_import(struct compiler *c, stmt_ty s)
2396{
2397 /* The Import node stores a module name like a.b.c as a single
2398 string. This is convenient for all cases except
2399 import a.b.c as d
2400 where we need to parse that string to extract the individual
2401 module names.
2402 XXX Perhaps change the representation to make this case simpler?
2403 */
2404 int i, n = asdl_seq_LEN(s->v.Import.names);
2405 for (i = 0; i < n; i++) {
2406 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2407 int r;
2408
2409 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2410 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2411
2412 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002413 r = compiler_import_as(c, alias->name, alias->asname);
2414 if (!r)
2415 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 }
2417 else {
2418 identifier tmp = alias->name;
2419 const char *base = PyString_AS_STRING(alias->name);
2420 char *dot = strchr(base, '.');
2421 if (dot)
2422 tmp = PyString_FromStringAndSize(base,
2423 dot - base);
2424 r = compiler_nameop(c, tmp, Store);
2425 if (dot) {
2426 Py_DECREF(tmp);
2427 }
2428 if (!r)
2429 return r;
2430 }
2431 }
2432 return 1;
2433}
2434
2435static int
2436compiler_from_import(struct compiler *c, stmt_ty s)
2437{
2438 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2439 int star = 0;
2440
2441 PyObject *names = PyTuple_New(n);
2442 if (!names)
2443 return 0;
2444
2445 /* build up the names */
2446 for (i = 0; i < n; i++) {
2447 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2448 Py_INCREF(alias->name);
2449 PyTuple_SET_ITEM(names, i, alias->name);
2450 }
2451
2452 if (s->lineno > c->c_future->ff_lineno) {
2453 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2454 "__future__")) {
2455 Py_DECREF(names);
2456 return compiler_error(c,
2457 "from __future__ imports must occur "
2458 "at the beginning of the file");
2459
2460 }
2461 }
2462
2463 ADDOP_O(c, LOAD_CONST, names, consts);
2464 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2465 for (i = 0; i < n; i++) {
2466 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2467 identifier store_name;
2468
2469 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2470 assert(n == 1);
2471 ADDOP(c, IMPORT_STAR);
2472 star = 1;
2473 break;
2474 }
2475
2476 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2477 store_name = alias->name;
2478 if (alias->asname)
2479 store_name = alias->asname;
2480
2481 if (!compiler_nameop(c, store_name, Store)) {
2482 Py_DECREF(names);
2483 return 0;
2484 }
2485 }
2486 if (!star)
2487 /* remove imported module */
2488 ADDOP(c, POP_TOP);
2489 return 1;
2490}
2491
2492static int
2493compiler_assert(struct compiler *c, stmt_ty s)
2494{
2495 static PyObject *assertion_error = NULL;
2496 basicblock *end;
2497
2498 if (Py_OptimizeFlag)
2499 return 1;
2500 if (assertion_error == NULL) {
2501 assertion_error = PyString_FromString("AssertionError");
2502 if (assertion_error == NULL)
2503 return 0;
2504 }
2505 VISIT(c, expr, s->v.Assert.test);
2506 end = compiler_new_block(c);
2507 if (end == NULL)
2508 return 0;
2509 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2510 ADDOP(c, POP_TOP);
2511 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2512 if (s->v.Assert.msg) {
2513 VISIT(c, expr, s->v.Assert.msg);
2514 ADDOP_I(c, RAISE_VARARGS, 2);
2515 }
2516 else {
2517 ADDOP_I(c, RAISE_VARARGS, 1);
2518 }
2519 compiler_use_block(c, end);
2520 ADDOP(c, POP_TOP);
2521 return 1;
2522}
2523
2524static int
2525compiler_visit_stmt(struct compiler *c, stmt_ty s)
2526{
2527 int i, n;
2528
2529 c->u->u_lineno = s->lineno;
2530 c->u->u_lineno_set = false;
2531 switch (s->kind) {
2532 case FunctionDef_kind:
2533 return compiler_function(c, s);
2534 case ClassDef_kind:
2535 return compiler_class(c, s);
2536 case Return_kind:
2537 if (c->u->u_ste->ste_type != FunctionBlock)
2538 return compiler_error(c, "'return' outside function");
2539 if (s->v.Return.value) {
2540 if (c->u->u_ste->ste_generator) {
2541 return compiler_error(c,
2542 "'return' with argument inside generator");
2543 }
2544 VISIT(c, expr, s->v.Return.value);
2545 }
2546 else
2547 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2548 ADDOP(c, RETURN_VALUE);
2549 break;
2550 case Delete_kind:
2551 VISIT_SEQ(c, expr, s->v.Delete.targets)
2552 break;
2553 case Assign_kind:
2554 n = asdl_seq_LEN(s->v.Assign.targets);
2555 VISIT(c, expr, s->v.Assign.value);
2556 for (i = 0; i < n; i++) {
2557 if (i < n - 1)
2558 ADDOP(c, DUP_TOP);
2559 VISIT(c, expr,
2560 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2561 }
2562 break;
2563 case AugAssign_kind:
2564 return compiler_augassign(c, s);
2565 case Print_kind:
2566 return compiler_print(c, s);
2567 case For_kind:
2568 return compiler_for(c, s);
2569 case While_kind:
2570 return compiler_while(c, s);
2571 case If_kind:
2572 return compiler_if(c, s);
2573 case Raise_kind:
2574 n = 0;
2575 if (s->v.Raise.type) {
2576 VISIT(c, expr, s->v.Raise.type);
2577 n++;
2578 if (s->v.Raise.inst) {
2579 VISIT(c, expr, s->v.Raise.inst);
2580 n++;
2581 if (s->v.Raise.tback) {
2582 VISIT(c, expr, s->v.Raise.tback);
2583 n++;
2584 }
2585 }
2586 }
2587 ADDOP_I(c, RAISE_VARARGS, n);
2588 break;
2589 case TryExcept_kind:
2590 return compiler_try_except(c, s);
2591 case TryFinally_kind:
2592 return compiler_try_finally(c, s);
2593 case Assert_kind:
2594 return compiler_assert(c, s);
2595 case Import_kind:
2596 return compiler_import(c, s);
2597 case ImportFrom_kind:
2598 return compiler_from_import(c, s);
2599 case Exec_kind:
2600 VISIT(c, expr, s->v.Exec.body);
2601 if (s->v.Exec.globals) {
2602 VISIT(c, expr, s->v.Exec.globals);
2603 if (s->v.Exec.locals) {
2604 VISIT(c, expr, s->v.Exec.locals);
2605 } else {
2606 ADDOP(c, DUP_TOP);
2607 }
2608 } else {
2609 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2610 ADDOP(c, DUP_TOP);
2611 }
2612 ADDOP(c, EXEC_STMT);
2613 break;
2614 case Global_kind:
2615 break;
2616 case Expr_kind:
2617 VISIT(c, expr, s->v.Expr.value);
2618 if (c->c_interactive && c->c_nestlevel <= 1) {
2619 ADDOP(c, PRINT_EXPR);
2620 }
2621 else {
2622 ADDOP(c, POP_TOP);
2623 }
2624 break;
2625 case Pass_kind:
2626 break;
2627 case Break_kind:
2628 if (!c->u->u_nfblocks)
2629 return compiler_error(c, "'break' outside loop");
2630 ADDOP(c, BREAK_LOOP);
2631 break;
2632 case Continue_kind:
2633 return compiler_continue(c);
2634 }
2635 return 1;
2636}
2637
2638static int
2639unaryop(unaryop_ty op)
2640{
2641 switch (op) {
2642 case Invert:
2643 return UNARY_INVERT;
2644 case Not:
2645 return UNARY_NOT;
2646 case UAdd:
2647 return UNARY_POSITIVE;
2648 case USub:
2649 return UNARY_NEGATIVE;
2650 }
2651 return 0;
2652}
2653
2654static int
2655binop(struct compiler *c, operator_ty op)
2656{
2657 switch (op) {
2658 case Add:
2659 return BINARY_ADD;
2660 case Sub:
2661 return BINARY_SUBTRACT;
2662 case Mult:
2663 return BINARY_MULTIPLY;
2664 case Div:
2665 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2666 return BINARY_TRUE_DIVIDE;
2667 else
2668 return BINARY_DIVIDE;
2669 case Mod:
2670 return BINARY_MODULO;
2671 case Pow:
2672 return BINARY_POWER;
2673 case LShift:
2674 return BINARY_LSHIFT;
2675 case RShift:
2676 return BINARY_RSHIFT;
2677 case BitOr:
2678 return BINARY_OR;
2679 case BitXor:
2680 return BINARY_XOR;
2681 case BitAnd:
2682 return BINARY_AND;
2683 case FloorDiv:
2684 return BINARY_FLOOR_DIVIDE;
2685 }
2686 return 0;
2687}
2688
2689static int
2690cmpop(cmpop_ty op)
2691{
2692 switch (op) {
2693 case Eq:
2694 return PyCmp_EQ;
2695 case NotEq:
2696 return PyCmp_NE;
2697 case Lt:
2698 return PyCmp_LT;
2699 case LtE:
2700 return PyCmp_LE;
2701 case Gt:
2702 return PyCmp_GT;
2703 case GtE:
2704 return PyCmp_GE;
2705 case Is:
2706 return PyCmp_IS;
2707 case IsNot:
2708 return PyCmp_IS_NOT;
2709 case In:
2710 return PyCmp_IN;
2711 case NotIn:
2712 return PyCmp_NOT_IN;
2713 }
2714 return PyCmp_BAD;
2715}
2716
2717static int
2718inplace_binop(struct compiler *c, operator_ty op)
2719{
2720 switch (op) {
2721 case Add:
2722 return INPLACE_ADD;
2723 case Sub:
2724 return INPLACE_SUBTRACT;
2725 case Mult:
2726 return INPLACE_MULTIPLY;
2727 case Div:
2728 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2729 return INPLACE_TRUE_DIVIDE;
2730 else
2731 return INPLACE_DIVIDE;
2732 case Mod:
2733 return INPLACE_MODULO;
2734 case Pow:
2735 return INPLACE_POWER;
2736 case LShift:
2737 return INPLACE_LSHIFT;
2738 case RShift:
2739 return INPLACE_RSHIFT;
2740 case BitOr:
2741 return INPLACE_OR;
2742 case BitXor:
2743 return INPLACE_XOR;
2744 case BitAnd:
2745 return INPLACE_AND;
2746 case FloorDiv:
2747 return INPLACE_FLOOR_DIVIDE;
2748 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002749 PyErr_Format(PyExc_SystemError,
2750 "inplace binary op %d should not be possible",
2751 op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 return 0;
2753}
2754
2755static int
2756compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2757{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002758 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2760
2761 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002762 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 /* XXX AugStore isn't used anywhere! */
2764
2765 /* First check for assignment to __debug__. Param? */
2766 if ((ctx == Store || ctx == AugStore || ctx == Del)
2767 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2768 return compiler_error(c, "can not assign to __debug__");
2769 }
2770
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002771 mangled = _Py_Mangle(c->u->u_private, name);
2772 if (!mangled)
2773 return 0;
2774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 op = 0;
2776 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002777 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 switch (scope) {
2779 case FREE:
2780 dict = c->u->u_freevars;
2781 optype = OP_DEREF;
2782 break;
2783 case CELL:
2784 dict = c->u->u_cellvars;
2785 optype = OP_DEREF;
2786 break;
2787 case LOCAL:
2788 if (c->u->u_ste->ste_type == FunctionBlock)
2789 optype = OP_FAST;
2790 break;
2791 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002792 if (c->u->u_ste->ste_type == FunctionBlock &&
2793 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 optype = OP_GLOBAL;
2795 break;
2796 case GLOBAL_EXPLICIT:
2797 optype = OP_GLOBAL;
2798 break;
2799 }
2800
2801 /* XXX Leave assert here, but handle __doc__ and the like better */
2802 assert(scope || PyString_AS_STRING(name)[0] == '_');
2803
2804 switch (optype) {
2805 case OP_DEREF:
2806 switch (ctx) {
2807 case Load: op = LOAD_DEREF; break;
2808 case Store: op = STORE_DEREF; break;
2809 case AugLoad:
2810 case AugStore:
2811 break;
2812 case Del:
2813 PyErr_Format(PyExc_SyntaxError,
2814 "can not delete variable '%s' referenced "
2815 "in nested scope",
2816 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002817 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002820 PyErr_SetString(PyExc_SystemError,
2821 "param invalid for deref variable");
2822 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 }
2824 break;
2825 case OP_FAST:
2826 switch (ctx) {
2827 case Load: op = LOAD_FAST; break;
2828 case Store: op = STORE_FAST; break;
2829 case Del: op = DELETE_FAST; break;
2830 case AugLoad:
2831 case AugStore:
2832 break;
2833 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002834 PyErr_SetString(PyExc_SystemError,
2835 "param invalid for local variable");
2836 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002838 ADDOP_O(c, op, mangled, varnames);
2839 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 return 1;
2841 case OP_GLOBAL:
2842 switch (ctx) {
2843 case Load: op = LOAD_GLOBAL; break;
2844 case Store: op = STORE_GLOBAL; break;
2845 case Del: op = DELETE_GLOBAL; break;
2846 case AugLoad:
2847 case AugStore:
2848 break;
2849 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002850 PyErr_SetString(PyExc_SystemError,
2851 "param invalid for global variable");
2852 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 }
2854 break;
2855 case OP_NAME:
2856 switch (ctx) {
2857 case Load: op = LOAD_NAME; break;
2858 case Store: op = STORE_NAME; break;
2859 case Del: op = DELETE_NAME; break;
2860 case AugLoad:
2861 case AugStore:
2862 break;
2863 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002864 PyErr_SetString(PyExc_SystemError,
2865 "param invalid for name variable");
2866 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 }
2868 break;
2869 }
2870
2871 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002872 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002873 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002874 if (arg < 0)
2875 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002876 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877}
2878
2879static int
2880compiler_boolop(struct compiler *c, expr_ty e)
2881{
2882 basicblock *end;
2883 int jumpi, i, n;
2884 asdl_seq *s;
2885
2886 assert(e->kind == BoolOp_kind);
2887 if (e->v.BoolOp.op == And)
2888 jumpi = JUMP_IF_FALSE;
2889 else
2890 jumpi = JUMP_IF_TRUE;
2891 end = compiler_new_block(c);
2892 if (end < 0)
2893 return 0;
2894 s = e->v.BoolOp.values;
2895 n = asdl_seq_LEN(s) - 1;
2896 for (i = 0; i < n; ++i) {
2897 VISIT(c, expr, asdl_seq_GET(s, i));
2898 ADDOP_JREL(c, jumpi, end);
2899 ADDOP(c, POP_TOP)
2900 }
2901 VISIT(c, expr, asdl_seq_GET(s, n));
2902 compiler_use_next_block(c, end);
2903 return 1;
2904}
2905
2906static int
2907compiler_list(struct compiler *c, expr_ty e)
2908{
2909 int n = asdl_seq_LEN(e->v.List.elts);
2910 if (e->v.List.ctx == Store) {
2911 ADDOP_I(c, UNPACK_SEQUENCE, n);
2912 }
2913 VISIT_SEQ(c, expr, e->v.List.elts);
2914 if (e->v.List.ctx == Load) {
2915 ADDOP_I(c, BUILD_LIST, n);
2916 }
2917 return 1;
2918}
2919
2920static int
2921compiler_tuple(struct compiler *c, expr_ty e)
2922{
2923 int n = asdl_seq_LEN(e->v.Tuple.elts);
2924 if (e->v.Tuple.ctx == Store) {
2925 ADDOP_I(c, UNPACK_SEQUENCE, n);
2926 }
2927 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2928 if (e->v.Tuple.ctx == Load) {
2929 ADDOP_I(c, BUILD_TUPLE, n);
2930 }
2931 return 1;
2932}
2933
2934static int
2935compiler_compare(struct compiler *c, expr_ty e)
2936{
2937 int i, n;
2938 basicblock *cleanup = NULL;
2939
2940 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2941 VISIT(c, expr, e->v.Compare.left);
2942 n = asdl_seq_LEN(e->v.Compare.ops);
2943 assert(n > 0);
2944 if (n > 1) {
2945 cleanup = compiler_new_block(c);
2946 if (cleanup == NULL)
2947 return 0;
2948 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2949 }
2950 for (i = 1; i < n; i++) {
2951 ADDOP(c, DUP_TOP);
2952 ADDOP(c, ROT_THREE);
2953 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2954 ADDOP_I(c, COMPARE_OP,
2955 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2956 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2957 NEXT_BLOCK(c);
2958 ADDOP(c, POP_TOP);
2959 if (i < (n - 1))
2960 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2961 }
2962 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2963 ADDOP_I(c, COMPARE_OP,
2964 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2965 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2966 if (n > 1) {
2967 basicblock *end = compiler_new_block(c);
2968 if (end == NULL)
2969 return 0;
2970 ADDOP_JREL(c, JUMP_FORWARD, end);
2971 compiler_use_next_block(c, cleanup);
2972 ADDOP(c, ROT_TWO);
2973 ADDOP(c, POP_TOP);
2974 compiler_use_next_block(c, end);
2975 }
2976 return 1;
2977}
2978
2979static int
2980compiler_call(struct compiler *c, expr_ty e)
2981{
2982 int n, code = 0;
2983
2984 VISIT(c, expr, e->v.Call.func);
2985 n = asdl_seq_LEN(e->v.Call.args);
2986 VISIT_SEQ(c, expr, e->v.Call.args);
2987 if (e->v.Call.keywords) {
2988 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2989 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2990 }
2991 if (e->v.Call.starargs) {
2992 VISIT(c, expr, e->v.Call.starargs);
2993 code |= 1;
2994 }
2995 if (e->v.Call.kwargs) {
2996 VISIT(c, expr, e->v.Call.kwargs);
2997 code |= 2;
2998 }
2999 switch (code) {
3000 case 0:
3001 ADDOP_I(c, CALL_FUNCTION, n);
3002 break;
3003 case 1:
3004 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3005 break;
3006 case 2:
3007 ADDOP_I(c, CALL_FUNCTION_KW, n);
3008 break;
3009 case 3:
3010 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3011 break;
3012 }
3013 return 1;
3014}
3015
3016static int
3017compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3018 asdl_seq *generators, int gen_index,
3019 expr_ty elt)
3020{
3021 /* generate code for the iterator, then each of the ifs,
3022 and then write to the element */
3023
3024 comprehension_ty l;
3025 basicblock *start, *anchor, *skip, *if_cleanup;
3026 int i, n;
3027
3028 start = compiler_new_block(c);
3029 skip = compiler_new_block(c);
3030 if_cleanup = compiler_new_block(c);
3031 anchor = compiler_new_block(c);
3032
3033 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3034 anchor == NULL)
3035 return 0;
3036
3037 l = asdl_seq_GET(generators, gen_index);
3038 VISIT(c, expr, l->iter);
3039 ADDOP(c, GET_ITER);
3040 compiler_use_next_block(c, start);
3041 ADDOP_JREL(c, FOR_ITER, anchor);
3042 NEXT_BLOCK(c);
3043 VISIT(c, expr, l->target);
3044
3045 /* XXX this needs to be cleaned up...a lot! */
3046 n = asdl_seq_LEN(l->ifs);
3047 for (i = 0; i < n; i++) {
3048 expr_ty e = asdl_seq_GET(l->ifs, i);
3049 VISIT(c, expr, e);
3050 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3051 NEXT_BLOCK(c);
3052 ADDOP(c, POP_TOP);
3053 }
3054
3055 if (++gen_index < asdl_seq_LEN(generators))
3056 if (!compiler_listcomp_generator(c, tmpname,
3057 generators, gen_index, elt))
3058 return 0;
3059
3060 /* only append after the last for generator */
3061 if (gen_index >= asdl_seq_LEN(generators)) {
3062 if (!compiler_nameop(c, tmpname, Load))
3063 return 0;
3064 VISIT(c, expr, elt);
3065 ADDOP_I(c, CALL_FUNCTION, 1);
3066 ADDOP(c, POP_TOP);
3067
3068 compiler_use_next_block(c, skip);
3069 }
3070 for (i = 0; i < n; i++) {
3071 ADDOP_I(c, JUMP_FORWARD, 1);
3072 if (i == 0)
3073 compiler_use_next_block(c, if_cleanup);
3074 ADDOP(c, POP_TOP);
3075 }
3076 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3077 compiler_use_next_block(c, anchor);
3078 /* delete the append method added to locals */
3079 if (gen_index == 1)
3080 if (!compiler_nameop(c, tmpname, Del))
3081 return 0;
3082
3083 return 1;
3084}
3085
3086static int
3087compiler_listcomp(struct compiler *c, expr_ty e)
3088{
3089 char tmpname[256];
3090 identifier tmp;
3091 int rc = 0;
3092 static identifier append;
3093 asdl_seq *generators = e->v.ListComp.generators;
3094
3095 assert(e->kind == ListComp_kind);
3096 if (!append) {
3097 append = PyString_InternFromString("append");
3098 if (!append)
3099 return 0;
3100 }
3101 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3102 tmp = PyString_FromString(tmpname);
3103 if (!tmp)
3104 return 0;
3105 ADDOP_I(c, BUILD_LIST, 0);
3106 ADDOP(c, DUP_TOP);
3107 ADDOP_O(c, LOAD_ATTR, append, names);
3108 if (compiler_nameop(c, tmp, Store))
3109 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3110 e->v.ListComp.elt);
3111 Py_DECREF(tmp);
3112 return rc;
3113}
3114
3115static int
3116compiler_genexp_generator(struct compiler *c,
3117 asdl_seq *generators, int gen_index,
3118 expr_ty elt)
3119{
3120 /* generate code for the iterator, then each of the ifs,
3121 and then write to the element */
3122
3123 comprehension_ty ge;
3124 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3125 int i, n;
3126
3127 start = compiler_new_block(c);
3128 skip = compiler_new_block(c);
3129 if_cleanup = compiler_new_block(c);
3130 anchor = compiler_new_block(c);
3131 end = compiler_new_block(c);
3132
3133 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3134 anchor == NULL || end == NULL)
3135 return 0;
3136
3137 ge = asdl_seq_GET(generators, gen_index);
3138 ADDOP_JREL(c, SETUP_LOOP, end);
3139 if (!compiler_push_fblock(c, LOOP, start))
3140 return 0;
3141
3142 if (gen_index == 0) {
3143 /* Receive outermost iter as an implicit argument */
3144 c->u->u_argcount = 1;
3145 ADDOP_I(c, LOAD_FAST, 0);
3146 }
3147 else {
3148 /* Sub-iter - calculate on the fly */
3149 VISIT(c, expr, ge->iter);
3150 ADDOP(c, GET_ITER);
3151 }
3152 compiler_use_next_block(c, start);
3153 ADDOP_JREL(c, FOR_ITER, anchor);
3154 NEXT_BLOCK(c);
3155 VISIT(c, expr, ge->target);
3156
3157 /* XXX this needs to be cleaned up...a lot! */
3158 n = asdl_seq_LEN(ge->ifs);
3159 for (i = 0; i < n; i++) {
3160 expr_ty e = asdl_seq_GET(ge->ifs, i);
3161 VISIT(c, expr, e);
3162 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3163 NEXT_BLOCK(c);
3164 ADDOP(c, POP_TOP);
3165 }
3166
3167 if (++gen_index < asdl_seq_LEN(generators))
3168 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3169 return 0;
3170
3171 /* only append after the last 'for' generator */
3172 if (gen_index >= asdl_seq_LEN(generators)) {
3173 VISIT(c, expr, elt);
3174 ADDOP(c, YIELD_VALUE);
3175 ADDOP(c, POP_TOP);
3176
3177 compiler_use_next_block(c, skip);
3178 }
3179 for (i = 0; i < n; i++) {
3180 ADDOP_I(c, JUMP_FORWARD, 1);
3181 if (i == 0)
3182 compiler_use_next_block(c, if_cleanup);
3183
3184 ADDOP(c, POP_TOP);
3185 }
3186 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3187 compiler_use_next_block(c, anchor);
3188 ADDOP(c, POP_BLOCK);
3189 compiler_pop_fblock(c, LOOP, start);
3190 compiler_use_next_block(c, end);
3191
3192 return 1;
3193}
3194
3195static int
3196compiler_genexp(struct compiler *c, expr_ty e)
3197{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003198 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 PyCodeObject *co;
3200 expr_ty outermost_iter = ((comprehension_ty)
3201 (asdl_seq_GET(e->v.GeneratorExp.generators,
3202 0)))->iter;
3203
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003204 if (!name) {
3205 name = PyString_FromString("<genexpr>");
3206 if (!name)
3207 return 0;
3208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209
3210 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3211 return 0;
3212 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3213 e->v.GeneratorExp.elt);
3214 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003215 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 if (co == NULL)
3217 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218
3219 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003220 Py_DECREF(co);
3221
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 VISIT(c, expr, outermost_iter);
3223 ADDOP(c, GET_ITER);
3224 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225
3226 return 1;
3227}
3228
3229static int
3230compiler_visit_keyword(struct compiler *c, keyword_ty k)
3231{
3232 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3233 VISIT(c, expr, k->value);
3234 return 1;
3235}
3236
3237/* Test whether expression is constant. For constants, report
3238 whether they are true or false.
3239
3240 Return values: 1 for true, 0 for false, -1 for non-constant.
3241 */
3242
3243static int
3244expr_constant(expr_ty e)
3245{
3246 switch (e->kind) {
3247 case Num_kind:
3248 return PyObject_IsTrue(e->v.Num.n);
3249 case Str_kind:
3250 return PyObject_IsTrue(e->v.Str.s);
3251 default:
3252 return -1;
3253 }
3254}
3255
3256static int
3257compiler_visit_expr(struct compiler *c, expr_ty e)
3258{
3259 int i, n;
3260
3261 if (e->lineno > c->u->u_lineno) {
3262 c->u->u_lineno = e->lineno;
3263 c->u->u_lineno_set = false;
3264 }
3265 switch (e->kind) {
3266 case BoolOp_kind:
3267 return compiler_boolop(c, e);
3268 case BinOp_kind:
3269 VISIT(c, expr, e->v.BinOp.left);
3270 VISIT(c, expr, e->v.BinOp.right);
3271 ADDOP(c, binop(c, e->v.BinOp.op));
3272 break;
3273 case UnaryOp_kind:
3274 VISIT(c, expr, e->v.UnaryOp.operand);
3275 ADDOP(c, unaryop(e->v.UnaryOp.op));
3276 break;
3277 case Lambda_kind:
3278 return compiler_lambda(c, e);
3279 case Dict_kind:
3280 /* XXX get rid of arg? */
3281 ADDOP_I(c, BUILD_MAP, 0);
3282 n = asdl_seq_LEN(e->v.Dict.values);
3283 /* We must arrange things just right for STORE_SUBSCR.
3284 It wants the stack to look like (value) (dict) (key) */
3285 for (i = 0; i < n; i++) {
3286 ADDOP(c, DUP_TOP);
3287 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3288 ADDOP(c, ROT_TWO);
3289 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3290 ADDOP(c, STORE_SUBSCR);
3291 }
3292 break;
3293 case ListComp_kind:
3294 return compiler_listcomp(c, e);
3295 case GeneratorExp_kind:
3296 return compiler_genexp(c, e);
3297 case Yield_kind:
3298 if (c->u->u_ste->ste_type != FunctionBlock)
3299 return compiler_error(c, "'yield' outside function");
3300 /*
3301 for (i = 0; i < c->u->u_nfblocks; i++) {
3302 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3303 return compiler_error(
3304 c, "'yield' not allowed in a 'try' "
3305 "block with a 'finally' clause");
3306 }
3307 */
3308 if (e->v.Yield.value) {
3309 VISIT(c, expr, e->v.Yield.value);
3310 }
3311 else {
3312 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3313 }
3314 ADDOP(c, YIELD_VALUE);
3315 break;
3316 case Compare_kind:
3317 return compiler_compare(c, e);
3318 case Call_kind:
3319 return compiler_call(c, e);
3320 case Repr_kind:
3321 VISIT(c, expr, e->v.Repr.value);
3322 ADDOP(c, UNARY_CONVERT);
3323 break;
3324 case Num_kind:
3325 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3326 break;
3327 case Str_kind:
3328 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3329 break;
3330 /* The following exprs can be assignment targets. */
3331 case Attribute_kind:
3332 if (e->v.Attribute.ctx != AugStore)
3333 VISIT(c, expr, e->v.Attribute.value);
3334 switch (e->v.Attribute.ctx) {
3335 case AugLoad:
3336 ADDOP(c, DUP_TOP);
3337 /* Fall through to load */
3338 case Load:
3339 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3340 break;
3341 case AugStore:
3342 ADDOP(c, ROT_TWO);
3343 /* Fall through to save */
3344 case Store:
3345 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3346 break;
3347 case Del:
3348 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3349 break;
3350 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003351 PyErr_SetString(PyExc_SystemError,
3352 "param invalid in attribute expression");
3353 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 }
3355 break;
3356 case Subscript_kind:
3357 switch (e->v.Subscript.ctx) {
3358 case AugLoad:
3359 VISIT(c, expr, e->v.Subscript.value);
3360 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3361 break;
3362 case Load:
3363 VISIT(c, expr, e->v.Subscript.value);
3364 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3365 break;
3366 case AugStore:
3367 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3368 break;
3369 case Store:
3370 VISIT(c, expr, e->v.Subscript.value);
3371 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3372 break;
3373 case Del:
3374 VISIT(c, expr, e->v.Subscript.value);
3375 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3376 break;
3377 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003378 PyErr_SetString(PyExc_SystemError,
3379 "param invalid in subscript expression");
3380 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 }
3382 break;
3383 case Name_kind:
3384 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3385 /* child nodes of List and Tuple will have expr_context set */
3386 case List_kind:
3387 return compiler_list(c, e);
3388 case Tuple_kind:
3389 return compiler_tuple(c, e);
3390 }
3391 return 1;
3392}
3393
3394static int
3395compiler_augassign(struct compiler *c, stmt_ty s)
3396{
3397 expr_ty e = s->v.AugAssign.target;
3398 expr_ty auge;
3399
3400 assert(s->kind == AugAssign_kind);
3401
3402 switch (e->kind) {
3403 case Attribute_kind:
3404 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3405 AugLoad, e->lineno);
3406 if (auge == NULL)
3407 return 0;
3408 VISIT(c, expr, auge);
3409 VISIT(c, expr, s->v.AugAssign.value);
3410 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3411 auge->v.Attribute.ctx = AugStore;
3412 VISIT(c, expr, auge);
3413 free(auge);
3414 break;
3415 case Subscript_kind:
3416 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3417 AugLoad, e->lineno);
3418 if (auge == NULL)
3419 return 0;
3420 VISIT(c, expr, auge);
3421 VISIT(c, expr, s->v.AugAssign.value);
3422 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3423 auge->v.Subscript.ctx = AugStore;
3424 VISIT(c, expr, auge);
3425 free(auge);
3426 break;
3427 case Name_kind:
3428 VISIT(c, expr, s->v.AugAssign.target);
3429 VISIT(c, expr, s->v.AugAssign.value);
3430 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3431 return compiler_nameop(c, e->v.Name.id, Store);
3432 default:
3433 fprintf(stderr,
3434 "invalid node type for augmented assignment\n");
3435 return 0;
3436 }
3437 return 1;
3438}
3439
3440static int
3441compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3442{
3443 struct fblockinfo *f;
3444 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3445 return 0;
3446 f = &c->u->u_fblock[c->u->u_nfblocks++];
3447 f->fb_type = t;
3448 f->fb_block = b;
3449 return 1;
3450}
3451
3452static void
3453compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3454{
3455 struct compiler_unit *u = c->u;
3456 assert(u->u_nfblocks > 0);
3457 u->u_nfblocks--;
3458 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3459 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3460}
3461
3462/* Raises a SyntaxError and returns 0.
3463 If something goes wrong, a different exception may be raised.
3464*/
3465
3466static int
3467compiler_error(struct compiler *c, const char *errstr)
3468{
3469 PyObject *loc;
3470 PyObject *u = NULL, *v = NULL;
3471
3472 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3473 if (!loc) {
3474 Py_INCREF(Py_None);
3475 loc = Py_None;
3476 }
3477 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3478 Py_None, loc);
3479 if (!u)
3480 goto exit;
3481 v = Py_BuildValue("(zO)", errstr, u);
3482 if (!v)
3483 goto exit;
3484 PyErr_SetObject(PyExc_SyntaxError, v);
3485 exit:
3486 Py_DECREF(loc);
3487 Py_XDECREF(u);
3488 Py_XDECREF(v);
3489 return 0;
3490}
3491
3492static int
3493compiler_handle_subscr(struct compiler *c, const char *kind,
3494 expr_context_ty ctx)
3495{
3496 int op = 0;
3497
3498 /* XXX this code is duplicated */
3499 switch (ctx) {
3500 case AugLoad: /* fall through to Load */
3501 case Load: op = BINARY_SUBSCR; break;
3502 case AugStore:/* fall through to Store */
3503 case Store: op = STORE_SUBSCR; break;
3504 case Del: op = DELETE_SUBSCR; break;
3505 case Param:
3506 fprintf(stderr,
3507 "invalid %s kind %d in subscript\n",
3508 kind, ctx);
3509 return 0;
3510 }
3511 if (ctx == AugLoad) {
3512 ADDOP_I(c, DUP_TOPX, 2);
3513 }
3514 else if (ctx == AugStore) {
3515 ADDOP(c, ROT_THREE);
3516 }
3517 ADDOP(c, op);
3518 return 1;
3519}
3520
3521static int
3522compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3523{
3524 int n = 2;
3525 assert(s->kind == Slice_kind);
3526
3527 /* only handles the cases where BUILD_SLICE is emitted */
3528 if (s->v.Slice.lower) {
3529 VISIT(c, expr, s->v.Slice.lower);
3530 }
3531 else {
3532 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3533 }
3534
3535 if (s->v.Slice.upper) {
3536 VISIT(c, expr, s->v.Slice.upper);
3537 }
3538 else {
3539 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3540 }
3541
3542 if (s->v.Slice.step) {
3543 n++;
3544 VISIT(c, expr, s->v.Slice.step);
3545 }
3546 ADDOP_I(c, BUILD_SLICE, n);
3547 return 1;
3548}
3549
3550static int
3551compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3552{
3553 int op = 0, slice_offset = 0, stack_count = 0;
3554
3555 assert(s->v.Slice.step == NULL);
3556 if (s->v.Slice.lower) {
3557 slice_offset++;
3558 stack_count++;
3559 if (ctx != AugStore)
3560 VISIT(c, expr, s->v.Slice.lower);
3561 }
3562 if (s->v.Slice.upper) {
3563 slice_offset += 2;
3564 stack_count++;
3565 if (ctx != AugStore)
3566 VISIT(c, expr, s->v.Slice.upper);
3567 }
3568
3569 if (ctx == AugLoad) {
3570 switch (stack_count) {
3571 case 0: ADDOP(c, DUP_TOP); break;
3572 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3573 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3574 }
3575 }
3576 else if (ctx == AugStore) {
3577 switch (stack_count) {
3578 case 0: ADDOP(c, ROT_TWO); break;
3579 case 1: ADDOP(c, ROT_THREE); break;
3580 case 2: ADDOP(c, ROT_FOUR); break;
3581 }
3582 }
3583
3584 switch (ctx) {
3585 case AugLoad: /* fall through to Load */
3586 case Load: op = SLICE; break;
3587 case AugStore:/* fall through to Store */
3588 case Store: op = STORE_SLICE; break;
3589 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003590 case Param:
3591 PyErr_SetString(PyExc_SystemError,
3592 "param invalid in simple slice");
3593 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 }
3595
3596 ADDOP(c, op + slice_offset);
3597 return 1;
3598}
3599
3600static int
3601compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3602 expr_context_ty ctx)
3603{
3604 switch (s->kind) {
3605 case Ellipsis_kind:
3606 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3607 break;
3608 case Slice_kind:
3609 return compiler_slice(c, s, ctx);
3610 break;
3611 case Index_kind:
3612 VISIT(c, expr, s->v.Index.value);
3613 break;
3614 case ExtSlice_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00003615 PyErr_SetString(PyExc_SystemError,
3616 "extended slice invalid in nested slice");
3617 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 }
3619 return 1;
3620}
3621
3622
3623static int
3624compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3625{
3626 switch (s->kind) {
3627 case Ellipsis_kind:
3628 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3629 break;
3630 case Slice_kind:
3631 if (!s->v.Slice.step)
3632 return compiler_simple_slice(c, s, ctx);
3633 if (!compiler_slice(c, s, ctx))
3634 return 0;
3635 if (ctx == AugLoad) {
3636 ADDOP_I(c, DUP_TOPX, 2);
3637 }
3638 else if (ctx == AugStore) {
3639 ADDOP(c, ROT_THREE);
3640 }
3641 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003642 case ExtSlice_kind: {
3643 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3644 for (i = 0; i < n; i++) {
3645 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3646 if (!compiler_visit_nested_slice(c, sub, ctx))
3647 return 0;
3648 }
3649 ADDOP_I(c, BUILD_TUPLE, n);
3650 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 }
3652 case Index_kind:
3653 if (ctx != AugStore)
3654 VISIT(c, expr, s->v.Index.value);
3655 return compiler_handle_subscr(c, "index", ctx);
3656 }
3657 return 1;
3658}
3659
3660/* do depth-first search of basic block graph, starting with block.
3661 post records the block indices in post-order.
3662
3663 XXX must handle implicit jumps from one block to next
3664*/
3665
3666static void
3667dfs(struct compiler *c, basicblock *b, struct assembler *a)
3668{
3669 int i;
3670 struct instr *instr = NULL;
3671
3672 if (b->b_seen)
3673 return;
3674 b->b_seen = 1;
3675 if (b->b_next != NULL)
3676 dfs(c, b->b_next, a);
3677 for (i = 0; i < b->b_iused; i++) {
3678 instr = &b->b_instr[i];
3679 if (instr->i_jrel || instr->i_jabs)
3680 dfs(c, instr->i_target, a);
3681 }
3682 a->a_postorder[a->a_nblocks++] = b;
3683}
3684
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003685static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3687{
3688 int i;
3689 struct instr *instr;
3690 if (b->b_seen || b->b_startdepth >= depth)
3691 return maxdepth;
3692 b->b_seen = 1;
3693 b->b_startdepth = depth;
3694 for (i = 0; i < b->b_iused; i++) {
3695 instr = &b->b_instr[i];
3696 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3697 if (depth > maxdepth)
3698 maxdepth = depth;
3699 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3700 if (instr->i_jrel || instr->i_jabs) {
3701 maxdepth = stackdepth_walk(c, instr->i_target,
3702 depth, maxdepth);
3703 if (instr->i_opcode == JUMP_ABSOLUTE ||
3704 instr->i_opcode == JUMP_FORWARD) {
3705 goto out; /* remaining code is dead */
3706 }
3707 }
3708 }
3709 if (b->b_next)
3710 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3711out:
3712 b->b_seen = 0;
3713 return maxdepth;
3714}
3715
3716/* Find the flow path that needs the largest stack. We assume that
3717 * cycles in the flow graph have no net effect on the stack depth.
3718 */
3719static int
3720stackdepth(struct compiler *c)
3721{
3722 basicblock *b, *entryblock;
3723 entryblock = NULL;
3724 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3725 b->b_seen = 0;
3726 b->b_startdepth = INT_MIN;
3727 entryblock = b;
3728 }
3729 return stackdepth_walk(c, entryblock, 0, 0);
3730}
3731
3732static int
3733assemble_init(struct assembler *a, int nblocks, int firstlineno)
3734{
3735 memset(a, 0, sizeof(struct assembler));
3736 a->a_lineno = firstlineno;
3737 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3738 if (!a->a_bytecode)
3739 return 0;
3740 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3741 if (!a->a_lnotab)
3742 return 0;
3743 a->a_postorder = (basicblock **)PyObject_Malloc(
3744 sizeof(basicblock *) * nblocks);
3745 if (!a->a_postorder)
3746 return 0;
3747 return 1;
3748}
3749
3750static void
3751assemble_free(struct assembler *a)
3752{
3753 Py_XDECREF(a->a_bytecode);
3754 Py_XDECREF(a->a_lnotab);
3755 if (a->a_postorder)
3756 PyObject_Free(a->a_postorder);
3757}
3758
3759/* Return the size of a basic block in bytes. */
3760
3761static int
3762instrsize(struct instr *instr)
3763{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003764 if (!instr->i_hasarg)
3765 return 1;
3766 if (instr->i_oparg > 0xffff)
3767 return 6;
3768 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769}
3770
3771static int
3772blocksize(basicblock *b)
3773{
3774 int i;
3775 int size = 0;
3776
3777 for (i = 0; i < b->b_iused; i++)
3778 size += instrsize(&b->b_instr[i]);
3779 return size;
3780}
3781
3782/* All about a_lnotab.
3783
3784c_lnotab is an array of unsigned bytes disguised as a Python string.
3785It is used to map bytecode offsets to source code line #s (when needed
3786for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003787
Tim Peters2a7f3842001-06-09 09:26:21 +00003788The array is conceptually a list of
3789 (bytecode offset increment, line number increment)
3790pairs. The details are important and delicate, best illustrated by example:
3791
3792 byte code offset source code line number
3793 0 1
3794 6 2
3795 50 7
3796 350 307
3797 361 308
3798
3799The first trick is that these numbers aren't stored, only the increments
3800from one row to the next (this doesn't really work, but it's a start):
3801
3802 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3803
3804The second trick is that an unsigned byte can't hold negative values, or
3805values larger than 255, so (a) there's a deep assumption that byte code
3806offsets and their corresponding line #s both increase monotonically, and (b)
3807if at least one column jumps by more than 255 from one row to the next, more
3808than one pair is written to the table. In case #b, there's no way to know
3809from looking at the table later how many were written. That's the delicate
3810part. A user of c_lnotab desiring to find the source line number
3811corresponding to a bytecode address A should do something like this
3812
3813 lineno = addr = 0
3814 for addr_incr, line_incr in c_lnotab:
3815 addr += addr_incr
3816 if addr > A:
3817 return lineno
3818 lineno += line_incr
3819
3820In order for this to work, when the addr field increments by more than 255,
3821the line # increment in each pair generated must be 0 until the remaining addr
3822increment is < 256. So, in the example above, com_set_lineno should not (as
3823was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3824255, 0, 45, 255, 0, 45.
3825*/
3826
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003827static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003829{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830 int d_bytecode, d_lineno;
3831 int len;
3832 char *lnotab;
3833
3834 d_bytecode = a->a_offset - a->a_lineno_off;
3835 d_lineno = i->i_lineno - a->a_lineno;
3836
3837 assert(d_bytecode >= 0);
3838 assert(d_lineno >= 0);
3839
3840 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003841 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003842
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 if (d_bytecode > 255) {
3844 int i, nbytes, ncodes = d_bytecode / 255;
3845 nbytes = a->a_lnotab_off + 2 * ncodes;
3846 len = PyString_GET_SIZE(a->a_lnotab);
3847 if (nbytes >= len) {
3848 if (len * 2 < nbytes)
3849 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003850 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 len *= 2;
3852 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3853 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003854 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3856 for (i = 0; i < ncodes; i++) {
3857 *lnotab++ = 255;
3858 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 d_bytecode -= ncodes * 255;
3861 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003862 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 assert(d_bytecode <= 255);
3864 if (d_lineno > 255) {
3865 int i, nbytes, ncodes = d_lineno / 255;
3866 nbytes = a->a_lnotab_off + 2 * ncodes;
3867 len = PyString_GET_SIZE(a->a_lnotab);
3868 if (nbytes >= len) {
3869 if (len * 2 < nbytes)
3870 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003871 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872 len *= 2;
3873 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3874 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003875 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3877 *lnotab++ = 255;
3878 *lnotab++ = d_bytecode;
3879 d_bytecode = 0;
3880 for (i = 1; i < ncodes; i++) {
3881 *lnotab++ = 255;
3882 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003883 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 d_lineno -= ncodes * 255;
3885 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003886 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003887
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888 len = PyString_GET_SIZE(a->a_lnotab);
3889 if (a->a_lnotab_off + 2 >= len) {
3890 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003891 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895 a->a_lnotab_off += 2;
3896 if (d_bytecode) {
3897 *lnotab++ = d_bytecode;
3898 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003899 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 else { /* First line of a block; def stmt, etc. */
3901 *lnotab++ = 0;
3902 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003903 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904 a->a_lineno = i->i_lineno;
3905 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003906 return 1;
3907}
3908
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909/* assemble_emit()
3910 Extend the bytecode with a new instruction.
3911 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003912*/
3913
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003914static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003916{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003917 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 int len = PyString_GET_SIZE(a->a_bytecode);
3919 char *code;
3920
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003921 size = instrsize(i);
3922 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003924 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003927 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 if (a->a_offset + size >= len) {
3929 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003930 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003931 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3933 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003934 if (size == 6) {
3935 assert(i->i_hasarg);
3936 *code++ = (char)EXTENDED_ARG;
3937 *code++ = ext & 0xff;
3938 *code++ = ext >> 8;
3939 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003942 if (i->i_hasarg) {
3943 assert(size == 3 || size == 6);
3944 *code++ = arg & 0xff;
3945 *code++ = arg >> 8;
3946 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003948}
3949
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003950static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003952{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003954 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003955 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957 /* Compute the size of each block and fixup jump args.
3958 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003959start:
3960 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003962 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 bsize = blocksize(b);
3964 b->b_offset = totsize;
3965 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003966 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003967 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3969 bsize = b->b_offset;
3970 for (i = 0; i < b->b_iused; i++) {
3971 struct instr *instr = &b->b_instr[i];
3972 /* Relative jumps are computed relative to
3973 the instruction pointer after fetching
3974 the jump instruction.
3975 */
3976 bsize += instrsize(instr);
3977 if (instr->i_jabs)
3978 instr->i_oparg = instr->i_target->b_offset;
3979 else if (instr->i_jrel) {
3980 int delta = instr->i_target->b_offset - bsize;
3981 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003982 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003983 else
3984 continue;
3985 if (instr->i_oparg > 0xffff)
3986 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003987 }
3988 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003989
3990 /* XXX: This is an awful hack that could hurt performance, but
3991 on the bright side it should work until we come up
3992 with a better solution.
3993
3994 In the meantime, should the goto be dropped in favor
3995 of a loop?
3996
3997 The issue is that in the first loop blocksize() is called
3998 which calls instrsize() which requires i_oparg be set
3999 appropriately. There is a bootstrap problem because
4000 i_oparg is calculated in the second loop above.
4001
4002 So we loop until we stop seeing new EXTENDED_ARGs.
4003 The only EXTENDED_ARGs that could be popping up are
4004 ones in jump instructions. So this should converge
4005 fairly quickly.
4006 */
4007 if (last_extended_arg_count != extended_arg_count) {
4008 last_extended_arg_count = extended_arg_count;
4009 goto start;
4010 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004011}
4012
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004013static PyObject *
4014dict_keys_inorder(PyObject *dict, int offset)
4015{
4016 PyObject *tuple, *k, *v;
4017 int i, pos = 0, size = PyDict_Size(dict);
4018
4019 tuple = PyTuple_New(size);
4020 if (tuple == NULL)
4021 return NULL;
4022 while (PyDict_Next(dict, &pos, &k, &v)) {
4023 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004024 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004025 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004026 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004028 PyTuple_SET_ITEM(tuple, i - offset, k);
4029 }
4030 return tuple;
4031}
4032
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004033static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004035{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036 PySTEntryObject *ste = c->u->u_ste;
4037 int flags = 0, n;
4038 if (ste->ste_type != ModuleBlock)
4039 flags |= CO_NEWLOCALS;
4040 if (ste->ste_type == FunctionBlock) {
4041 if (!ste->ste_unoptimized)
4042 flags |= CO_OPTIMIZED;
4043 if (ste->ste_nested)
4044 flags |= CO_NESTED;
4045 if (ste->ste_generator)
4046 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004047 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048 if (ste->ste_varargs)
4049 flags |= CO_VARARGS;
4050 if (ste->ste_varkeywords)
4051 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004052 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 flags |= CO_GENERATOR;
4054 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4055 flags |= CO_FUTURE_DIVISION;
4056 n = PyDict_Size(c->u->u_freevars);
4057 if (n < 0)
4058 return -1;
4059 if (n == 0) {
4060 n = PyDict_Size(c->u->u_cellvars);
4061 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004062 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 if (n == 0) {
4064 flags |= CO_NOFREE;
4065 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004066 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004067
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004068 return flags;
4069}
4070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071static PyCodeObject *
4072makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004073{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074 PyObject *tmp;
4075 PyCodeObject *co = NULL;
4076 PyObject *consts = NULL;
4077 PyObject *names = NULL;
4078 PyObject *varnames = NULL;
4079 PyObject *filename = NULL;
4080 PyObject *name = NULL;
4081 PyObject *freevars = NULL;
4082 PyObject *cellvars = NULL;
4083 PyObject *bytecode = NULL;
4084 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086 tmp = dict_keys_inorder(c->u->u_consts, 0);
4087 if (!tmp)
4088 goto error;
4089 consts = PySequence_List(tmp); /* optimize_code requires a list */
4090 Py_DECREF(tmp);
4091
4092 names = dict_keys_inorder(c->u->u_names, 0);
4093 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4094 if (!consts || !names || !varnames)
4095 goto error;
4096
4097 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4098 if (!cellvars)
4099 goto error;
4100 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4101 if (!freevars)
4102 goto error;
4103 filename = PyString_FromString(c->c_filename);
4104 if (!filename)
4105 goto error;
4106
4107 nlocals = PyDict_Size(c->u->u_varnames);
4108 flags = compute_code_flags(c);
4109 if (flags < 0)
4110 goto error;
4111
4112 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4113 if (!bytecode)
4114 goto error;
4115
4116 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4117 if (!tmp)
4118 goto error;
4119 Py_DECREF(consts);
4120 consts = tmp;
4121
4122 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4123 bytecode, consts, names, varnames,
4124 freevars, cellvars,
4125 filename, c->u->u_name,
4126 c->u->u_firstlineno,
4127 a->a_lnotab);
4128 error:
4129 Py_XDECREF(consts);
4130 Py_XDECREF(names);
4131 Py_XDECREF(varnames);
4132 Py_XDECREF(filename);
4133 Py_XDECREF(name);
4134 Py_XDECREF(freevars);
4135 Py_XDECREF(cellvars);
4136 Py_XDECREF(bytecode);
4137 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004138}
4139
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140static PyCodeObject *
4141assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004142{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004143 basicblock *b, *entryblock;
4144 struct assembler a;
4145 int i, j, nblocks;
4146 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 /* Make sure every block that falls off the end returns None.
4149 XXX NEXT_BLOCK() isn't quite right, because if the last
4150 block ends with a jump or return b_next shouldn't set.
4151 */
4152 if (!c->u->u_curblock->b_return) {
4153 NEXT_BLOCK(c);
4154 if (addNone)
4155 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4156 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004157 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159 nblocks = 0;
4160 entryblock = NULL;
4161 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4162 nblocks++;
4163 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004164 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004165
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004166 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4167 goto error;
4168 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004169
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004170 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004171 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173 /* Emit code in reverse postorder from dfs. */
4174 for (i = a.a_nblocks - 1; i >= 0; i--) {
4175 basicblock *b = a.a_postorder[i];
4176 for (j = 0; j < b->b_iused; j++)
4177 if (!assemble_emit(&a, &b->b_instr[j]))
4178 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004179 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4182 goto error;
4183 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4184 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186 co = makecode(c, &a);
4187 error:
4188 assemble_free(&a);
4189 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004190}