blob: d813400697294d823c3cca544d5e6b3800761fcd [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossum9bfef441993-03-29 10:43:31 +00003Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025/* Compile an expression node to intermediate code */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027/* XXX TO DO:
28 XXX Compute maximum needed stack sizes while compiling
29 XXX Generate simple jump for break/return outside 'try...finally'
Guido van Rossum3f5da241990-12-20 15:06:42 +000030*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031
Guido van Rossum3f5da241990-12-20 15:06:42 +000032#include "allobjects.h"
33
Guido van Rossum10dc2e81990-11-18 17:27:39 +000034#include "node.h"
35#include "token.h"
36#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000037#include "compile.h"
38#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000039#include "structmember.h"
40
41#include <ctype.h>
Guido van Rossumf1dc5661993-07-05 10:31:29 +000042#include <errno.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000043
Guido van Rossum3f5da241990-12-20 15:06:42 +000044#define OFF(x) offsetof(codeobject, x)
45
46static struct memberlist code_memberlist[] = {
Guido van Rossum39d942d1992-01-12 02:30:05 +000047 {"co_code", T_OBJECT, OFF(co_code), READONLY},
48 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
49 {"co_names", T_OBJECT, OFF(co_names), READONLY},
50 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000051 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000052 {NULL} /* Sentinel */
53};
54
55static object *
56code_getattr(co, name)
57 codeobject *co;
58 char *name;
59{
60 return getmember((char *)co, code_memberlist, name);
61}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000062
63static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000064code_dealloc(co)
65 codeobject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +000066{
Guido van Rossum3f5da241990-12-20 15:06:42 +000067 XDECREF(co->co_code);
68 XDECREF(co->co_consts);
69 XDECREF(co->co_names);
70 XDECREF(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +000071 XDECREF(co->co_name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000072 DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +000073}
74
Guido van Rossum2dff9911992-09-03 20:50:59 +000075static object *
76code_repr(co)
77 codeobject *co;
78{
79 char buf[500];
80 int lineno = -1;
81 char *p = GETSTRINGVALUE(co->co_code);
82 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +000083 char *name = "???";
Guido van Rossum2dff9911992-09-03 20:50:59 +000084 if (*p == SET_LINENO)
85 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
86 if (co->co_filename && is_stringobject(co->co_filename))
87 filename = getstringvalue(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +000088 if (co->co_name && is_stringobject(co->co_name))
89 name = getstringvalue(co->co_name);
90 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
91 name, (long)co, filename, lineno);
Guido van Rossum2dff9911992-09-03 20:50:59 +000092 return newstringobject(buf);
93}
94
Guido van Rossum2e8f8a31993-11-05 10:20:10 +000095static int
96code_compare(co, cp)
97 codeobject *co, *cp;
98{
99 int cmp;
100 cmp = cmpobject((object *)co->co_code, (object *)cp->co_code);
101 if (cmp) return cmp;
102 cmp = cmpobject(co->co_consts, cp->co_consts);
103 if (cmp) return cmp;
104 cmp = cmpobject(co->co_names, cp->co_names);
105 return cmp;
106}
107
108static long
109code_hash(co)
110 codeobject *co;
111{
112 long h, h1, h2, h3;
113 h1 = hashobject((object *)co->co_code);
114 if (h1 == -1) return -1;
115 h2 = hashobject(co->co_consts);
116 if (h2 == -1) return -1;
117 h3 = hashobject(co->co_names);
118 if (h3 == -1) return -1;
119 h = h1 ^ h2 ^ h3;
120 if (h == -1) h = -2;
121 return h;
122}
123
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000124typeobject Codetype = {
125 OB_HEAD_INIT(&Typetype)
126 0,
127 "code",
128 sizeof(codeobject),
129 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000130 (destructor)code_dealloc, /*tp_dealloc*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000131 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000132 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000133 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000134 (cmpfunc)code_compare, /*tp_compare*/
135 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000136 0, /*tp_as_number*/
137 0, /*tp_as_sequence*/
138 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000139 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000140};
141
Guido van Rossuma082ce41991-06-04 19:41:56 +0000142codeobject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000143newcodeobject(code, consts, names, filename, name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000144 object *code;
145 object *consts;
146 object *names;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000147 object *filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000148 object *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000149{
150 codeobject *co;
151 int i;
152 /* Check argument types */
153 if (code == NULL || !is_stringobject(code) ||
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000154 consts == NULL ||
155 names == NULL ||
Guido van Rossum590baa41993-11-30 13:40:46 +0000156 name == NULL || !(is_stringobject(name) || name == None)) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000157 err_badcall();
158 return NULL;
159 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000160 /* Allow two lists instead of two tuples */
161 if (is_listobject(consts) && is_listobject(names)) {
162 consts = listtuple(consts);
163 if (consts == NULL)
164 return NULL;
165 names = listtuple(names);
166 if (names == NULL) {
167 DECREF(consts);
168 return NULL;
169 }
170 }
171 else if (!is_tupleobject(consts) && !is_tupleobject(names)) {
172 err_badcall();
173 return NULL;
174 }
175 else {
176 INCREF(consts);
177 INCREF(names);
178 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000179 /* Make sure the list of names contains only strings */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000180 for (i = gettuplesize(names); --i >= 0; ) {
181 object *v = gettupleitem(names, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000182 if (v == NULL || !is_stringobject(v)) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000183 DECREF(consts);
184 DECREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000185 err_badcall();
186 return NULL;
187 }
188 }
189 co = NEWOBJ(codeobject, &Codetype);
190 if (co != NULL) {
191 INCREF(code);
192 co->co_code = (stringobject *)code;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000193 co->co_consts = consts;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000194 co->co_names = names;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000195 INCREF(filename);
196 co->co_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000197 INCREF(name);
198 co->co_name = name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000199 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000200 else {
201 DECREF(consts);
202 DECREF(names);
203 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000204 return co;
205}
206
207
208/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000209
210#define MAXBLOCKS 20 /* Max static block nesting within a function */
211
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000212struct compiling {
213 object *c_code; /* string */
214 object *c_consts; /* list of objects */
215 object *c_names; /* list of strings (names) */
Guido van Rossumc5e96291991-12-10 13:53:51 +0000216 object *c_globals; /* dictionary */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000217 int c_nexti; /* index into c_code */
218 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000219 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000220 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000221 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000222 int c_begin; /* begin of current loop, for 'continue' */
223 int c_block[MAXBLOCKS]; /* stack of block types */
224 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000225 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000226 char *c_name; /* name of object (e.g. function) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000227};
228
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000229
230/* Interface to the block stack */
231
232static void
233block_push(c, type)
234 struct compiling *c;
235 int type;
236{
237 if (c->c_nblocks >= MAXBLOCKS) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +0000238 err_setstr(SystemError, "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000239 c->c_errors++;
240 }
241 else {
242 c->c_block[c->c_nblocks++] = type;
243 }
244}
245
246static void
247block_pop(c, type)
248 struct compiling *c;
249 int type;
250{
251 if (c->c_nblocks > 0)
252 c->c_nblocks--;
253 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
254 err_setstr(SystemError, "bad block pop");
255 c->c_errors++;
256 }
257}
258
259
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000260/* Prototypes */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000261
Guido van Rossum590baa41993-11-30 13:40:46 +0000262static int com_init PROTO((struct compiling *, char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000263static void com_free PROTO((struct compiling *));
264static void com_done PROTO((struct compiling *));
265static void com_node PROTO((struct compiling *, struct _node *));
266static void com_addbyte PROTO((struct compiling *, int));
267static void com_addint PROTO((struct compiling *, int));
268static void com_addoparg PROTO((struct compiling *, int, int));
269static void com_addfwref PROTO((struct compiling *, int, int *));
270static void com_backpatch PROTO((struct compiling *, int));
271static int com_add PROTO((struct compiling *, object *, object *));
272static int com_addconst PROTO((struct compiling *, object *));
273static int com_addname PROTO((struct compiling *, object *));
274static void com_addopname PROTO((struct compiling *, int, node *));
Guido van Rossum288a60f1991-12-16 13:05:10 +0000275static void com_list PROTO((struct compiling *, node *, int));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000276static int com_argdefs PROTO((struct compiling *, node *, int *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000277
278static int
Guido van Rossum590baa41993-11-30 13:40:46 +0000279com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000280 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000281 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000282{
Guido van Rossum62d46241991-04-03 19:00:23 +0000283 if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000284 goto fail_3;
285 if ((c->c_consts = newlistobject(0)) == NULL)
286 goto fail_2;
287 if ((c->c_names = newlistobject(0)) == NULL)
288 goto fail_1;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000289 if ((c->c_globals = newdictobject()) == NULL)
290 goto fail_0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000291 c->c_nexti = 0;
292 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000293 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000294 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000295 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000296 c->c_begin = 0;
297 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000298 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000299 c->c_name = "?";
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000300 return 1;
301
Guido van Rossumc5e96291991-12-10 13:53:51 +0000302 fail_0:
303 DECREF(c->c_names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000304 fail_1:
305 DECREF(c->c_consts);
306 fail_2:
307 DECREF(c->c_code);
308 fail_3:
309 return 0;
310}
311
312static void
313com_free(c)
314 struct compiling *c;
315{
316 XDECREF(c->c_code);
317 XDECREF(c->c_consts);
318 XDECREF(c->c_names);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000319 XDECREF(c->c_globals);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000320}
321
322static void
323com_done(c)
324 struct compiling *c;
325{
326 if (c->c_code != NULL)
327 resizestring(&c->c_code, c->c_nexti);
328}
329
330static void
331com_addbyte(c, byte)
332 struct compiling *c;
333 int byte;
334{
335 int len;
336 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000337 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000338 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
339 abort();
Guido van Rossum01cfd441991-10-20 20:12:38 +0000340 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000341 err_setstr(SystemError, "com_addbyte: byte out of range");
342 c->c_errors++;
343 }
344 if (c->c_code == NULL)
345 return;
346 len = getstringsize(c->c_code);
347 if (c->c_nexti >= len) {
348 if (resizestring(&c->c_code, len+1000) != 0) {
349 c->c_errors++;
350 return;
351 }
352 }
353 getstringvalue(c->c_code)[c->c_nexti++] = byte;
354}
355
356static void
357com_addint(c, x)
358 struct compiling *c;
359 int x;
360{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000361 com_addbyte(c, x & 0xff);
362 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000363}
364
365static void
366com_addoparg(c, op, arg)
367 struct compiling *c;
368 int op;
369 int arg;
370{
371 com_addbyte(c, op);
372 com_addint(c, arg);
373}
374
375static void
376com_addfwref(c, op, p_anchor)
377 struct compiling *c;
378 int op;
379 int *p_anchor;
380{
381 /* Compile a forward reference for backpatching */
382 int here;
383 int anchor;
384 com_addbyte(c, op);
385 here = c->c_nexti;
386 anchor = *p_anchor;
387 *p_anchor = here;
388 com_addint(c, anchor == 0 ? 0 : here - anchor);
389}
390
391static void
392com_backpatch(c, anchor)
393 struct compiling *c;
394 int anchor; /* Must be nonzero */
395{
396 unsigned char *code = (unsigned char *) getstringvalue(c->c_code);
397 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000398 int dist;
399 int prev;
400 for (;;) {
401 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000402 prev = code[anchor] + (code[anchor+1] << 8);
403 dist = target - (anchor+2);
404 code[anchor] = dist & 0xff;
405 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000406 if (!prev)
407 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000408 anchor -= prev;
409 }
410}
411
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000412/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000413
414static int
415com_add(c, list, v)
416 struct compiling *c;
417 object *list;
418 object *v;
419{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000420 int n = getlistsize(list);
421 int i;
422 for (i = n; --i >= 0; ) {
423 object *w = getlistitem(list, i);
Guido van Rossumefc0bd01991-07-01 18:44:20 +0000424 if (v->ob_type == w->ob_type && cmpobject(v, w) == 0)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000425 return i;
426 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000427 if (addlistitem(list, v) != 0)
428 c->c_errors++;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000429 return n;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000430}
431
432static int
433com_addconst(c, v)
434 struct compiling *c;
435 object *v;
436{
437 return com_add(c, c->c_consts, v);
438}
439
440static int
441com_addname(c, v)
442 struct compiling *c;
443 object *v;
444{
445 return com_add(c, c->c_names, v);
446}
447
448static void
449com_addopname(c, op, n)
450 struct compiling *c;
451 int op;
452 node *n;
453{
454 object *v;
455 int i;
456 char *name;
457 if (TYPE(n) == STAR)
458 name = "*";
459 else {
460 REQ(n, NAME);
461 name = STR(n);
462 }
463 if ((v = newstringobject(name)) == NULL) {
464 c->c_errors++;
465 i = 255;
466 }
467 else {
468 i = com_addname(c, v);
469 DECREF(v);
470 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000471 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
472 switch (op) {
473 case LOAD_NAME:
474 case STORE_NAME:
475 case DELETE_NAME:
476 if (dictlookup(c->c_globals, name) != NULL) {
477 switch (op) {
478 case LOAD_NAME: op = LOAD_GLOBAL; break;
479 case STORE_NAME: op = STORE_GLOBAL; break;
480 case DELETE_NAME: op = DELETE_GLOBAL; break;
481 }
482 }
483 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000484 com_addoparg(c, op, i);
485}
486
487static object *
488parsenumber(s)
489 char *s;
490{
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000491 extern long mystrtol PROTO((const char *, char **, int));
492 extern unsigned long mystrtoul PROTO((const char *, char **, int));
493 extern double atof PROTO((const char *));
Guido van Rossumc5e96291991-12-10 13:53:51 +0000494 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000495 long x;
Guido van Rossum282914b1991-04-04 10:42:56 +0000496 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000497 end = s + strlen(s) - 1;
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000498 if (*end == 'l' || *end == 'L')
Guido van Rossumc5e96291991-12-10 13:53:51 +0000499 return long_scan(s, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000500 if (s[0] == '0')
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000501 x = (long) mystrtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000502 else
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000503 x = mystrtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000504 if (*end == '\0') {
505 if (errno != 0) {
Guido van Rossumefb087b1991-12-16 15:41:41 +0000506 err_setstr(OverflowError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000507 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000508 return NULL;
509 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000510 return newintobject(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000511 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000512 /* XXX Huge floats may silently fail */
513 return newfloatobject(atof(s));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000514}
515
516static object *
517parsestr(s)
518 char *s;
519{
520 object *v;
521 int len;
522 char *buf;
523 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000524 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000525 int c;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000526 int quote = *s;
527 if (quote != '\'' && quote != '\"') {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000528 err_badcall();
529 return NULL;
530 }
531 s++;
532 len = strlen(s);
Guido van Rossum8054fad1993-10-26 15:19:44 +0000533 if (s[--len] != quote) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000534 err_badcall();
535 return NULL;
536 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000537 if (len >= 4 && s[0] == quote && s[1] == quote) {
538 s += 2;
539 len -= 2;
540 if (s[--len] != quote || s[--len] != quote) {
541 err_badcall();
542 return NULL;
543 }
544 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000545 if (strchr(s, '\\') == NULL)
546 return newsizedstringobject(s, len);
547 v = newsizedstringobject((char *)NULL, len);
548 p = buf = getstringvalue(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000549 end = s + len;
550 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000551 if (*s != '\\') {
552 *p++ = *s++;
553 continue;
554 }
555 s++;
556 switch (*s++) {
557 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000558 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000559 case '\\': *p++ = '\\'; break;
560 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000561 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000562 case 'b': *p++ = '\b'; break;
563 case 'f': *p++ = '\014'; break; /* FF */
564 case 't': *p++ = '\t'; break;
565 case 'n': *p++ = '\n'; break;
566 case 'r': *p++ = '\r'; break;
567 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000568 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
569 case '0': case '1': case '2': case '3':
570 case '4': case '5': case '6': case '7':
571 c = s[-1] - '0';
572 if ('0' <= *s && *s <= '7') {
573 c = (c<<3) + *s++ - '0';
574 if ('0' <= *s && *s <= '7')
575 c = (c<<3) + *s++ - '0';
576 }
577 *p++ = c;
578 break;
579 case 'x':
580 if (isxdigit(*s)) {
581 sscanf(s, "%x", &c);
582 *p++ = c;
583 do {
584 s++;
585 } while (isxdigit(*s));
586 break;
587 }
588 /* FALLTHROUGH */
589 default: *p++ = '\\'; *p++ = s[-1]; break;
590 }
591 }
592 resizestring(&v, (int)(p - buf));
593 return v;
594}
595
596static void
597com_list_constructor(c, n)
598 struct compiling *c;
599 node *n;
600{
601 int len;
602 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000603 if (TYPE(n) != testlist)
604 REQ(n, exprlist);
605 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
606 len = (NCH(n) + 1) / 2;
607 for (i = 0; i < NCH(n); i += 2)
608 com_node(c, CHILD(n, i));
609 com_addoparg(c, BUILD_LIST, len);
610}
611
612static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000613com_dictmaker(c, n)
614 struct compiling *c;
615 node *n;
616{
617 int i;
618 /* dictmaker: test ':' test (',' test ':' value)* [','] */
619 for (i = 0; i+2 < NCH(n); i += 4) {
620 /* We must arrange things just right for STORE_SUBSCR.
621 It wants the stack to look like (value) (dict) (key) */
622 com_addbyte(c, DUP_TOP);
623 com_node(c, CHILD(n, i+2)); /* value */
624 com_addbyte(c, ROT_TWO);
625 com_node(c, CHILD(n, i)); /* key */
626 com_addbyte(c, STORE_SUBSCR);
627 }
628}
629
630static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000631com_atom(c, n)
632 struct compiling *c;
633 node *n;
634{
635 node *ch;
636 object *v;
637 int i;
638 REQ(n, atom);
639 ch = CHILD(n, 0);
640 switch (TYPE(ch)) {
641 case LPAR:
642 if (TYPE(CHILD(n, 1)) == RPAR)
643 com_addoparg(c, BUILD_TUPLE, 0);
644 else
645 com_node(c, CHILD(n, 1));
646 break;
647 case LSQB:
648 if (TYPE(CHILD(n, 1)) == RSQB)
649 com_addoparg(c, BUILD_LIST, 0);
650 else
651 com_list_constructor(c, CHILD(n, 1));
652 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000653 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000654 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000655 if (TYPE(CHILD(n, 1)) != RBRACE)
656 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000657 break;
658 case BACKQUOTE:
659 com_node(c, CHILD(n, 1));
660 com_addbyte(c, UNARY_CONVERT);
661 break;
662 case NUMBER:
663 if ((v = parsenumber(STR(ch))) == NULL) {
664 c->c_errors++;
665 i = 255;
666 }
667 else {
668 i = com_addconst(c, v);
669 DECREF(v);
670 }
671 com_addoparg(c, LOAD_CONST, i);
672 break;
673 case STRING:
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000674 if ((v = parsestr(STR(ch))) != NULL) {
675 /* String literal concatenation */
676 for (i = 1; i < NCH(n) && v != NULL; i++) {
677 joinstring_decref(&v,
678 parsestr(STR(CHILD(n, i))));
679 }
680 }
681 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000682 c->c_errors++;
683 i = 255;
684 }
685 else {
686 i = com_addconst(c, v);
687 DECREF(v);
688 }
689 com_addoparg(c, LOAD_CONST, i);
690 break;
691 case NAME:
692 com_addopname(c, LOAD_NAME, ch);
693 break;
694 default:
695 fprintf(stderr, "node type %d\n", TYPE(ch));
696 err_setstr(SystemError, "com_atom: unexpected node type");
697 c->c_errors++;
698 }
699}
700
701static void
702com_slice(c, n, op)
703 struct compiling *c;
704 node *n;
705 int op;
706{
707 if (NCH(n) == 1) {
708 com_addbyte(c, op);
709 }
710 else if (NCH(n) == 2) {
711 if (TYPE(CHILD(n, 0)) != COLON) {
712 com_node(c, CHILD(n, 0));
713 com_addbyte(c, op+1);
714 }
715 else {
716 com_node(c, CHILD(n, 1));
717 com_addbyte(c, op+2);
718 }
719 }
720 else {
721 com_node(c, CHILD(n, 0));
722 com_node(c, CHILD(n, 2));
723 com_addbyte(c, op+3);
724 }
725}
726
727static void
728com_apply_subscript(c, n)
729 struct compiling *c;
730 node *n;
731{
732 REQ(n, subscript);
733 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) {
734 /* It's a single subscript */
735 com_node(c, CHILD(n, 0));
736 com_addbyte(c, BINARY_SUBSCR);
737 }
738 else {
739 /* It's a slice: [expr] ':' [expr] */
740 com_slice(c, n, SLICE);
741 }
742}
743
744static void
745com_call_function(c, n)
746 struct compiling *c;
747 node *n; /* EITHER testlist OR ')' */
748{
749 if (TYPE(n) == RPAR) {
Guido van Rossum288a60f1991-12-16 13:05:10 +0000750 com_addoparg(c, BUILD_TUPLE, 0);
751 com_addbyte(c, BINARY_CALL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000752 }
753 else {
Guido van Rossum288a60f1991-12-16 13:05:10 +0000754 REQ(n, testlist);
755 com_list(c, n, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000756 com_addbyte(c, BINARY_CALL);
757 }
758}
759
760static void
761com_select_member(c, n)
762 struct compiling *c;
763 node *n;
764{
765 com_addopname(c, LOAD_ATTR, n);
766}
767
768static void
769com_apply_trailer(c, n)
770 struct compiling *c;
771 node *n;
772{
773 REQ(n, trailer);
774 switch (TYPE(CHILD(n, 0))) {
775 case LPAR:
776 com_call_function(c, CHILD(n, 1));
777 break;
778 case DOT:
779 com_select_member(c, CHILD(n, 1));
780 break;
781 case LSQB:
782 com_apply_subscript(c, CHILD(n, 1));
783 break;
784 default:
785 err_setstr(SystemError,
786 "com_apply_trailer: unknown trailer type");
787 c->c_errors++;
788 }
789}
790
791static void
792com_factor(c, n)
793 struct compiling *c;
794 node *n;
795{
796 int i;
797 REQ(n, factor);
798 if (TYPE(CHILD(n, 0)) == PLUS) {
799 com_factor(c, CHILD(n, 1));
800 com_addbyte(c, UNARY_POSITIVE);
801 }
802 else if (TYPE(CHILD(n, 0)) == MINUS) {
803 com_factor(c, CHILD(n, 1));
804 com_addbyte(c, UNARY_NEGATIVE);
805 }
Guido van Rossum7928cd71991-10-24 14:59:31 +0000806 else if (TYPE(CHILD(n, 0)) == TILDE) {
807 com_factor(c, CHILD(n, 1));
808 com_addbyte(c, UNARY_INVERT);
809 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000810 else {
811 com_atom(c, CHILD(n, 0));
812 for (i = 1; i < NCH(n); i++)
813 com_apply_trailer(c, CHILD(n, i));
814 }
815}
816
817static void
818com_term(c, n)
819 struct compiling *c;
820 node *n;
821{
822 int i;
823 int op;
824 REQ(n, term);
825 com_factor(c, CHILD(n, 0));
826 for (i = 2; i < NCH(n); i += 2) {
827 com_factor(c, CHILD(n, i));
828 switch (TYPE(CHILD(n, i-1))) {
829 case STAR:
830 op = BINARY_MULTIPLY;
831 break;
832 case SLASH:
833 op = BINARY_DIVIDE;
834 break;
835 case PERCENT:
836 op = BINARY_MODULO;
837 break;
838 default:
839 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +0000840 "com_term: operator not *, / or %");
841 c->c_errors++;
842 op = 255;
843 }
844 com_addbyte(c, op);
845 }
846}
847
848static void
849com_arith_expr(c, n)
850 struct compiling *c;
851 node *n;
852{
853 int i;
854 int op;
855 REQ(n, arith_expr);
856 com_term(c, CHILD(n, 0));
857 for (i = 2; i < NCH(n); i += 2) {
858 com_term(c, CHILD(n, i));
859 switch (TYPE(CHILD(n, i-1))) {
860 case PLUS:
861 op = BINARY_ADD;
862 break;
863 case MINUS:
864 op = BINARY_SUBTRACT;
865 break;
866 default:
867 err_setstr(SystemError,
868 "com_arith_expr: operator not + or -");
869 c->c_errors++;
870 op = 255;
871 }
872 com_addbyte(c, op);
873 }
874}
875
876static void
877com_shift_expr(c, n)
878 struct compiling *c;
879 node *n;
880{
881 int i;
882 int op;
883 REQ(n, shift_expr);
884 com_arith_expr(c, CHILD(n, 0));
885 for (i = 2; i < NCH(n); i += 2) {
886 com_arith_expr(c, CHILD(n, i));
887 switch (TYPE(CHILD(n, i-1))) {
888 case LEFTSHIFT:
889 op = BINARY_LSHIFT;
890 break;
891 case RIGHTSHIFT:
892 op = BINARY_RSHIFT;
893 break;
894 default:
895 err_setstr(SystemError,
896 "com_shift_expr: operator not << or >>");
897 c->c_errors++;
898 op = 255;
899 }
900 com_addbyte(c, op);
901 }
902}
903
904static void
905com_and_expr(c, n)
906 struct compiling *c;
907 node *n;
908{
909 int i;
910 int op;
911 REQ(n, and_expr);
912 com_shift_expr(c, CHILD(n, 0));
913 for (i = 2; i < NCH(n); i += 2) {
914 com_shift_expr(c, CHILD(n, i));
915 if (TYPE(CHILD(n, i-1)) == AMPER) {
916 op = BINARY_AND;
917 }
918 else {
919 err_setstr(SystemError,
920 "com_and_expr: operator not &");
921 c->c_errors++;
922 op = 255;
923 }
924 com_addbyte(c, op);
925 }
926}
927
928static void
929com_xor_expr(c, n)
930 struct compiling *c;
931 node *n;
932{
933 int i;
934 int op;
935 REQ(n, xor_expr);
936 com_and_expr(c, CHILD(n, 0));
937 for (i = 2; i < NCH(n); i += 2) {
938 com_and_expr(c, CHILD(n, i));
939 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
940 op = BINARY_XOR;
941 }
942 else {
943 err_setstr(SystemError,
944 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000945 c->c_errors++;
946 op = 255;
947 }
948 com_addbyte(c, op);
949 }
950}
951
952static void
953com_expr(c, n)
954 struct compiling *c;
955 node *n;
956{
957 int i;
958 int op;
959 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000960 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000961 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +0000962 com_xor_expr(c, CHILD(n, i));
963 if (TYPE(CHILD(n, i-1)) == VBAR) {
964 op = BINARY_OR;
965 }
966 else {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000967 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +0000968 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000969 c->c_errors++;
970 op = 255;
971 }
972 com_addbyte(c, op);
973 }
974}
975
976static enum cmp_op
977cmp_type(n)
978 node *n;
979{
980 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +0000981 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000982 | 'in' | 'not' 'in' | 'is' | 'is' not' */
983 if (NCH(n) == 1) {
984 n = CHILD(n, 0);
985 switch (TYPE(n)) {
986 case LESS: return LT;
987 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +0000988 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000989 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +0000990 case LESSEQUAL: return LE;
991 case GREATEREQUAL: return GE;
992 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000993 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
994 if (strcmp(STR(n), "is") == 0) return IS;
995 }
996 }
997 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000998 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000999 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1000 return NOT_IN;
1001 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1002 return IS_NOT;
1003 }
1004 }
1005 return BAD;
1006}
1007
1008static void
1009com_comparison(c, n)
1010 struct compiling *c;
1011 node *n;
1012{
1013 int i;
1014 enum cmp_op op;
1015 int anchor;
1016 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1017 com_expr(c, CHILD(n, 0));
1018 if (NCH(n) == 1)
1019 return;
1020
1021 /****************************************************************
1022 The following code is generated for all but the last
1023 comparison in a chain:
1024
1025 label: on stack: opcode: jump to:
1026
1027 a <code to load b>
1028 a, b DUP_TOP
1029 a, b, b ROT_THREE
1030 b, a, b COMPARE_OP
1031 b, 0-or-1 JUMP_IF_FALSE L1
1032 b, 1 POP_TOP
1033 b
1034
1035 We are now ready to repeat this sequence for the next
1036 comparison in the chain.
1037
1038 For the last we generate:
1039
1040 b <code to load c>
1041 b, c COMPARE_OP
1042 0-or-1
1043
1044 If there were any jumps to L1 (i.e., there was more than one
1045 comparison), we generate:
1046
1047 0-or-1 JUMP_FORWARD L2
1048 L1: b, 0 ROT_TWO
1049 0, b POP_TOP
1050 0
1051 L2:
1052 ****************************************************************/
1053
1054 anchor = 0;
1055
1056 for (i = 2; i < NCH(n); i += 2) {
1057 com_expr(c, CHILD(n, i));
1058 if (i+2 < NCH(n)) {
1059 com_addbyte(c, DUP_TOP);
1060 com_addbyte(c, ROT_THREE);
1061 }
1062 op = cmp_type(CHILD(n, i-1));
1063 if (op == BAD) {
1064 err_setstr(SystemError,
1065 "com_comparison: unknown comparison op");
1066 c->c_errors++;
1067 }
1068 com_addoparg(c, COMPARE_OP, op);
1069 if (i+2 < NCH(n)) {
1070 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1071 com_addbyte(c, POP_TOP);
1072 }
1073 }
1074
1075 if (anchor) {
1076 int anchor2 = 0;
1077 com_addfwref(c, JUMP_FORWARD, &anchor2);
1078 com_backpatch(c, anchor);
1079 com_addbyte(c, ROT_TWO);
1080 com_addbyte(c, POP_TOP);
1081 com_backpatch(c, anchor2);
1082 }
1083}
1084
1085static void
1086com_not_test(c, n)
1087 struct compiling *c;
1088 node *n;
1089{
1090 REQ(n, not_test); /* 'not' not_test | comparison */
1091 if (NCH(n) == 1) {
1092 com_comparison(c, CHILD(n, 0));
1093 }
1094 else {
1095 com_not_test(c, CHILD(n, 1));
1096 com_addbyte(c, UNARY_NOT);
1097 }
1098}
1099
1100static void
1101com_and_test(c, n)
1102 struct compiling *c;
1103 node *n;
1104{
1105 int i;
1106 int anchor;
1107 REQ(n, and_test); /* not_test ('and' not_test)* */
1108 anchor = 0;
1109 i = 0;
1110 for (;;) {
1111 com_not_test(c, CHILD(n, i));
1112 if ((i += 2) >= NCH(n))
1113 break;
1114 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1115 com_addbyte(c, POP_TOP);
1116 }
1117 if (anchor)
1118 com_backpatch(c, anchor);
1119}
1120
1121static void
1122com_test(c, n)
1123 struct compiling *c;
1124 node *n;
1125{
Guido van Rossum57531fe1993-11-30 14:57:42 +00001126 REQ(n, test); /* and_test ('and' and_test)* | lambdef */
1127 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
1128 object *v;
1129 int i;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001130 int argcount;
1131 int ndefs = com_argdefs(c, CHILD(n, 0), &argcount);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001132 v = (object *) compile(CHILD(n, 0), c->c_filename);
1133 if (v == NULL) {
1134 c->c_errors++;
1135 i = 255;
1136 }
1137 else {
1138 i = com_addconst(c, v);
1139 DECREF(v);
1140 }
1141 com_addoparg(c, LOAD_CONST, i);
1142 com_addbyte(c, BUILD_FUNCTION);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001143 if (ndefs > 0)
1144 com_addoparg(c, SET_FUNC_ARGS, argcount);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001145 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001146 else {
1147 int anchor = 0;
1148 int i = 0;
1149 for (;;) {
1150 com_and_test(c, CHILD(n, i));
1151 if ((i += 2) >= NCH(n))
1152 break;
1153 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1154 com_addbyte(c, POP_TOP);
1155 }
1156 if (anchor)
1157 com_backpatch(c, anchor);
1158 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001159}
1160
1161static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001162com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001163 struct compiling *c;
1164 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001165 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001166{
1167 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001168 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001169 com_node(c, CHILD(n, 0));
1170 }
1171 else {
1172 int i;
1173 int len;
1174 len = (NCH(n) + 1) / 2;
1175 for (i = 0; i < NCH(n); i += 2)
1176 com_node(c, CHILD(n, i));
1177 com_addoparg(c, BUILD_TUPLE, len);
1178 }
1179}
1180
1181
1182/* Begin of assignment compilation */
1183
1184static void com_assign_name PROTO((struct compiling *, node *, int));
1185static void com_assign PROTO((struct compiling *, node *, int));
1186
1187static void
1188com_assign_attr(c, n, assigning)
1189 struct compiling *c;
1190 node *n;
1191 int assigning;
1192{
1193 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
1194}
1195
1196static void
1197com_assign_slice(c, n, assigning)
1198 struct compiling *c;
1199 node *n;
1200 int assigning;
1201{
1202 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE);
1203}
1204
1205static void
1206com_assign_subscript(c, n, assigning)
1207 struct compiling *c;
1208 node *n;
1209 int assigning;
1210{
1211 com_node(c, n);
1212 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR);
1213}
1214
1215static void
1216com_assign_trailer(c, n, assigning)
1217 struct compiling *c;
1218 node *n;
1219 int assigning;
1220{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001221 REQ(n, trailer);
1222 switch (TYPE(CHILD(n, 0))) {
1223 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001224 err_setstr(SyntaxError, "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001225 c->c_errors++;
1226 break;
1227 case DOT: /* '.' NAME */
1228 com_assign_attr(c, CHILD(n, 1), assigning);
1229 break;
1230 case LSQB: /* '[' subscript ']' */
1231 n = CHILD(n, 1);
1232 REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */
1233 if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON)
1234 com_assign_slice(c, n, assigning);
1235 else
1236 com_assign_subscript(c, CHILD(n, 0), assigning);
1237 break;
1238 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001239 err_setstr(SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001240 c->c_errors++;
1241 }
1242}
1243
1244static void
1245com_assign_tuple(c, n, assigning)
1246 struct compiling *c;
1247 node *n;
1248 int assigning;
1249{
1250 int i;
1251 if (TYPE(n) != testlist)
1252 REQ(n, exprlist);
1253 if (assigning)
1254 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
1255 for (i = 0; i < NCH(n); i += 2)
1256 com_assign(c, CHILD(n, i), assigning);
1257}
1258
1259static void
1260com_assign_list(c, n, assigning)
1261 struct compiling *c;
1262 node *n;
1263 int assigning;
1264{
1265 int i;
1266 if (assigning)
1267 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2);
1268 for (i = 0; i < NCH(n); i += 2)
1269 com_assign(c, CHILD(n, i), assigning);
1270}
1271
1272static void
1273com_assign_name(c, n, assigning)
1274 struct compiling *c;
1275 node *n;
1276 int assigning;
1277{
1278 REQ(n, NAME);
1279 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
1280}
1281
1282static void
1283com_assign(c, n, assigning)
1284 struct compiling *c;
1285 node *n;
1286 int assigning;
1287{
1288 /* Loop to avoid trivial recursion */
1289 for (;;) {
1290 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001291
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001292 case exprlist:
1293 case testlist:
1294 if (NCH(n) > 1) {
1295 com_assign_tuple(c, n, assigning);
1296 return;
1297 }
1298 n = CHILD(n, 0);
1299 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001300
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001301 case test:
1302 case and_test:
1303 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001304 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001305 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001306 case xor_expr:
1307 case and_expr:
1308 case shift_expr:
1309 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001310 case term:
1311 if (NCH(n) > 1) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001312 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001313 "can't assign to operator");
1314 c->c_errors++;
1315 return;
1316 }
1317 n = CHILD(n, 0);
1318 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001319
Guido van Rossum7928cd71991-10-24 14:59:31 +00001320 case factor: /* ('+'|'-'|'~') factor | atom trailer* */
1321 if (TYPE(CHILD(n, 0)) != atom) { /* '+'|'-'|'~' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001322 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001323 "can't assign to operator");
1324 c->c_errors++;
1325 return;
1326 }
1327 if (NCH(n) > 1) { /* trailer present */
1328 int i;
1329 com_node(c, CHILD(n, 0));
1330 for (i = 1; i+1 < NCH(n); i++) {
1331 com_apply_trailer(c, CHILD(n, i));
1332 } /* NB i is still alive */
1333 com_assign_trailer(c,
1334 CHILD(n, i), assigning);
1335 return;
1336 }
1337 n = CHILD(n, 0);
1338 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001339
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001340 case atom:
1341 switch (TYPE(CHILD(n, 0))) {
1342 case LPAR:
1343 n = CHILD(n, 1);
1344 if (TYPE(n) == RPAR) {
1345 /* XXX Should allow () = () ??? */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001346 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001347 "can't assign to ()");
1348 c->c_errors++;
1349 return;
1350 }
1351 break;
1352 case LSQB:
1353 n = CHILD(n, 1);
1354 if (TYPE(n) == RSQB) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001355 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001356 "can't assign to []");
1357 c->c_errors++;
1358 return;
1359 }
1360 com_assign_list(c, n, assigning);
1361 return;
1362 case NAME:
1363 com_assign_name(c, CHILD(n, 0), assigning);
1364 return;
1365 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001366 err_setstr(SyntaxError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +00001367 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001368 c->c_errors++;
1369 return;
1370 }
1371 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001372
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001373 default:
1374 fprintf(stderr, "node type %d\n", TYPE(n));
1375 err_setstr(SystemError, "com_assign: bad node");
1376 c->c_errors++;
1377 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001378
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001379 }
1380 }
1381}
1382
1383static void
1384com_expr_stmt(c, n)
1385 struct compiling *c;
1386 node *n;
1387{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001388 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001389 com_node(c, CHILD(n, NCH(n)-1));
1390 if (NCH(n) == 1) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001391 if (c->c_interactive)
1392 com_addbyte(c, PRINT_EXPR);
1393 else
1394 com_addbyte(c, POP_TOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001395 }
1396 else {
1397 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001398 for (i = 0; i < NCH(n)-2; i+=2) {
1399 if (i+2 < NCH(n)-2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001400 com_addbyte(c, DUP_TOP);
1401 com_assign(c, CHILD(n, i), 1/*assign*/);
1402 }
1403 }
1404}
1405
1406static void
1407com_print_stmt(c, n)
1408 struct compiling *c;
1409 node *n;
1410{
1411 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001412 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
1413 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001414 com_node(c, CHILD(n, i));
1415 com_addbyte(c, PRINT_ITEM);
1416 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001417 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001418 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001419 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001420}
1421
1422static void
1423com_return_stmt(c, n)
1424 struct compiling *c;
1425 node *n;
1426{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001427 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001428 if (!c->c_infunction) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001429 err_setstr(SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001430 c->c_errors++;
1431 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001432 if (NCH(n) < 2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001433 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1434 else
1435 com_node(c, CHILD(n, 1));
1436 com_addbyte(c, RETURN_VALUE);
1437}
1438
1439static void
1440com_raise_stmt(c, n)
1441 struct compiling *c;
1442 node *n;
1443{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001444 REQ(n, raise_stmt); /* 'raise' test [',' test] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001445 com_node(c, CHILD(n, 1));
1446 if (NCH(n) > 3)
1447 com_node(c, CHILD(n, 3));
1448 else
1449 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1450 com_addbyte(c, RAISE_EXCEPTION);
1451}
1452
1453static void
1454com_import_stmt(c, n)
1455 struct compiling *c;
1456 node *n;
1457{
1458 int i;
1459 REQ(n, import_stmt);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001460 /* 'import' NAME (',' NAME)* |
1461 'from' NAME 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001462 if (STR(CHILD(n, 0))[0] == 'f') {
1463 /* 'from' NAME 'import' ... */
1464 REQ(CHILD(n, 1), NAME);
1465 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
1466 for (i = 3; i < NCH(n); i += 2)
1467 com_addopname(c, IMPORT_FROM, CHILD(n, i));
1468 com_addbyte(c, POP_TOP);
1469 }
1470 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001471 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001472 for (i = 1; i < NCH(n); i += 2) {
1473 com_addopname(c, IMPORT_NAME, CHILD(n, i));
1474 com_addopname(c, STORE_NAME, CHILD(n, i));
1475 }
1476 }
1477}
1478
1479static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001480com_global_stmt(c, n)
1481 struct compiling *c;
1482 node *n;
1483{
1484 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001485 REQ(n, global_stmt);
1486 /* 'global' NAME (',' NAME)* */
1487 for (i = 1; i < NCH(n); i += 2) {
1488 if (dictinsert(c->c_globals, STR(CHILD(n, i)), None) != 0)
1489 c->c_errors++;
1490 }
1491}
1492
Guido van Rossum25831651993-05-19 14:50:45 +00001493#define strequ(a, b) (strcmp((a), (b)) == 0)
1494
1495static void
1496com_access_stmt(c, n)
1497 struct compiling *c;
1498 node *n;
1499{
1500 int i, j, k, mode, imode;
1501 object *vmode;
1502 REQ(n, access_stmt);
1503 /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)*
1504 accesstype: NAME+ */
1505
1506 /* Find where the colon is */
1507 i = 1;
1508 while (TYPE(CHILD(n,i-1)) != COLON)
1509 i += 1;
1510
1511 /* Calculate the mode mask */
1512 mode = 0;
1513 for (j = i; j < NCH(n); j += 2) {
Guido van Rossum81daa321993-05-20 14:24:46 +00001514 int r = 0, w = 0, p = 0;
1515 for (k = 0; k < NCH(CHILD(n,j)); k++) {
Guido van Rossum25831651993-05-19 14:50:45 +00001516 if (strequ(STR(CHILD(CHILD(n,j),k)), "public"))
1517 p = 0;
1518 else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected"))
1519 p = 1;
1520 else if (strequ(STR(CHILD(CHILD(n,j),k)), "private"))
1521 p = 2;
1522 else if (strequ(STR(CHILD(CHILD(n,j),k)), "read"))
1523 r = 1;
1524 else if (strequ(STR(CHILD(CHILD(n,j),k)), "write"))
1525 w = 1;
1526 else /* XXX should make this an exception */
1527 fprintf(stderr, "bad access type %s\n",
1528 STR(CHILD(CHILD(n,j),k)));
1529 }
1530 if (r == 0 && w == 0)
Guido van Rossum81daa321993-05-20 14:24:46 +00001531 r = w = 1;
Guido van Rossum25831651993-05-19 14:50:45 +00001532 if (p == 0) {
1533 if (r == 1) mode |= AC_R_PUBLIC;
1534 if (w == 1) mode |= AC_W_PUBLIC;
1535 } else if (p == 1) {
1536 if (r == 1) mode |= AC_R_PROTECTED;
1537 if (w == 1) mode |= AC_W_PROTECTED;
1538 } else {
1539 if (r == 1) mode |= AC_R_PRIVATE;
1540 if (w == 1) mode |= AC_W_PRIVATE;
1541 }
1542 }
1543 vmode = newintobject((long)mode);
1544 imode = com_addconst(c, vmode);
1545 XDECREF(vmode);
1546 for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) {
1547 com_addoparg(c, LOAD_CONST, imode);
1548 com_addopname(c, ACCESS_MODE, CHILD(n, i));
1549 }
1550}
1551
Guido van Rossumc5e96291991-12-10 13:53:51 +00001552static void
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001553com_exec_stmt(c, n)
1554 struct compiling *c;
1555 node *n;
1556{
1557 REQ(n, exec_stmt);
1558 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
1559 com_node(c, CHILD(n, 1));
1560 if (NCH(n) >= 4)
1561 com_node(c, CHILD(n, 3));
1562 else
1563 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1564 if (NCH(n) >= 6)
1565 com_node(c, CHILD(n, 5));
1566 else
1567 com_addbyte(c, DUP_TOP);
1568 com_addbyte(c, EXEC_STMT);
1569}
1570
1571static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001572com_if_stmt(c, n)
1573 struct compiling *c;
1574 node *n;
1575{
1576 int i;
1577 int anchor = 0;
1578 REQ(n, if_stmt);
1579 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
1580 for (i = 0; i+3 < NCH(n); i+=4) {
1581 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001582 node *ch = CHILD(n, i+1);
1583 if (i > 0)
1584 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001585 com_node(c, CHILD(n, i+1));
1586 com_addfwref(c, JUMP_IF_FALSE, &a);
1587 com_addbyte(c, POP_TOP);
1588 com_node(c, CHILD(n, i+3));
1589 com_addfwref(c, JUMP_FORWARD, &anchor);
1590 com_backpatch(c, a);
1591 com_addbyte(c, POP_TOP);
1592 }
1593 if (i+2 < NCH(n))
1594 com_node(c, CHILD(n, i+2));
1595 com_backpatch(c, anchor);
1596}
1597
1598static void
1599com_while_stmt(c, n)
1600 struct compiling *c;
1601 node *n;
1602{
1603 int break_anchor = 0;
1604 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001605 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001606 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
1607 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001608 block_push(c, SETUP_LOOP);
1609 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001610 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001611 com_node(c, CHILD(n, 1));
1612 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1613 com_addbyte(c, POP_TOP);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001614 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001615 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001616 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001617 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1618 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001619 com_backpatch(c, anchor);
1620 com_addbyte(c, POP_TOP);
1621 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001622 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001623 if (NCH(n) > 4)
1624 com_node(c, CHILD(n, 6));
1625 com_backpatch(c, break_anchor);
1626}
1627
1628static void
1629com_for_stmt(c, n)
1630 struct compiling *c;
1631 node *n;
1632{
1633 object *v;
1634 int break_anchor = 0;
1635 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001636 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001637 REQ(n, for_stmt);
1638 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
1639 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001640 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001641 com_node(c, CHILD(n, 3));
1642 v = newintobject(0L);
1643 if (v == NULL)
1644 c->c_errors++;
1645 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1646 XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001647 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001648 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001649 com_addfwref(c, FOR_LOOP, &anchor);
1650 com_assign(c, CHILD(n, 1), 1/*assigning*/);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001651 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001652 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001653 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001654 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1655 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001656 com_backpatch(c, anchor);
1657 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001658 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001659 if (NCH(n) > 8)
1660 com_node(c, CHILD(n, 8));
1661 com_backpatch(c, break_anchor);
1662}
1663
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001664/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001665
1666 SETUP_FINALLY L
1667 <code for S>
1668 POP_BLOCK
1669 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001670 L: <code for Sf>
1671 END_FINALLY
1672
1673 The special instructions use the block stack. Each block
1674 stack entry contains the instruction that created it (here
1675 SETUP_FINALLY), the level of the value stack at the time the
1676 block stack entry was created, and a label (here L).
1677
1678 SETUP_FINALLY:
1679 Pushes the current value stack level and the label
1680 onto the block stack.
1681 POP_BLOCK:
1682 Pops en entry from the block stack, and pops the value
1683 stack until its level is the same as indicated on the
1684 block stack. (The label is ignored.)
1685 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001686 Pops a variable number of entries from the *value* stack
1687 and re-raises the exception they specify. The number of
1688 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001689
1690 The block stack is unwound when an exception is raised:
1691 when a SETUP_FINALLY entry is found, the exception is pushed
1692 onto the value stack (and the exception condition is cleared),
1693 and the interpreter jumps to the label gotten from the block
1694 stack.
1695
1696 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00001697 (The contents of the value stack is shown in [], with the top
1698 at the right; 'tb' is trace-back info, 'val' the exception's
1699 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001700
1701 Value stack Label Instruction Argument
1702 [] SETUP_EXCEPT L1
1703 [] <code for S>
1704 [] POP_BLOCK
1705 [] JUMP_FORWARD L0
1706
Guido van Rossum3f5da241990-12-20 15:06:42 +00001707 [tb, val, exc] L1: DUP )
1708 [tb, val, exc, exc] <evaluate E1> )
1709 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1710 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1711 [tb, val, exc, 1] POP )
1712 [tb, val, exc] POP
1713 [tb, val] <assign to V1> (or POP if no V1)
1714 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001715 [] <code for S1>
1716 JUMP_FORWARD L0
1717
Guido van Rossum3f5da241990-12-20 15:06:42 +00001718 [tb, val, exc, 0] L2: POP
1719 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001720 .............................etc.......................
1721
Guido van Rossum3f5da241990-12-20 15:06:42 +00001722 [tb, val, exc, 0] Ln+1: POP
1723 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001724
1725 [] L0: <next statement>
1726
1727 Of course, parts are not generated if Vi or Ei is not present.
1728*/
1729
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001730static void
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001731com_try_except(c, n)
1732 struct compiling *c;
1733 node *n;
1734{
1735 int except_anchor = 0;
1736 int end_anchor = 0;
1737 int else_anchor = 0;
1738 int i;
1739 node *ch;
1740
1741 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
1742 block_push(c, SETUP_EXCEPT);
1743 com_node(c, CHILD(n, 2));
1744 com_addbyte(c, POP_BLOCK);
1745 block_pop(c, SETUP_EXCEPT);
1746 com_addfwref(c, JUMP_FORWARD, &else_anchor);
1747 com_backpatch(c, except_anchor);
1748 for (i = 3;
1749 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
1750 i += 3) {
1751 /* except_clause: 'except' [expr [',' expr]] */
1752 if (except_anchor == 0) {
1753 err_setstr(SyntaxError,
1754 "default 'except:' must be last");
1755 c->c_errors++;
1756 break;
1757 }
1758 except_anchor = 0;
1759 com_addoparg(c, SET_LINENO, ch->n_lineno);
1760 if (NCH(ch) > 1) {
1761 com_addbyte(c, DUP_TOP);
1762 com_node(c, CHILD(ch, 1));
1763 com_addoparg(c, COMPARE_OP, EXC_MATCH);
1764 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
1765 com_addbyte(c, POP_TOP);
1766 }
1767 com_addbyte(c, POP_TOP);
1768 if (NCH(ch) > 3)
1769 com_assign(c, CHILD(ch, 3), 1/*assigning*/);
1770 else
1771 com_addbyte(c, POP_TOP);
1772 com_addbyte(c, POP_TOP);
1773 com_node(c, CHILD(n, i+2));
1774 com_addfwref(c, JUMP_FORWARD, &end_anchor);
1775 if (except_anchor) {
1776 com_backpatch(c, except_anchor);
1777 com_addbyte(c, POP_TOP);
1778 }
1779 }
1780 com_addbyte(c, END_FINALLY);
1781 com_backpatch(c, else_anchor);
1782 if (i < NCH(n))
1783 com_node(c, CHILD(n, i+2));
1784 com_backpatch(c, end_anchor);
1785}
1786
1787static void
1788com_try_finally(c, n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001789 struct compiling *c;
1790 node *n;
1791{
1792 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001793 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00001794
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001795 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
1796 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001797 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001798 com_addbyte(c, POP_BLOCK);
1799 block_pop(c, SETUP_FINALLY);
1800 block_push(c, END_FINALLY);
1801 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1802 com_backpatch(c, finally_anchor);
1803 ch = CHILD(n, NCH(n)-1);
1804 com_addoparg(c, SET_LINENO, ch->n_lineno);
1805 com_node(c, ch);
1806 com_addbyte(c, END_FINALLY);
1807 block_pop(c, END_FINALLY);
1808}
1809
1810static void
1811com_try_stmt(c, n)
1812 struct compiling *c;
1813 node *n;
1814{
1815 REQ(n, try_stmt);
1816 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1817 | 'try' ':' suite 'finally' ':' suite */
1818 if (TYPE(CHILD(n, 3)) != except_clause)
1819 com_try_finally(c, n);
1820 else
1821 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001822}
1823
1824static void
1825com_suite(c, n)
1826 struct compiling *c;
1827 node *n;
1828{
1829 REQ(n, suite);
1830 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
1831 if (NCH(n) == 1) {
1832 com_node(c, CHILD(n, 0));
1833 }
1834 else {
1835 int i;
1836 for (i = 0; i < NCH(n); i++) {
1837 node *ch = CHILD(n, i);
1838 if (TYPE(ch) == stmt)
1839 com_node(c, ch);
1840 }
1841 }
1842}
1843
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001844/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001845static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001846com_continue_stmt(c, n)
1847 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001848 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001849{
1850 int i = c->c_nblocks;
1851 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
1852 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1853 }
1854 else {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001855 err_setstr(SyntaxError, "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001856 c->c_errors++;
1857 }
1858 /* XXX Could allow it inside a 'finally' clause
1859 XXX if we could pop the exception still on the stack */
1860}
1861
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001862static int
1863com_argdefs(c, n, argcount_return)
1864 struct compiling *c;
1865 node *n;
1866 int *argcount_return;
1867{
1868 int i, nch, nargs, ndefs, star;
1869 if (TYPE(n) == lambdef) {
1870 /* lambdef: 'lambda' [varargslist] ':' test */
1871 n = CHILD(n, 1);
1872 }
1873 else {
1874 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
1875 n = CHILD(n, 2);
1876 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
1877 n = CHILD(n, 1);
1878 }
1879 if (TYPE(n) != varargslist)
1880 return -1;
1881 /* varargslist:
1882 (fpdef ['=' test] ',')* '*' NAME |
1883 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
1884 nch = NCH(n);
1885 if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) {
1886 star = 1;
1887 nch -= 2;
1888 }
1889 else
1890 star = 0;
1891 nargs = 0;
1892 ndefs = 0;
1893 for (i = 0; i < nch; i++) {
1894 int t;
1895 nargs++;
1896 i++;
1897 if (i >= nch)
1898 break;
1899 t = TYPE(CHILD(n, i));
1900 if (t == EQUAL) {
1901 i++;
1902 ndefs++;
1903 com_node(c, CHILD(n, i));
1904 i++;
1905 if (i >= nch)
1906 break;
1907 t = TYPE(CHILD(n, i));
1908 }
1909 else {
1910 /* Treat "(a=1, b)" as "(a=1, b=None)" */
1911 if (ndefs) {
1912 com_addoparg(c, LOAD_CONST,
1913 com_addconst(c, None));
1914 ndefs++;
1915 }
1916 }
1917 if (t != COMMA)
1918 break;
1919 }
1920 if (star)
1921 nargs ^= 0x4000;
1922 *argcount_return = nargs;
1923 if (ndefs > 0)
1924 com_addoparg(c, BUILD_TUPLE, ndefs);
1925 return ndefs;
1926}
1927
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001928static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001929com_funcdef(c, n)
1930 struct compiling *c;
1931 node *n;
1932{
1933 object *v;
1934 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum590baa41993-11-30 13:40:46 +00001935 v = (object *)compile(n, c->c_filename);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001936 if (v == NULL)
1937 c->c_errors++;
1938 else {
1939 int i = com_addconst(c, v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001940 int argcount;
1941 int ndefs = com_argdefs(c, n, &argcount);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001942 com_addoparg(c, LOAD_CONST, i);
1943 com_addbyte(c, BUILD_FUNCTION);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001944 if (ndefs > 0)
1945 com_addoparg(c, SET_FUNC_ARGS, argcount);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001946 com_addopname(c, STORE_NAME, CHILD(n, 1));
1947 DECREF(v);
1948 }
1949}
1950
1951static void
Guido van Rossum25831651993-05-19 14:50:45 +00001952com_bases(c, n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00001953 struct compiling *c;
1954 node *n;
1955{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001956 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001957 REQ(n, testlist);
1958 /* testlist: test (',' test)* [','] */
1959 for (i = 0; i < NCH(n); i += 2)
1960 com_node(c, CHILD(n, i));
1961 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2);
1962}
1963
1964static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001965com_classdef(c, n)
1966 struct compiling *c;
1967 node *n;
1968{
Guido van Rossum25831651993-05-19 14:50:45 +00001969 int i;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001970 object *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001971 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00001972 /* classdef: class NAME ['(' testlist ')'] ':' suite */
1973 if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) {
1974 c->c_errors++;
1975 return;
1976 }
1977 /* Push the class name on the stack */
1978 i = com_addconst(c, v);
1979 com_addoparg(c, LOAD_CONST, i);
1980 DECREF(v);
1981 /* Push the tuple of base classes on the stack */
1982 if (TYPE(CHILD(n, 2)) != LPAR)
Guido van Rossumc5e96291991-12-10 13:53:51 +00001983 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum25831651993-05-19 14:50:45 +00001984 else
1985 com_bases(c, CHILD(n, 3));
Guido van Rossum590baa41993-11-30 13:40:46 +00001986 v = (object *)compile(n, c->c_filename);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001987 if (v == NULL)
1988 c->c_errors++;
1989 else {
Guido van Rossum25831651993-05-19 14:50:45 +00001990 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001991 com_addoparg(c, LOAD_CONST, i);
1992 com_addbyte(c, BUILD_FUNCTION);
1993 com_addbyte(c, UNARY_CALL);
1994 com_addbyte(c, BUILD_CLASS);
1995 com_addopname(c, STORE_NAME, CHILD(n, 1));
1996 DECREF(v);
1997 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001998}
1999
2000static void
2001com_node(c, n)
2002 struct compiling *c;
2003 node *n;
2004{
2005 switch (TYPE(n)) {
2006
2007 /* Definition nodes */
2008
2009 case funcdef:
2010 com_funcdef(c, n);
2011 break;
2012 case classdef:
2013 com_classdef(c, n);
2014 break;
2015
2016 /* Trivial parse tree nodes */
2017
2018 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002019 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002020 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002021 com_node(c, CHILD(n, 0));
2022 break;
2023
2024 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002025 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2026 com_addoparg(c, SET_LINENO, n->n_lineno);
2027 {
2028 int i;
2029 for (i = 0; i < NCH(n)-1; i += 2)
2030 com_node(c, CHILD(n, i));
2031 }
2032 break;
2033
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002034 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002035 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002036 com_node(c, CHILD(n, 0));
2037 break;
2038
2039 /* Statement nodes */
2040
2041 case expr_stmt:
2042 com_expr_stmt(c, n);
2043 break;
2044 case print_stmt:
2045 com_print_stmt(c, n);
2046 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002047 case del_stmt: /* 'del' exprlist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002048 com_assign(c, CHILD(n, 1), 0/*delete*/);
2049 break;
2050 case pass_stmt:
2051 break;
2052 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002053 if (c->c_loops == 0) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00002054 err_setstr(SyntaxError, "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002055 c->c_errors++;
2056 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002057 com_addbyte(c, BREAK_LOOP);
2058 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002059 case continue_stmt:
2060 com_continue_stmt(c, n);
2061 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002062 case return_stmt:
2063 com_return_stmt(c, n);
2064 break;
2065 case raise_stmt:
2066 com_raise_stmt(c, n);
2067 break;
2068 case import_stmt:
2069 com_import_stmt(c, n);
2070 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002071 case global_stmt:
2072 com_global_stmt(c, n);
2073 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002074 case access_stmt:
2075 com_access_stmt(c, n);
2076 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002077 case exec_stmt:
2078 com_exec_stmt(c, n);
2079 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002080 case if_stmt:
2081 com_if_stmt(c, n);
2082 break;
2083 case while_stmt:
2084 com_while_stmt(c, n);
2085 break;
2086 case for_stmt:
2087 com_for_stmt(c, n);
2088 break;
2089 case try_stmt:
2090 com_try_stmt(c, n);
2091 break;
2092 case suite:
2093 com_suite(c, n);
2094 break;
2095
2096 /* Expression nodes */
2097
2098 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002099 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002100 break;
2101 case test:
2102 com_test(c, n);
2103 break;
2104 case and_test:
2105 com_and_test(c, n);
2106 break;
2107 case not_test:
2108 com_not_test(c, n);
2109 break;
2110 case comparison:
2111 com_comparison(c, n);
2112 break;
2113 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002114 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002115 break;
2116 case expr:
2117 com_expr(c, n);
2118 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00002119 case xor_expr:
2120 com_xor_expr(c, n);
2121 break;
2122 case and_expr:
2123 com_and_expr(c, n);
2124 break;
2125 case shift_expr:
2126 com_shift_expr(c, n);
2127 break;
2128 case arith_expr:
2129 com_arith_expr(c, n);
2130 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002131 case term:
2132 com_term(c, n);
2133 break;
2134 case factor:
2135 com_factor(c, n);
2136 break;
2137 case atom:
2138 com_atom(c, n);
2139 break;
2140
2141 default:
2142 fprintf(stderr, "node type %d\n", TYPE(n));
2143 err_setstr(SystemError, "com_node: unexpected node type");
2144 c->c_errors++;
2145 }
2146}
2147
2148static void com_fplist PROTO((struct compiling *, node *));
2149
2150static void
2151com_fpdef(c, n)
2152 struct compiling *c;
2153 node *n;
2154{
2155 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
2156 if (TYPE(CHILD(n, 0)) == LPAR)
2157 com_fplist(c, CHILD(n, 1));
2158 else
2159 com_addopname(c, STORE_NAME, CHILD(n, 0));
2160}
2161
2162static void
2163com_fplist(c, n)
2164 struct compiling *c;
2165 node *n;
2166{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00002167 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002168 if (NCH(n) == 1) {
2169 com_fpdef(c, CHILD(n, 0));
2170 }
2171 else {
2172 int i;
2173 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
2174 for (i = 0; i < NCH(n); i += 2)
2175 com_fpdef(c, CHILD(n, i));
2176 }
2177}
2178
2179static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002180com_arglist(c, n)
2181 struct compiling *c;
2182 node *n;
2183{
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002184 int nch, op, nargs, i, t;
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002185 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00002186 /* varargslist:
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002187 (fpdef ['=' test] ',')* '*' NAME |
2188 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2189 nch = NCH(n);
2190 if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) {
2191 op = UNPACK_VARARG;
2192 nch -= 2;
2193 }
2194 else
2195 op = UNPACK_ARG;
2196 nargs = 0;
2197 for (i = 0; i < nch; i++) {
2198 nargs++;
2199 i++;
2200 if (i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002201 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002202 t = TYPE(CHILD(n, i));
2203 if (t == EQUAL) {
2204 i += 2;
2205 if (i >= nch)
2206 break;
2207 t = TYPE(CHILD(n, i));
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002208 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002209 if (t != COMMA)
2210 break;
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002211 }
2212 com_addoparg(c, op, nargs);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002213 for (i = 0; i < nch; i++) {
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002214 com_fpdef(c, CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002215 i++;
2216 if (i >= nch)
2217 break;
2218 t = TYPE(CHILD(n, i));
2219 if (t == EQUAL) {
2220 i += 2;
2221 if (i >= nch)
2222 break;
2223 t = TYPE(CHILD(n, i));
2224 }
2225 if (t != COMMA)
2226 break;
2227 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002228 if (op == UNPACK_VARARG)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002229 com_addopname(c, STORE_NAME, CHILD(n, nch+1));
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002230}
2231
2232static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002233com_file_input(c, n)
2234 struct compiling *c;
2235 node *n;
2236{
2237 int i;
2238 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
2239 for (i = 0; i < NCH(n); i++) {
2240 node *ch = CHILD(n, i);
2241 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
2242 com_node(c, ch);
2243 }
2244}
2245
2246/* Top-level compile-node interface */
2247
2248static void
2249compile_funcdef(c, n)
2250 struct compiling *c;
2251 node *n;
2252{
2253 node *ch;
2254 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002255 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002256 com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); /* Patched! */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002257 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
2258 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002259 if (TYPE(ch) == RPAR)
Guido van Rossum288a60f1991-12-16 13:05:10 +00002260 com_addoparg(c, UNPACK_ARG, 0);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002261 else
2262 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002263 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002264 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002265 c->c_infunction = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002266 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2267 com_addbyte(c, RETURN_VALUE);
2268}
2269
2270static void
Guido van Rossum590baa41993-11-30 13:40:46 +00002271compile_lambdef(c, n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002272 struct compiling *c;
2273 node *n;
2274{
Guido van Rossum590baa41993-11-30 13:40:46 +00002275 node *ch;
2276 REQ(n, lambdef); /* lambdef: 'lambda' [parameters] ':' test */
2277 c->c_name = NULL;
2278
2279 ch = CHILD(n, 1);
2280 if (TYPE(ch) == COLON) {
2281 com_addoparg(c, UNPACK_ARG, 0);
2282 com_node(c, CHILD(n, 2));
2283 }
2284 else {
2285 com_addoparg(c, RESERVE_FAST, com_addconst(c, None));
2286 com_arglist(c, ch);
2287 com_node(c, CHILD(n, 3));
2288 }
2289
Guido van Rossum12d12c51993-10-26 17:58:25 +00002290 com_addbyte(c, RETURN_VALUE);
2291}
2292
2293static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002294compile_node(c, n)
2295 struct compiling *c;
2296 node *n;
2297{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002298 com_addoparg(c, SET_LINENO, n->n_lineno);
2299
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002300 switch (TYPE(n)) {
2301
Guido van Rossum4c417781991-01-21 16:09:22 +00002302 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002303 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002304 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002305 n = CHILD(n, 0);
2306 if (TYPE(n) != NEWLINE)
2307 com_node(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002308 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2309 com_addbyte(c, RETURN_VALUE);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002310 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002311 break;
2312
Guido van Rossum4c417781991-01-21 16:09:22 +00002313 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002314 com_file_input(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002315 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2316 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002317 break;
2318
Guido van Rossum590baa41993-11-30 13:40:46 +00002319 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00002320 com_node(c, CHILD(n, 0));
2321 com_addbyte(c, RETURN_VALUE);
2322 break;
2323
Guido van Rossum590baa41993-11-30 13:40:46 +00002324 case lambdef: /* anonymous function definition */
2325 compile_lambdef(c, n);
2326 break;
2327
Guido van Rossum4c417781991-01-21 16:09:22 +00002328 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002329 compile_funcdef(c, n);
2330 break;
2331
Guido van Rossum4c417781991-01-21 16:09:22 +00002332 case classdef: /* A class definition */
Guido van Rossum25831651993-05-19 14:50:45 +00002333 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2334 c->c_name = STR(CHILD(n, 1));
Guido van Rossumc5e96291991-12-10 13:53:51 +00002335 com_node(c, CHILD(n, NCH(n)-1)); /* The suite */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002336 com_addbyte(c, LOAD_LOCALS);
2337 com_addbyte(c, RETURN_VALUE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002338 break;
2339
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002340 default:
2341 fprintf(stderr, "node type %d\n", TYPE(n));
2342 err_setstr(SystemError, "compile_node: unexpected node type");
2343 c->c_errors++;
2344 }
2345}
2346
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002347/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00002348
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002349 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
2350 instructions that refer to local variables with LOAD_FAST etc.
2351 The latter instructions are much faster because they don't need to
2352 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002353
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002354 To find all local variables, we check all STORE_NAME, IMPORT_FROM and
2355 DELETE_NAME instructions. This yields all local variables, including
2356 arguments, function definitions, class definitions and import
2357 statements.
2358
2359 All remaining LOAD_NAME instructions must refer to non-local (global
2360 or builtin) variables, so are replaced by LOAD_GLOBAL.
2361
2362 There are two problems: 'from foo import *' and 'exec' may introduce
2363 local variables that we can't know while compiling. If this is the
2364 case, we don't optimize at all (this rarely happens, since exec is
2365 rare, & this form of import statement is mostly used at the module
2366 level).
2367
Guido van Rossum282914b1991-04-04 10:42:56 +00002368 NB: this modifies the string object co->co_code!
2369*/
2370
2371static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002372optimize(c)
2373 struct compiling *c;
Guido van Rossum282914b1991-04-04 10:42:56 +00002374{
Guido van Rossum0a697f61991-04-16 08:39:12 +00002375 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00002376 object *locals;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002377 int nlocals;
Guido van Rossum282914b1991-04-04 10:42:56 +00002378 int opcode;
2379 int oparg;
2380 object *name;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002381 int fast_reserved;
2382 object *error_type, *error_value;
2383
Guido van Rossum282914b1991-04-04 10:42:56 +00002384#define NEXTOP() (*next_instr++)
2385#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
2386#define GETITEM(v, i) (getlistitem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002387#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
2388
Guido van Rossum282914b1991-04-04 10:42:56 +00002389 locals = newdictobject();
2390 if (locals == NULL) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002391 c->c_errors++;
2392 return;
Guido van Rossum282914b1991-04-04 10:42:56 +00002393 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002394 nlocals = 0;
2395
2396 err_get(&error_type, &error_value);
Guido van Rossum282914b1991-04-04 10:42:56 +00002397
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002398 next_instr = (unsigned char *) getstringvalue(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002399 for (;;) {
2400 opcode = NEXTOP();
2401 if (opcode == STOP_CODE)
2402 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002403 if (opcode == EXEC_STMT)
2404 goto end; /* Don't optimize if exec present */
Guido van Rossum282914b1991-04-04 10:42:56 +00002405 if (HAS_ARG(opcode))
2406 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002407 if (opcode == STORE_NAME || opcode == DELETE_NAME ||
2408 opcode == IMPORT_FROM) {
2409 object *v;
Guido van Rossum282914b1991-04-04 10:42:56 +00002410 name = GETNAMEOBJ(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002411 if (dict2lookup(locals, name) != NULL)
2412 continue;
2413 err_clear();
2414 v = newintobject(nlocals);
2415 if (v == NULL) {
2416 c->c_errors++;
2417 goto err;
Guido van Rossum282914b1991-04-04 10:42:56 +00002418 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002419 nlocals++;
2420 if (dict2insert(locals, name, v) != 0) {
2421 DECREF(v);
2422 c->c_errors++;
2423 goto err;
2424 }
2425 DECREF(v);
Guido van Rossum282914b1991-04-04 10:42:56 +00002426 }
2427 }
2428
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002429 if (dictlookup(locals, "*") != NULL) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002430 /* Don't optimize anything */
2431 goto end;
2432 }
2433
2434 next_instr = (unsigned char *) getstringvalue(c->c_code);
2435 fast_reserved = 0;
Guido van Rossum282914b1991-04-04 10:42:56 +00002436 for (;;) {
2437 cur_instr = next_instr;
2438 opcode = NEXTOP();
2439 if (opcode == STOP_CODE)
2440 break;
2441 if (HAS_ARG(opcode))
2442 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002443 if (opcode == RESERVE_FAST) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002444 int i;
2445 object *localmap = newtupleobject(nlocals);
2446 int pos;
2447 object *key, *value;
2448 if (localmap == NULL) { /* XXX mask error */
2449 err_clear();
2450 continue;
2451 }
2452 pos = 0;
2453 while (mappinggetnext(locals, &pos, &key, &value)) {
2454 int j;
2455 if (!is_intobject(value))
2456 continue;
2457 j = getintvalue(value);
2458 if (0 <= j && j < nlocals) {
2459 INCREF(key);
2460 settupleitem(localmap, j, key);
2461 }
2462 }
2463 i = com_addconst(c, localmap);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002464 cur_instr[1] = i & 0xff;
2465 cur_instr[2] = (i>>8) & 0xff;
2466 fast_reserved = 1;
2467 continue;
2468 }
2469 if (!fast_reserved)
2470 continue;
2471 if (opcode == LOAD_NAME ||
2472 opcode == STORE_NAME ||
2473 opcode == DELETE_NAME) {
2474 object *v;
2475 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00002476 name = GETNAMEOBJ(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002477 v = dict2lookup(locals, name);
2478 if (v == NULL) {
Guido van Rossum83163251991-08-16 08:58:43 +00002479 err_clear();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002480 if (opcode == LOAD_NAME)
2481 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002482 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00002483 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002484 i = getintvalue(v);
2485 switch (opcode) {
2486 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
2487 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
2488 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
2489 }
2490 cur_instr[1] = i & 0xff;
2491 cur_instr[2] = (i>>8) & 0xff;
Guido van Rossum282914b1991-04-04 10:42:56 +00002492 }
2493 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002494
2495 end:
2496 err_setval(error_type, error_value);
2497 err:
Guido van Rossum282914b1991-04-04 10:42:56 +00002498 DECREF(locals);
2499}
2500
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002501codeobject *
Guido van Rossum590baa41993-11-30 13:40:46 +00002502compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002503 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002504 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002505{
2506 struct compiling sc;
2507 codeobject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00002508 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002509 return NULL;
2510 compile_node(&sc, n);
2511 com_done(&sc);
Guido van Rossum590baa41993-11-30 13:40:46 +00002512 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0)
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002513 optimize(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002514 co = NULL;
2515 if (sc.c_errors == 0) {
2516 object *v, *w;
2517 v = newstringobject(sc.c_filename);
Guido van Rossum590baa41993-11-30 13:40:46 +00002518 if (sc.c_name)
2519 w = newstringobject(sc.c_name);
2520 else {
2521 INCREF(None);
2522 w = None;
2523 }
Guido van Rossum9bfef441993-03-29 10:43:31 +00002524 if (v != NULL && w != NULL)
2525 co = newcodeobject(sc.c_code, sc.c_consts,
2526 sc.c_names, v, w);
2527 XDECREF(v);
2528 XDECREF(w);
Guido van Rossuma082ce41991-06-04 19:41:56 +00002529 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002530 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002531 return co;
2532}