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