blob: c8ce0aacaca938436178552d4d2295d071be96d7 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
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'
30 XXX Include function name in code (and module names?)
31*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032
Guido van Rossum3f5da241990-12-20 15:06:42 +000033#include "allobjects.h"
34
Guido van Rossum10dc2e81990-11-18 17:27:39 +000035#include "node.h"
36#include "token.h"
37#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000038#include "compile.h"
39#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000040#include "structmember.h"
41
42#include <ctype.h>
43
Guido van Rossum282914b1991-04-04 10:42:56 +000044extern int errno;
45
Guido van Rossum3f5da241990-12-20 15:06:42 +000046#define OFF(x) offsetof(codeobject, x)
47
48static struct memberlist code_memberlist[] = {
Guido van Rossum39d942d1992-01-12 02:30:05 +000049 {"co_code", T_OBJECT, OFF(co_code), READONLY},
50 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
51 {"co_names", T_OBJECT, OFF(co_names), READONLY},
52 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000053 {NULL} /* Sentinel */
54};
55
56static object *
57code_getattr(co, name)
58 codeobject *co;
59 char *name;
60{
61 return getmember((char *)co, code_memberlist, name);
62}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000063
64static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000065code_dealloc(co)
66 codeobject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +000067{
Guido van Rossum3f5da241990-12-20 15:06:42 +000068 XDECREF(co->co_code);
69 XDECREF(co->co_consts);
70 XDECREF(co->co_names);
71 XDECREF(co->co_filename);
72 DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +000073}
74
75typeobject Codetype = {
76 OB_HEAD_INIT(&Typetype)
77 0,
78 "code",
79 sizeof(codeobject),
80 0,
81 code_dealloc, /*tp_dealloc*/
82 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000083 code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084 0, /*tp_setattr*/
85 0, /*tp_compare*/
86 0, /*tp_repr*/
87 0, /*tp_as_number*/
88 0, /*tp_as_sequence*/
89 0, /*tp_as_mapping*/
90};
91
Guido van Rossuma082ce41991-06-04 19:41:56 +000092codeobject *
Guido van Rossum3f5da241990-12-20 15:06:42 +000093newcodeobject(code, consts, names, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +000094 object *code;
95 object *consts;
96 object *names;
Guido van Rossuma082ce41991-06-04 19:41:56 +000097 object *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +000098{
99 codeobject *co;
100 int i;
101 /* Check argument types */
102 if (code == NULL || !is_stringobject(code) ||
103 consts == NULL || !is_listobject(consts) ||
104 names == NULL || !is_listobject(names)) {
105 err_badcall();
106 return NULL;
107 }
108 /* Make sure the list of names contains only strings */
109 for (i = getlistsize(names); --i >= 0; ) {
110 object *v = getlistitem(names, i);
111 if (v == NULL || !is_stringobject(v)) {
112 err_badcall();
113 return NULL;
114 }
115 }
116 co = NEWOBJ(codeobject, &Codetype);
117 if (co != NULL) {
118 INCREF(code);
119 co->co_code = (stringobject *)code;
120 INCREF(consts);
121 co->co_consts = consts;
122 INCREF(names);
123 co->co_names = names;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000124 INCREF(filename);
125 co->co_filename = filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000126 }
127 return co;
128}
129
130
131/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000132
133#define MAXBLOCKS 20 /* Max static block nesting within a function */
134
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000135struct compiling {
136 object *c_code; /* string */
137 object *c_consts; /* list of objects */
138 object *c_names; /* list of strings (names) */
Guido van Rossumc5e96291991-12-10 13:53:51 +0000139 object *c_globals; /* dictionary */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000140 int c_nexti; /* index into c_code */
141 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142 int c_infunction; /* set when compiling a function */
143 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000144 int c_begin; /* begin of current loop, for 'continue' */
145 int c_block[MAXBLOCKS]; /* stack of block types */
146 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147 char *c_filename; /* filename of current node */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000148};
149
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000150
151/* Interface to the block stack */
152
153static void
154block_push(c, type)
155 struct compiling *c;
156 int type;
157{
158 if (c->c_nblocks >= MAXBLOCKS) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +0000159 err_setstr(SystemError, "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000160 c->c_errors++;
161 }
162 else {
163 c->c_block[c->c_nblocks++] = type;
164 }
165}
166
167static void
168block_pop(c, type)
169 struct compiling *c;
170 int type;
171{
172 if (c->c_nblocks > 0)
173 c->c_nblocks--;
174 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
175 err_setstr(SystemError, "bad block pop");
176 c->c_errors++;
177 }
178}
179
180
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000181/* Prototypes */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000182
Guido van Rossum3f5da241990-12-20 15:06:42 +0000183static int com_init PROTO((struct compiling *, char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000184static void com_free PROTO((struct compiling *));
185static void com_done PROTO((struct compiling *));
186static void com_node PROTO((struct compiling *, struct _node *));
187static void com_addbyte PROTO((struct compiling *, int));
188static void com_addint PROTO((struct compiling *, int));
189static void com_addoparg PROTO((struct compiling *, int, int));
190static void com_addfwref PROTO((struct compiling *, int, int *));
191static void com_backpatch PROTO((struct compiling *, int));
192static int com_add PROTO((struct compiling *, object *, object *));
193static int com_addconst PROTO((struct compiling *, object *));
194static int com_addname PROTO((struct compiling *, object *));
195static void com_addopname PROTO((struct compiling *, int, node *));
Guido van Rossum288a60f1991-12-16 13:05:10 +0000196static void com_list PROTO((struct compiling *, node *, int));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000197
198static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000199com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000200 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000201 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000202{
Guido van Rossum62d46241991-04-03 19:00:23 +0000203 if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000204 goto fail_3;
205 if ((c->c_consts = newlistobject(0)) == NULL)
206 goto fail_2;
207 if ((c->c_names = newlistobject(0)) == NULL)
208 goto fail_1;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000209 if ((c->c_globals = newdictobject()) == NULL)
210 goto fail_0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000211 c->c_nexti = 0;
212 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000213 c->c_infunction = 0;
214 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000215 c->c_begin = 0;
216 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000217 c->c_filename = filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000218 return 1;
219
Guido van Rossumc5e96291991-12-10 13:53:51 +0000220 fail_0:
221 DECREF(c->c_names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000222 fail_1:
223 DECREF(c->c_consts);
224 fail_2:
225 DECREF(c->c_code);
226 fail_3:
227 return 0;
228}
229
230static void
231com_free(c)
232 struct compiling *c;
233{
234 XDECREF(c->c_code);
235 XDECREF(c->c_consts);
236 XDECREF(c->c_names);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000237 XDECREF(c->c_globals);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000238}
239
240static void
241com_done(c)
242 struct compiling *c;
243{
244 if (c->c_code != NULL)
245 resizestring(&c->c_code, c->c_nexti);
246}
247
248static void
249com_addbyte(c, byte)
250 struct compiling *c;
251 int byte;
252{
253 int len;
254 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000255 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000256 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
257 abort();
Guido van Rossum01cfd441991-10-20 20:12:38 +0000258 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000259 err_setstr(SystemError, "com_addbyte: byte out of range");
260 c->c_errors++;
261 }
262 if (c->c_code == NULL)
263 return;
264 len = getstringsize(c->c_code);
265 if (c->c_nexti >= len) {
266 if (resizestring(&c->c_code, len+1000) != 0) {
267 c->c_errors++;
268 return;
269 }
270 }
271 getstringvalue(c->c_code)[c->c_nexti++] = byte;
272}
273
274static void
275com_addint(c, x)
276 struct compiling *c;
277 int x;
278{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000279 com_addbyte(c, x & 0xff);
280 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000281}
282
283static void
284com_addoparg(c, op, arg)
285 struct compiling *c;
286 int op;
287 int arg;
288{
289 com_addbyte(c, op);
290 com_addint(c, arg);
291}
292
293static void
294com_addfwref(c, op, p_anchor)
295 struct compiling *c;
296 int op;
297 int *p_anchor;
298{
299 /* Compile a forward reference for backpatching */
300 int here;
301 int anchor;
302 com_addbyte(c, op);
303 here = c->c_nexti;
304 anchor = *p_anchor;
305 *p_anchor = here;
306 com_addint(c, anchor == 0 ? 0 : here - anchor);
307}
308
309static void
310com_backpatch(c, anchor)
311 struct compiling *c;
312 int anchor; /* Must be nonzero */
313{
314 unsigned char *code = (unsigned char *) getstringvalue(c->c_code);
315 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000316 int dist;
317 int prev;
318 for (;;) {
319 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000320 prev = code[anchor] + (code[anchor+1] << 8);
321 dist = target - (anchor+2);
322 code[anchor] = dist & 0xff;
323 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000324 if (!prev)
325 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000326 anchor -= prev;
327 }
328}
329
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000330/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000331
332static int
333com_add(c, list, v)
334 struct compiling *c;
335 object *list;
336 object *v;
337{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000338 int n = getlistsize(list);
339 int i;
340 for (i = n; --i >= 0; ) {
341 object *w = getlistitem(list, i);
Guido van Rossumefc0bd01991-07-01 18:44:20 +0000342 if (v->ob_type == w->ob_type && cmpobject(v, w) == 0)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000343 return i;
344 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000345 if (addlistitem(list, v) != 0)
346 c->c_errors++;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000347 return n;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000348}
349
350static int
351com_addconst(c, v)
352 struct compiling *c;
353 object *v;
354{
355 return com_add(c, c->c_consts, v);
356}
357
358static int
359com_addname(c, v)
360 struct compiling *c;
361 object *v;
362{
363 return com_add(c, c->c_names, v);
364}
365
366static void
367com_addopname(c, op, n)
368 struct compiling *c;
369 int op;
370 node *n;
371{
372 object *v;
373 int i;
374 char *name;
375 if (TYPE(n) == STAR)
376 name = "*";
377 else {
378 REQ(n, NAME);
379 name = STR(n);
380 }
381 if ((v = newstringobject(name)) == NULL) {
382 c->c_errors++;
383 i = 255;
384 }
385 else {
386 i = com_addname(c, v);
387 DECREF(v);
388 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000389 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
390 switch (op) {
391 case LOAD_NAME:
392 case STORE_NAME:
393 case DELETE_NAME:
394 if (dictlookup(c->c_globals, name) != NULL) {
395 switch (op) {
396 case LOAD_NAME: op = LOAD_GLOBAL; break;
397 case STORE_NAME: op = STORE_GLOBAL; break;
398 case DELETE_NAME: op = DELETE_GLOBAL; break;
399 }
400 }
401 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000402 com_addoparg(c, op, i);
403}
404
405static object *
406parsenumber(s)
407 char *s;
408{
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000409 extern long strtol PROTO((const char *, char **, int));
410 extern unsigned long strtoul PROTO((const char *, char **, int));
411 extern double strtod PROTO((const char *, char **));
Guido van Rossumc5e96291991-12-10 13:53:51 +0000412 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000413 long x;
Guido van Rossum282914b1991-04-04 10:42:56 +0000414 double xx;
415 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000416 end = s + strlen(s) - 1;
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000417 if (*end == 'l' || *end == 'L')
Guido van Rossumc5e96291991-12-10 13:53:51 +0000418 return long_scan(s, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000419 if (s[0] == '0')
420 x = (long) strtoul(s, &end, 0);
421 else
422 x = strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000423 if (*end == '\0') {
424 if (errno != 0) {
Guido van Rossumefb087b1991-12-16 15:41:41 +0000425 err_setstr(OverflowError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000426 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000427 return NULL;
428 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000429 return newintobject(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000430 }
431 errno = 0;
432 xx = strtod(s, &end);
433 if (*end == '\0') {
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000434#ifndef BROKEN_STRTOD
435 /* Some strtod() versions (e.g., in older SunOS systems)
436 set errno incorrectly; better to ignore overflows
437 than not to be able to use float literals at all! */
Guido van Rossum282914b1991-04-04 10:42:56 +0000438 if (errno != 0) {
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000439 err_setstr(OverflowError, "float literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000440 return NULL;
441 }
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000442#endif
Guido van Rossum282914b1991-04-04 10:42:56 +0000443 return newfloatobject(xx);
444 }
Guido van Rossumefb087b1991-12-16 15:41:41 +0000445 err_setstr(SystemError, "bad number syntax?!?!");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000446 return NULL;
447}
448
449static object *
450parsestr(s)
451 char *s;
452{
453 object *v;
454 int len;
455 char *buf;
456 char *p;
457 int c;
458 if (*s != '\'') {
459 err_badcall();
460 return NULL;
461 }
462 s++;
463 len = strlen(s);
464 if (s[--len] != '\'') {
465 err_badcall();
466 return NULL;
467 }
468 if (strchr(s, '\\') == NULL)
469 return newsizedstringobject(s, len);
470 v = newsizedstringobject((char *)NULL, len);
471 p = buf = getstringvalue(v);
472 while (*s != '\0' && *s != '\'') {
473 if (*s != '\\') {
474 *p++ = *s++;
475 continue;
476 }
477 s++;
478 switch (*s++) {
479 /* XXX This assumes ASCII! */
480 case '\\': *p++ = '\\'; break;
481 case '\'': *p++ = '\''; break;
482 case 'b': *p++ = '\b'; break;
483 case 'f': *p++ = '\014'; break; /* FF */
484 case 't': *p++ = '\t'; break;
485 case 'n': *p++ = '\n'; break;
486 case 'r': *p++ = '\r'; break;
487 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000488 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
489 case '0': case '1': case '2': case '3':
490 case '4': case '5': case '6': case '7':
491 c = s[-1] - '0';
492 if ('0' <= *s && *s <= '7') {
493 c = (c<<3) + *s++ - '0';
494 if ('0' <= *s && *s <= '7')
495 c = (c<<3) + *s++ - '0';
496 }
497 *p++ = c;
498 break;
499 case 'x':
500 if (isxdigit(*s)) {
501 sscanf(s, "%x", &c);
502 *p++ = c;
503 do {
504 s++;
505 } while (isxdigit(*s));
506 break;
507 }
508 /* FALLTHROUGH */
509 default: *p++ = '\\'; *p++ = s[-1]; break;
510 }
511 }
512 resizestring(&v, (int)(p - buf));
513 return v;
514}
515
516static void
517com_list_constructor(c, n)
518 struct compiling *c;
519 node *n;
520{
521 int len;
522 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000523 if (TYPE(n) != testlist)
524 REQ(n, exprlist);
525 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
526 len = (NCH(n) + 1) / 2;
527 for (i = 0; i < NCH(n); i += 2)
528 com_node(c, CHILD(n, i));
529 com_addoparg(c, BUILD_LIST, len);
530}
531
532static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000533com_dictmaker(c, n)
534 struct compiling *c;
535 node *n;
536{
537 int i;
538 /* dictmaker: test ':' test (',' test ':' value)* [','] */
539 for (i = 0; i+2 < NCH(n); i += 4) {
540 /* We must arrange things just right for STORE_SUBSCR.
541 It wants the stack to look like (value) (dict) (key) */
542 com_addbyte(c, DUP_TOP);
543 com_node(c, CHILD(n, i+2)); /* value */
544 com_addbyte(c, ROT_TWO);
545 com_node(c, CHILD(n, i)); /* key */
546 com_addbyte(c, STORE_SUBSCR);
547 }
548}
549
550static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000551com_atom(c, n)
552 struct compiling *c;
553 node *n;
554{
555 node *ch;
556 object *v;
557 int i;
558 REQ(n, atom);
559 ch = CHILD(n, 0);
560 switch (TYPE(ch)) {
561 case LPAR:
562 if (TYPE(CHILD(n, 1)) == RPAR)
563 com_addoparg(c, BUILD_TUPLE, 0);
564 else
565 com_node(c, CHILD(n, 1));
566 break;
567 case LSQB:
568 if (TYPE(CHILD(n, 1)) == RSQB)
569 com_addoparg(c, BUILD_LIST, 0);
570 else
571 com_list_constructor(c, CHILD(n, 1));
572 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000573 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000574 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000575 if (TYPE(CHILD(n, 1)) != RBRACE)
576 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000577 break;
578 case BACKQUOTE:
579 com_node(c, CHILD(n, 1));
580 com_addbyte(c, UNARY_CONVERT);
581 break;
582 case NUMBER:
583 if ((v = parsenumber(STR(ch))) == NULL) {
584 c->c_errors++;
585 i = 255;
586 }
587 else {
588 i = com_addconst(c, v);
589 DECREF(v);
590 }
591 com_addoparg(c, LOAD_CONST, i);
592 break;
593 case STRING:
594 if ((v = parsestr(STR(ch))) == NULL) {
595 c->c_errors++;
596 i = 255;
597 }
598 else {
599 i = com_addconst(c, v);
600 DECREF(v);
601 }
602 com_addoparg(c, LOAD_CONST, i);
603 break;
604 case NAME:
605 com_addopname(c, LOAD_NAME, ch);
606 break;
607 default:
608 fprintf(stderr, "node type %d\n", TYPE(ch));
609 err_setstr(SystemError, "com_atom: unexpected node type");
610 c->c_errors++;
611 }
612}
613
614static void
615com_slice(c, n, op)
616 struct compiling *c;
617 node *n;
618 int op;
619{
620 if (NCH(n) == 1) {
621 com_addbyte(c, op);
622 }
623 else if (NCH(n) == 2) {
624 if (TYPE(CHILD(n, 0)) != COLON) {
625 com_node(c, CHILD(n, 0));
626 com_addbyte(c, op+1);
627 }
628 else {
629 com_node(c, CHILD(n, 1));
630 com_addbyte(c, op+2);
631 }
632 }
633 else {
634 com_node(c, CHILD(n, 0));
635 com_node(c, CHILD(n, 2));
636 com_addbyte(c, op+3);
637 }
638}
639
640static void
641com_apply_subscript(c, n)
642 struct compiling *c;
643 node *n;
644{
645 REQ(n, subscript);
646 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) {
647 /* It's a single subscript */
648 com_node(c, CHILD(n, 0));
649 com_addbyte(c, BINARY_SUBSCR);
650 }
651 else {
652 /* It's a slice: [expr] ':' [expr] */
653 com_slice(c, n, SLICE);
654 }
655}
656
657static void
658com_call_function(c, n)
659 struct compiling *c;
660 node *n; /* EITHER testlist OR ')' */
661{
662 if (TYPE(n) == RPAR) {
Guido van Rossum288a60f1991-12-16 13:05:10 +0000663 com_addoparg(c, BUILD_TUPLE, 0);
664 com_addbyte(c, BINARY_CALL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000665 }
666 else {
Guido van Rossum288a60f1991-12-16 13:05:10 +0000667 REQ(n, testlist);
668 com_list(c, n, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000669 com_addbyte(c, BINARY_CALL);
670 }
671}
672
673static void
674com_select_member(c, n)
675 struct compiling *c;
676 node *n;
677{
678 com_addopname(c, LOAD_ATTR, n);
679}
680
681static void
682com_apply_trailer(c, n)
683 struct compiling *c;
684 node *n;
685{
686 REQ(n, trailer);
687 switch (TYPE(CHILD(n, 0))) {
688 case LPAR:
689 com_call_function(c, CHILD(n, 1));
690 break;
691 case DOT:
692 com_select_member(c, CHILD(n, 1));
693 break;
694 case LSQB:
695 com_apply_subscript(c, CHILD(n, 1));
696 break;
697 default:
698 err_setstr(SystemError,
699 "com_apply_trailer: unknown trailer type");
700 c->c_errors++;
701 }
702}
703
704static void
705com_factor(c, n)
706 struct compiling *c;
707 node *n;
708{
709 int i;
710 REQ(n, factor);
711 if (TYPE(CHILD(n, 0)) == PLUS) {
712 com_factor(c, CHILD(n, 1));
713 com_addbyte(c, UNARY_POSITIVE);
714 }
715 else if (TYPE(CHILD(n, 0)) == MINUS) {
716 com_factor(c, CHILD(n, 1));
717 com_addbyte(c, UNARY_NEGATIVE);
718 }
Guido van Rossum7928cd71991-10-24 14:59:31 +0000719 else if (TYPE(CHILD(n, 0)) == TILDE) {
720 com_factor(c, CHILD(n, 1));
721 com_addbyte(c, UNARY_INVERT);
722 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000723 else {
724 com_atom(c, CHILD(n, 0));
725 for (i = 1; i < NCH(n); i++)
726 com_apply_trailer(c, CHILD(n, i));
727 }
728}
729
730static void
731com_term(c, n)
732 struct compiling *c;
733 node *n;
734{
735 int i;
736 int op;
737 REQ(n, term);
738 com_factor(c, CHILD(n, 0));
739 for (i = 2; i < NCH(n); i += 2) {
740 com_factor(c, CHILD(n, i));
741 switch (TYPE(CHILD(n, i-1))) {
742 case STAR:
743 op = BINARY_MULTIPLY;
744 break;
745 case SLASH:
746 op = BINARY_DIVIDE;
747 break;
748 case PERCENT:
749 op = BINARY_MODULO;
750 break;
751 default:
752 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +0000753 "com_term: operator not *, / or %");
754 c->c_errors++;
755 op = 255;
756 }
757 com_addbyte(c, op);
758 }
759}
760
761static void
762com_arith_expr(c, n)
763 struct compiling *c;
764 node *n;
765{
766 int i;
767 int op;
768 REQ(n, arith_expr);
769 com_term(c, CHILD(n, 0));
770 for (i = 2; i < NCH(n); i += 2) {
771 com_term(c, CHILD(n, i));
772 switch (TYPE(CHILD(n, i-1))) {
773 case PLUS:
774 op = BINARY_ADD;
775 break;
776 case MINUS:
777 op = BINARY_SUBTRACT;
778 break;
779 default:
780 err_setstr(SystemError,
781 "com_arith_expr: operator not + or -");
782 c->c_errors++;
783 op = 255;
784 }
785 com_addbyte(c, op);
786 }
787}
788
789static void
790com_shift_expr(c, n)
791 struct compiling *c;
792 node *n;
793{
794 int i;
795 int op;
796 REQ(n, shift_expr);
797 com_arith_expr(c, CHILD(n, 0));
798 for (i = 2; i < NCH(n); i += 2) {
799 com_arith_expr(c, CHILD(n, i));
800 switch (TYPE(CHILD(n, i-1))) {
801 case LEFTSHIFT:
802 op = BINARY_LSHIFT;
803 break;
804 case RIGHTSHIFT:
805 op = BINARY_RSHIFT;
806 break;
807 default:
808 err_setstr(SystemError,
809 "com_shift_expr: operator not << or >>");
810 c->c_errors++;
811 op = 255;
812 }
813 com_addbyte(c, op);
814 }
815}
816
817static void
818com_and_expr(c, n)
819 struct compiling *c;
820 node *n;
821{
822 int i;
823 int op;
824 REQ(n, and_expr);
825 com_shift_expr(c, CHILD(n, 0));
826 for (i = 2; i < NCH(n); i += 2) {
827 com_shift_expr(c, CHILD(n, i));
828 if (TYPE(CHILD(n, i-1)) == AMPER) {
829 op = BINARY_AND;
830 }
831 else {
832 err_setstr(SystemError,
833 "com_and_expr: operator not &");
834 c->c_errors++;
835 op = 255;
836 }
837 com_addbyte(c, op);
838 }
839}
840
841static void
842com_xor_expr(c, n)
843 struct compiling *c;
844 node *n;
845{
846 int i;
847 int op;
848 REQ(n, xor_expr);
849 com_and_expr(c, CHILD(n, 0));
850 for (i = 2; i < NCH(n); i += 2) {
851 com_and_expr(c, CHILD(n, i));
852 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
853 op = BINARY_XOR;
854 }
855 else {
856 err_setstr(SystemError,
857 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000858 c->c_errors++;
859 op = 255;
860 }
861 com_addbyte(c, op);
862 }
863}
864
865static void
866com_expr(c, n)
867 struct compiling *c;
868 node *n;
869{
870 int i;
871 int op;
872 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000873 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000874 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +0000875 com_xor_expr(c, CHILD(n, i));
876 if (TYPE(CHILD(n, i-1)) == VBAR) {
877 op = BINARY_OR;
878 }
879 else {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000880 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +0000881 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000882 c->c_errors++;
883 op = 255;
884 }
885 com_addbyte(c, op);
886 }
887}
888
889static enum cmp_op
890cmp_type(n)
891 node *n;
892{
893 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +0000894 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000895 | 'in' | 'not' 'in' | 'is' | 'is' not' */
896 if (NCH(n) == 1) {
897 n = CHILD(n, 0);
898 switch (TYPE(n)) {
899 case LESS: return LT;
900 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +0000901 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000902 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +0000903 case LESSEQUAL: return LE;
904 case GREATEREQUAL: return GE;
905 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000906 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
907 if (strcmp(STR(n), "is") == 0) return IS;
908 }
909 }
910 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000911 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000912 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
913 return NOT_IN;
914 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
915 return IS_NOT;
916 }
917 }
918 return BAD;
919}
920
921static void
922com_comparison(c, n)
923 struct compiling *c;
924 node *n;
925{
926 int i;
927 enum cmp_op op;
928 int anchor;
929 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
930 com_expr(c, CHILD(n, 0));
931 if (NCH(n) == 1)
932 return;
933
934 /****************************************************************
935 The following code is generated for all but the last
936 comparison in a chain:
937
938 label: on stack: opcode: jump to:
939
940 a <code to load b>
941 a, b DUP_TOP
942 a, b, b ROT_THREE
943 b, a, b COMPARE_OP
944 b, 0-or-1 JUMP_IF_FALSE L1
945 b, 1 POP_TOP
946 b
947
948 We are now ready to repeat this sequence for the next
949 comparison in the chain.
950
951 For the last we generate:
952
953 b <code to load c>
954 b, c COMPARE_OP
955 0-or-1
956
957 If there were any jumps to L1 (i.e., there was more than one
958 comparison), we generate:
959
960 0-or-1 JUMP_FORWARD L2
961 L1: b, 0 ROT_TWO
962 0, b POP_TOP
963 0
964 L2:
965 ****************************************************************/
966
967 anchor = 0;
968
969 for (i = 2; i < NCH(n); i += 2) {
970 com_expr(c, CHILD(n, i));
971 if (i+2 < NCH(n)) {
972 com_addbyte(c, DUP_TOP);
973 com_addbyte(c, ROT_THREE);
974 }
975 op = cmp_type(CHILD(n, i-1));
976 if (op == BAD) {
977 err_setstr(SystemError,
978 "com_comparison: unknown comparison op");
979 c->c_errors++;
980 }
981 com_addoparg(c, COMPARE_OP, op);
982 if (i+2 < NCH(n)) {
983 com_addfwref(c, JUMP_IF_FALSE, &anchor);
984 com_addbyte(c, POP_TOP);
985 }
986 }
987
988 if (anchor) {
989 int anchor2 = 0;
990 com_addfwref(c, JUMP_FORWARD, &anchor2);
991 com_backpatch(c, anchor);
992 com_addbyte(c, ROT_TWO);
993 com_addbyte(c, POP_TOP);
994 com_backpatch(c, anchor2);
995 }
996}
997
998static void
999com_not_test(c, n)
1000 struct compiling *c;
1001 node *n;
1002{
1003 REQ(n, not_test); /* 'not' not_test | comparison */
1004 if (NCH(n) == 1) {
1005 com_comparison(c, CHILD(n, 0));
1006 }
1007 else {
1008 com_not_test(c, CHILD(n, 1));
1009 com_addbyte(c, UNARY_NOT);
1010 }
1011}
1012
1013static void
1014com_and_test(c, n)
1015 struct compiling *c;
1016 node *n;
1017{
1018 int i;
1019 int anchor;
1020 REQ(n, and_test); /* not_test ('and' not_test)* */
1021 anchor = 0;
1022 i = 0;
1023 for (;;) {
1024 com_not_test(c, CHILD(n, i));
1025 if ((i += 2) >= NCH(n))
1026 break;
1027 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1028 com_addbyte(c, POP_TOP);
1029 }
1030 if (anchor)
1031 com_backpatch(c, anchor);
1032}
1033
1034static void
1035com_test(c, n)
1036 struct compiling *c;
1037 node *n;
1038{
1039 int i;
1040 int anchor;
1041 REQ(n, test); /* and_test ('and' and_test)* */
1042 anchor = 0;
1043 i = 0;
1044 for (;;) {
1045 com_and_test(c, CHILD(n, i));
1046 if ((i += 2) >= NCH(n))
1047 break;
1048 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1049 com_addbyte(c, POP_TOP);
1050 }
1051 if (anchor)
1052 com_backpatch(c, anchor);
1053}
1054
1055static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001056com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001057 struct compiling *c;
1058 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001059 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001060{
1061 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001062 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001063 com_node(c, CHILD(n, 0));
1064 }
1065 else {
1066 int i;
1067 int len;
1068 len = (NCH(n) + 1) / 2;
1069 for (i = 0; i < NCH(n); i += 2)
1070 com_node(c, CHILD(n, i));
1071 com_addoparg(c, BUILD_TUPLE, len);
1072 }
1073}
1074
1075
1076/* Begin of assignment compilation */
1077
1078static void com_assign_name PROTO((struct compiling *, node *, int));
1079static void com_assign PROTO((struct compiling *, node *, int));
1080
1081static void
1082com_assign_attr(c, n, assigning)
1083 struct compiling *c;
1084 node *n;
1085 int assigning;
1086{
1087 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
1088}
1089
1090static void
1091com_assign_slice(c, n, assigning)
1092 struct compiling *c;
1093 node *n;
1094 int assigning;
1095{
1096 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE);
1097}
1098
1099static void
1100com_assign_subscript(c, n, assigning)
1101 struct compiling *c;
1102 node *n;
1103 int assigning;
1104{
1105 com_node(c, n);
1106 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR);
1107}
1108
1109static void
1110com_assign_trailer(c, n, assigning)
1111 struct compiling *c;
1112 node *n;
1113 int assigning;
1114{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001115 REQ(n, trailer);
1116 switch (TYPE(CHILD(n, 0))) {
1117 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001118 err_setstr(SyntaxError, "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001119 c->c_errors++;
1120 break;
1121 case DOT: /* '.' NAME */
1122 com_assign_attr(c, CHILD(n, 1), assigning);
1123 break;
1124 case LSQB: /* '[' subscript ']' */
1125 n = CHILD(n, 1);
1126 REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */
1127 if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON)
1128 com_assign_slice(c, n, assigning);
1129 else
1130 com_assign_subscript(c, CHILD(n, 0), assigning);
1131 break;
1132 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001133 err_setstr(SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001134 c->c_errors++;
1135 }
1136}
1137
1138static void
1139com_assign_tuple(c, n, assigning)
1140 struct compiling *c;
1141 node *n;
1142 int assigning;
1143{
1144 int i;
1145 if (TYPE(n) != testlist)
1146 REQ(n, exprlist);
1147 if (assigning)
1148 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
1149 for (i = 0; i < NCH(n); i += 2)
1150 com_assign(c, CHILD(n, i), assigning);
1151}
1152
1153static void
1154com_assign_list(c, n, assigning)
1155 struct compiling *c;
1156 node *n;
1157 int assigning;
1158{
1159 int i;
1160 if (assigning)
1161 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2);
1162 for (i = 0; i < NCH(n); i += 2)
1163 com_assign(c, CHILD(n, i), assigning);
1164}
1165
1166static void
1167com_assign_name(c, n, assigning)
1168 struct compiling *c;
1169 node *n;
1170 int assigning;
1171{
1172 REQ(n, NAME);
1173 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
1174}
1175
1176static void
1177com_assign(c, n, assigning)
1178 struct compiling *c;
1179 node *n;
1180 int assigning;
1181{
1182 /* Loop to avoid trivial recursion */
1183 for (;;) {
1184 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001185
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001186 case exprlist:
1187 case testlist:
1188 if (NCH(n) > 1) {
1189 com_assign_tuple(c, n, assigning);
1190 return;
1191 }
1192 n = CHILD(n, 0);
1193 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001194
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001195 case test:
1196 case and_test:
1197 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001198 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001199 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001200 case xor_expr:
1201 case and_expr:
1202 case shift_expr:
1203 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001204 case term:
1205 if (NCH(n) > 1) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001206 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001207 "can't assign to operator");
1208 c->c_errors++;
1209 return;
1210 }
1211 n = CHILD(n, 0);
1212 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001213
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 case factor: /* ('+'|'-'|'~') factor | atom trailer* */
1215 if (TYPE(CHILD(n, 0)) != atom) { /* '+'|'-'|'~' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001216 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001217 "can't assign to operator");
1218 c->c_errors++;
1219 return;
1220 }
1221 if (NCH(n) > 1) { /* trailer present */
1222 int i;
1223 com_node(c, CHILD(n, 0));
1224 for (i = 1; i+1 < NCH(n); i++) {
1225 com_apply_trailer(c, CHILD(n, i));
1226 } /* NB i is still alive */
1227 com_assign_trailer(c,
1228 CHILD(n, i), assigning);
1229 return;
1230 }
1231 n = CHILD(n, 0);
1232 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001233
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001234 case atom:
1235 switch (TYPE(CHILD(n, 0))) {
1236 case LPAR:
1237 n = CHILD(n, 1);
1238 if (TYPE(n) == RPAR) {
1239 /* XXX Should allow () = () ??? */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001240 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001241 "can't assign to ()");
1242 c->c_errors++;
1243 return;
1244 }
1245 break;
1246 case LSQB:
1247 n = CHILD(n, 1);
1248 if (TYPE(n) == RSQB) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001249 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001250 "can't assign to []");
1251 c->c_errors++;
1252 return;
1253 }
1254 com_assign_list(c, n, assigning);
1255 return;
1256 case NAME:
1257 com_assign_name(c, CHILD(n, 0), assigning);
1258 return;
1259 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001260 err_setstr(SyntaxError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +00001261 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001262 c->c_errors++;
1263 return;
1264 }
1265 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001266
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001267 default:
1268 fprintf(stderr, "node type %d\n", TYPE(n));
1269 err_setstr(SystemError, "com_assign: bad node");
1270 c->c_errors++;
1271 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001272
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001273 }
1274 }
1275}
1276
1277static void
1278com_expr_stmt(c, n)
1279 struct compiling *c;
1280 node *n;
1281{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001282 REQ(n, expr_stmt); /* exprlist ('=' exprlist)* */
1283 com_node(c, CHILD(n, NCH(n)-1));
1284 if (NCH(n) == 1) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001285 com_addbyte(c, PRINT_EXPR);
1286 }
1287 else {
1288 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001289 for (i = 0; i < NCH(n)-2; i+=2) {
1290 if (i+2 < NCH(n)-2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001291 com_addbyte(c, DUP_TOP);
1292 com_assign(c, CHILD(n, i), 1/*assign*/);
1293 }
1294 }
1295}
1296
1297static void
1298com_print_stmt(c, n)
1299 struct compiling *c;
1300 node *n;
1301{
1302 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001303 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
1304 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001305 com_node(c, CHILD(n, i));
1306 com_addbyte(c, PRINT_ITEM);
1307 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001308 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001309 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001310 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001311}
1312
1313static void
1314com_return_stmt(c, n)
1315 struct compiling *c;
1316 node *n;
1317{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001318 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001319 if (!c->c_infunction) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001320 err_setstr(SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001321 c->c_errors++;
1322 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001323 if (NCH(n) < 2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001324 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1325 else
1326 com_node(c, CHILD(n, 1));
1327 com_addbyte(c, RETURN_VALUE);
1328}
1329
1330static void
1331com_raise_stmt(c, n)
1332 struct compiling *c;
1333 node *n;
1334{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001335 REQ(n, raise_stmt); /* 'raise' test [',' test] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001336 com_node(c, CHILD(n, 1));
1337 if (NCH(n) > 3)
1338 com_node(c, CHILD(n, 3));
1339 else
1340 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1341 com_addbyte(c, RAISE_EXCEPTION);
1342}
1343
1344static void
1345com_import_stmt(c, n)
1346 struct compiling *c;
1347 node *n;
1348{
1349 int i;
1350 REQ(n, import_stmt);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001351 /* 'import' NAME (',' NAME)* |
1352 'from' NAME 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001353 if (STR(CHILD(n, 0))[0] == 'f') {
1354 /* 'from' NAME 'import' ... */
1355 REQ(CHILD(n, 1), NAME);
1356 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
1357 for (i = 3; i < NCH(n); i += 2)
1358 com_addopname(c, IMPORT_FROM, CHILD(n, i));
1359 com_addbyte(c, POP_TOP);
1360 }
1361 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001362 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001363 for (i = 1; i < NCH(n); i += 2) {
1364 com_addopname(c, IMPORT_NAME, CHILD(n, i));
1365 com_addopname(c, STORE_NAME, CHILD(n, i));
1366 }
1367 }
1368}
1369
1370static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001371com_global_stmt(c, n)
1372 struct compiling *c;
1373 node *n;
1374{
1375 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001376 REQ(n, global_stmt);
1377 /* 'global' NAME (',' NAME)* */
1378 for (i = 1; i < NCH(n); i += 2) {
1379 if (dictinsert(c->c_globals, STR(CHILD(n, i)), None) != 0)
1380 c->c_errors++;
1381 }
1382}
1383
1384static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001385com_if_stmt(c, n)
1386 struct compiling *c;
1387 node *n;
1388{
1389 int i;
1390 int anchor = 0;
1391 REQ(n, if_stmt);
1392 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
1393 for (i = 0; i+3 < NCH(n); i+=4) {
1394 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001395 node *ch = CHILD(n, i+1);
1396 if (i > 0)
1397 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001398 com_node(c, CHILD(n, i+1));
1399 com_addfwref(c, JUMP_IF_FALSE, &a);
1400 com_addbyte(c, POP_TOP);
1401 com_node(c, CHILD(n, i+3));
1402 com_addfwref(c, JUMP_FORWARD, &anchor);
1403 com_backpatch(c, a);
1404 com_addbyte(c, POP_TOP);
1405 }
1406 if (i+2 < NCH(n))
1407 com_node(c, CHILD(n, i+2));
1408 com_backpatch(c, anchor);
1409}
1410
1411static void
1412com_while_stmt(c, n)
1413 struct compiling *c;
1414 node *n;
1415{
1416 int break_anchor = 0;
1417 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001418 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001419 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
1420 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001421 block_push(c, SETUP_LOOP);
1422 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001423 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001424 com_node(c, CHILD(n, 1));
1425 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1426 com_addbyte(c, POP_TOP);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001427 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001428 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001429 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001430 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1431 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001432 com_backpatch(c, anchor);
1433 com_addbyte(c, POP_TOP);
1434 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001435 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001436 if (NCH(n) > 4)
1437 com_node(c, CHILD(n, 6));
1438 com_backpatch(c, break_anchor);
1439}
1440
1441static void
1442com_for_stmt(c, n)
1443 struct compiling *c;
1444 node *n;
1445{
1446 object *v;
1447 int break_anchor = 0;
1448 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001449 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001450 REQ(n, for_stmt);
1451 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
1452 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001453 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001454 com_node(c, CHILD(n, 3));
1455 v = newintobject(0L);
1456 if (v == NULL)
1457 c->c_errors++;
1458 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1459 XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001460 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001461 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001462 com_addfwref(c, FOR_LOOP, &anchor);
1463 com_assign(c, CHILD(n, 1), 1/*assigning*/);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001464 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001465 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001466 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001467 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1468 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001469 com_backpatch(c, anchor);
1470 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001471 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001472 if (NCH(n) > 8)
1473 com_node(c, CHILD(n, 8));
1474 com_backpatch(c, break_anchor);
1475}
1476
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001477/* Although 'execpt' and 'finally' clauses can be combined
1478 syntactically, they are compiled separately. In fact,
1479 try: S
1480 except E1: S1
1481 except E2: S2
1482 ...
1483 finally: Sf
1484 is equivalent to
1485 try:
1486 try: S
1487 except E1: S1
1488 except E2: S2
1489 ...
1490 finally: Sf
1491 meaning that the 'finally' clause is entered even if things
1492 go wrong again in an exception handler. Note that this is
1493 not the case for exception handlers: at most one is entered.
1494
1495 Code generated for "try: S finally: Sf" is as follows:
1496
1497 SETUP_FINALLY L
1498 <code for S>
1499 POP_BLOCK
1500 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001501 L: <code for Sf>
1502 END_FINALLY
1503
1504 The special instructions use the block stack. Each block
1505 stack entry contains the instruction that created it (here
1506 SETUP_FINALLY), the level of the value stack at the time the
1507 block stack entry was created, and a label (here L).
1508
1509 SETUP_FINALLY:
1510 Pushes the current value stack level and the label
1511 onto the block stack.
1512 POP_BLOCK:
1513 Pops en entry from the block stack, and pops the value
1514 stack until its level is the same as indicated on the
1515 block stack. (The label is ignored.)
1516 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001517 Pops a variable number of entries from the *value* stack
1518 and re-raises the exception they specify. The number of
1519 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001520
1521 The block stack is unwound when an exception is raised:
1522 when a SETUP_FINALLY entry is found, the exception is pushed
1523 onto the value stack (and the exception condition is cleared),
1524 and the interpreter jumps to the label gotten from the block
1525 stack.
1526
1527 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00001528 (The contents of the value stack is shown in [], with the top
1529 at the right; 'tb' is trace-back info, 'val' the exception's
1530 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001531
1532 Value stack Label Instruction Argument
1533 [] SETUP_EXCEPT L1
1534 [] <code for S>
1535 [] POP_BLOCK
1536 [] JUMP_FORWARD L0
1537
Guido van Rossum3f5da241990-12-20 15:06:42 +00001538 [tb, val, exc] L1: DUP )
1539 [tb, val, exc, exc] <evaluate E1> )
1540 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1541 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1542 [tb, val, exc, 1] POP )
1543 [tb, val, exc] POP
1544 [tb, val] <assign to V1> (or POP if no V1)
1545 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001546 [] <code for S1>
1547 JUMP_FORWARD L0
1548
Guido van Rossum3f5da241990-12-20 15:06:42 +00001549 [tb, val, exc, 0] L2: POP
1550 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001551 .............................etc.......................
1552
Guido van Rossum3f5da241990-12-20 15:06:42 +00001553 [tb, val, exc, 0] Ln+1: POP
1554 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001555
1556 [] L0: <next statement>
1557
1558 Of course, parts are not generated if Vi or Ei is not present.
1559*/
1560
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001561static void
1562com_try_stmt(c, n)
1563 struct compiling *c;
1564 node *n;
1565{
1566 int finally_anchor = 0;
1567 int except_anchor = 0;
1568 REQ(n, try_stmt);
1569 /* 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite] */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001570
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001571 if (NCH(n) > 3 && TYPE(CHILD(n, NCH(n)-3)) != except_clause) {
1572 /* Have a 'finally' clause */
1573 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001574 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001575 }
1576 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == except_clause) {
1577 /* Have an 'except' clause */
1578 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001579 block_push(c, SETUP_EXCEPT);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001580 }
1581 com_node(c, CHILD(n, 2));
1582 if (except_anchor) {
1583 int end_anchor = 0;
1584 int i;
1585 node *ch;
1586 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001587 block_pop(c, SETUP_EXCEPT);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001588 com_addfwref(c, JUMP_FORWARD, &end_anchor);
1589 com_backpatch(c, except_anchor);
1590 for (i = 3;
1591 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
1592 i += 3) {
1593 /* except_clause: 'except' [expr [',' expr]] */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001594 if (except_anchor == 0) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001595 err_setstr(SyntaxError,
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001596 "default 'except:' must be last");
1597 c->c_errors++;
1598 break;
1599 }
1600 except_anchor = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001601 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001602 if (NCH(ch) > 1) {
1603 com_addbyte(c, DUP_TOP);
1604 com_node(c, CHILD(ch, 1));
1605 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001606 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001607 com_addbyte(c, POP_TOP);
1608 }
1609 com_addbyte(c, POP_TOP);
1610 if (NCH(ch) > 3)
1611 com_assign(c, CHILD(ch, 3), 1/*assigning*/);
1612 else
1613 com_addbyte(c, POP_TOP);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001614 com_addbyte(c, POP_TOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001615 com_node(c, CHILD(n, i+2));
1616 com_addfwref(c, JUMP_FORWARD, &end_anchor);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001617 if (except_anchor) {
1618 com_backpatch(c, except_anchor);
1619 com_addbyte(c, POP_TOP);
1620 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001621 }
1622 com_addbyte(c, END_FINALLY);
1623 com_backpatch(c, end_anchor);
1624 }
1625 if (finally_anchor) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001626 node *ch;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001627 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001628 block_pop(c, SETUP_FINALLY);
1629 block_push(c, END_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001630 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001631 com_backpatch(c, finally_anchor);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001632 ch = CHILD(n, NCH(n)-1);
1633 com_addoparg(c, SET_LINENO, ch->n_lineno);
1634 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001635 com_addbyte(c, END_FINALLY);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001636 block_pop(c, END_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001637 }
1638}
1639
1640static void
1641com_suite(c, n)
1642 struct compiling *c;
1643 node *n;
1644{
1645 REQ(n, suite);
1646 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
1647 if (NCH(n) == 1) {
1648 com_node(c, CHILD(n, 0));
1649 }
1650 else {
1651 int i;
1652 for (i = 0; i < NCH(n); i++) {
1653 node *ch = CHILD(n, i);
1654 if (TYPE(ch) == stmt)
1655 com_node(c, ch);
1656 }
1657 }
1658}
1659
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001660/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001661static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001662com_continue_stmt(c, n)
1663 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001664 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001665{
1666 int i = c->c_nblocks;
1667 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
1668 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1669 }
1670 else {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001671 err_setstr(SyntaxError, "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001672 c->c_errors++;
1673 }
1674 /* XXX Could allow it inside a 'finally' clause
1675 XXX if we could pop the exception still on the stack */
1676}
1677
1678static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001679com_funcdef(c, n)
1680 struct compiling *c;
1681 node *n;
1682{
1683 object *v;
1684 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001685 v = (object *)compile(n, c->c_filename);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001686 if (v == NULL)
1687 c->c_errors++;
1688 else {
1689 int i = com_addconst(c, v);
1690 com_addoparg(c, LOAD_CONST, i);
1691 com_addbyte(c, BUILD_FUNCTION);
1692 com_addopname(c, STORE_NAME, CHILD(n, 1));
1693 DECREF(v);
1694 }
1695}
1696
1697static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001698com_oldbases(c, n)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001699 struct compiling *c;
1700 node *n;
1701{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001702 int i;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001703 REQ(n, baselist);
1704 /*
1705 baselist: atom arguments (',' atom arguments)*
Guido van Rossumc5e96291991-12-10 13:53:51 +00001706 arguments: '(' ')'
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001707 */
1708 for (i = 0; i < NCH(n); i += 3)
1709 com_node(c, CHILD(n, i));
1710 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 3);
1711}
1712
1713static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001714com_newbases(c, n)
1715 struct compiling *c;
1716 node *n;
1717{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001718 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001719 REQ(n, testlist);
1720 /* testlist: test (',' test)* [','] */
1721 for (i = 0; i < NCH(n); i += 2)
1722 com_node(c, CHILD(n, i));
1723 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2);
1724}
1725
1726static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001727com_classdef(c, n)
1728 struct compiling *c;
1729 node *n;
1730{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001731 object *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001732 REQ(n, classdef);
1733 /*
Guido van Rossumc5e96291991-12-10 13:53:51 +00001734 classdef: 'class' NAME
1735 ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001736 baselist: atom arguments (',' atom arguments)*
Guido van Rossumc5e96291991-12-10 13:53:51 +00001737 arguments: '(' ')'
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001738 */
Guido van Rossumc5e96291991-12-10 13:53:51 +00001739 /* This piece of code must push a tuple on the stack (the bases) */
1740 if (TYPE(CHILD(n, 2)) != LPAR) {
1741 /* New syntax without base classes:
1742 class NAME ':' suite
1743 ___________^
1744 */
1745 com_addoparg(c, BUILD_TUPLE, 0);
1746 }
1747 else {
1748 if (TYPE(CHILD(n, 3)) == RPAR) {
1749 /* Old syntax with or without base classes:
1750 class NAME '(' ')' ['=' baselist] ':' suite
1751 _______________^....^...^
1752 */
1753 if (TYPE(CHILD(n, 4)) == EQUAL)
1754 com_oldbases(c, CHILD(n, 5));
1755 else
1756 com_addoparg(c, BUILD_TUPLE, 0);
1757 }
1758 else {
1759 /* New syntax with base classes:
1760 class NAME '(' testlist ')' ':' suite
1761 _______________^
1762 */
1763 com_newbases(c, CHILD(n, 3));
1764 }
1765 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001766 v = (object *)compile(n, c->c_filename);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001767 if (v == NULL)
1768 c->c_errors++;
1769 else {
1770 int i = com_addconst(c, v);
1771 com_addoparg(c, LOAD_CONST, i);
1772 com_addbyte(c, BUILD_FUNCTION);
1773 com_addbyte(c, UNARY_CALL);
1774 com_addbyte(c, BUILD_CLASS);
1775 com_addopname(c, STORE_NAME, CHILD(n, 1));
1776 DECREF(v);
1777 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001778}
1779
1780static void
1781com_node(c, n)
1782 struct compiling *c;
1783 node *n;
1784{
1785 switch (TYPE(n)) {
1786
1787 /* Definition nodes */
1788
1789 case funcdef:
1790 com_funcdef(c, n);
1791 break;
1792 case classdef:
1793 com_classdef(c, n);
1794 break;
1795
1796 /* Trivial parse tree nodes */
1797
1798 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001799 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001800 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001801 com_node(c, CHILD(n, 0));
1802 break;
1803
1804 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001805 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
1806 com_addoparg(c, SET_LINENO, n->n_lineno);
1807 {
1808 int i;
1809 for (i = 0; i < NCH(n)-1; i += 2)
1810 com_node(c, CHILD(n, i));
1811 }
1812 break;
1813
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001814 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001815 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001816 com_node(c, CHILD(n, 0));
1817 break;
1818
1819 /* Statement nodes */
1820
1821 case expr_stmt:
1822 com_expr_stmt(c, n);
1823 break;
1824 case print_stmt:
1825 com_print_stmt(c, n);
1826 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001827 case del_stmt: /* 'del' exprlist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001828 com_assign(c, CHILD(n, 1), 0/*delete*/);
1829 break;
1830 case pass_stmt:
1831 break;
1832 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001833 if (c->c_loops == 0) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001834 err_setstr(SyntaxError, "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001835 c->c_errors++;
1836 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001837 com_addbyte(c, BREAK_LOOP);
1838 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001839 case continue_stmt:
1840 com_continue_stmt(c, n);
1841 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001842 case return_stmt:
1843 com_return_stmt(c, n);
1844 break;
1845 case raise_stmt:
1846 com_raise_stmt(c, n);
1847 break;
1848 case import_stmt:
1849 com_import_stmt(c, n);
1850 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001851 case global_stmt:
1852 com_global_stmt(c, n);
1853 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001854 case if_stmt:
1855 com_if_stmt(c, n);
1856 break;
1857 case while_stmt:
1858 com_while_stmt(c, n);
1859 break;
1860 case for_stmt:
1861 com_for_stmt(c, n);
1862 break;
1863 case try_stmt:
1864 com_try_stmt(c, n);
1865 break;
1866 case suite:
1867 com_suite(c, n);
1868 break;
1869
1870 /* Expression nodes */
1871
1872 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00001873 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001874 break;
1875 case test:
1876 com_test(c, n);
1877 break;
1878 case and_test:
1879 com_and_test(c, n);
1880 break;
1881 case not_test:
1882 com_not_test(c, n);
1883 break;
1884 case comparison:
1885 com_comparison(c, n);
1886 break;
1887 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00001888 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001889 break;
1890 case expr:
1891 com_expr(c, n);
1892 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001893 case xor_expr:
1894 com_xor_expr(c, n);
1895 break;
1896 case and_expr:
1897 com_and_expr(c, n);
1898 break;
1899 case shift_expr:
1900 com_shift_expr(c, n);
1901 break;
1902 case arith_expr:
1903 com_arith_expr(c, n);
1904 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001905 case term:
1906 com_term(c, n);
1907 break;
1908 case factor:
1909 com_factor(c, n);
1910 break;
1911 case atom:
1912 com_atom(c, n);
1913 break;
1914
1915 default:
1916 fprintf(stderr, "node type %d\n", TYPE(n));
1917 err_setstr(SystemError, "com_node: unexpected node type");
1918 c->c_errors++;
1919 }
1920}
1921
1922static void com_fplist PROTO((struct compiling *, node *));
1923
1924static void
1925com_fpdef(c, n)
1926 struct compiling *c;
1927 node *n;
1928{
1929 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
1930 if (TYPE(CHILD(n, 0)) == LPAR)
1931 com_fplist(c, CHILD(n, 1));
1932 else
1933 com_addopname(c, STORE_NAME, CHILD(n, 0));
1934}
1935
1936static void
1937com_fplist(c, n)
1938 struct compiling *c;
1939 node *n;
1940{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00001941 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001942 if (NCH(n) == 1) {
1943 com_fpdef(c, CHILD(n, 0));
1944 }
1945 else {
1946 int i;
1947 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
1948 for (i = 0; i < NCH(n); i += 2)
1949 com_fpdef(c, CHILD(n, i));
1950 }
1951}
1952
1953static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00001954com_arglist(c, n)
1955 struct compiling *c;
1956 node *n;
1957{
1958 int i, nargs, op;
1959 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001960 /* varargslist:
1961 (fpdef ',')* ('+'|'*') NAME | fpdef (',' fpdef)* [','] */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00001962 op = UNPACK_ARG;
1963 nargs = (NCH(n) + 1) / 2;
1964 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumacbefef1992-01-19 16:33:51 +00001965 int t = TYPE(CHILD(n, i));
1966 if (t == PLUS || t == STAR) {
Guido van Rossum49d6dc41992-01-14 18:30:26 +00001967 op = UNPACK_VARARG;
1968 nargs = i/2;
1969 break;
1970 }
1971 }
1972 com_addoparg(c, op, nargs);
1973 for (i = 0; i < 2*nargs; i += 2)
1974 com_fpdef(c, CHILD(n, i));
1975 if (op == UNPACK_VARARG)
1976 com_addopname(c, STORE_NAME, CHILD(n, 2*nargs+1));
1977}
1978
1979static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001980com_file_input(c, n)
1981 struct compiling *c;
1982 node *n;
1983{
1984 int i;
1985 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
1986 for (i = 0; i < NCH(n); i++) {
1987 node *ch = CHILD(n, i);
1988 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
1989 com_node(c, ch);
1990 }
1991}
1992
1993/* Top-level compile-node interface */
1994
1995static void
1996compile_funcdef(c, n)
1997 struct compiling *c;
1998 node *n;
1999{
2000 node *ch;
2001 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002002 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
2003 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002004 if (TYPE(ch) == RPAR)
Guido van Rossum288a60f1991-12-16 13:05:10 +00002005 com_addoparg(c, UNPACK_ARG, 0);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002006 else
2007 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002008 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002009 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002010 c->c_infunction = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002011 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2012 com_addbyte(c, RETURN_VALUE);
2013}
2014
2015static void
2016compile_node(c, n)
2017 struct compiling *c;
2018 node *n;
2019{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002020 com_addoparg(c, SET_LINENO, n->n_lineno);
2021
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002022 switch (TYPE(n)) {
2023
Guido van Rossum4c417781991-01-21 16:09:22 +00002024 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002025 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
2026 n = CHILD(n, 0);
2027 if (TYPE(n) != NEWLINE)
2028 com_node(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002029 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2030 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002031 break;
2032
Guido van Rossum4c417781991-01-21 16:09:22 +00002033 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002034 com_file_input(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002035 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2036 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002037 break;
2038
Guido van Rossum4c417781991-01-21 16:09:22 +00002039 case expr_input: /* Built-in function eval() */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002040 com_node(c, CHILD(n, 0));
2041 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002042 break;
2043
Guido van Rossum4c417781991-01-21 16:09:22 +00002044 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00002045 com_node(c, CHILD(n, 0));
2046 com_addbyte(c, RETURN_VALUE);
2047 break;
2048
2049 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002050 compile_funcdef(c, n);
2051 break;
2052
Guido van Rossum4c417781991-01-21 16:09:22 +00002053 case classdef: /* A class definition */
Guido van Rossumc5e96291991-12-10 13:53:51 +00002054 /* classdef: 'class' NAME
2055 ['(' testlist ')' |'(' ')' ['=' baselist]]
2056 ':' suite */
Guido van Rossumc5e96291991-12-10 13:53:51 +00002057 com_node(c, CHILD(n, NCH(n)-1)); /* The suite */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002058 com_addbyte(c, LOAD_LOCALS);
2059 com_addbyte(c, RETURN_VALUE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002060 break;
2061
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002062 default:
2063 fprintf(stderr, "node type %d\n", TYPE(n));
2064 err_setstr(SystemError, "compile_node: unexpected node type");
2065 c->c_errors++;
2066 }
2067}
2068
Guido van Rossum282914b1991-04-04 10:42:56 +00002069/* Optimization for local and global variables.
2070
2071 Attempt to replace all LOAD_NAME instructions that refer to a local
2072 variable with LOAD_LOCAL instructions, and all that refer to a global
2073 variable with LOAD_GLOBAL instructions.
2074
2075 To find all local variables, we check all STORE_NAME and IMPORT_FROM
2076 instructions. This yields all local variables, including arguments,
2077 function definitions, class definitions and import statements.
2078
2079 There is one leak: 'from foo import *' introduces local variables
2080 that we can't know while compiling. If this is the case, LOAD_GLOBAL
2081 instructions are not generated -- LOAD_NAME is left in place for
2082 globals, since it first checks for globals (LOAD_LOCAL is still used
2083 for recognized locals, since it doesn't hurt).
2084
2085 This optimization means that using the same name as a global and
2086 as a local variable within the same scope is now illegal, which
2087 is a change to the language! Also using eval() to introduce new
2088 local variables won't work. But both were bad practice at best.
2089
2090 The optimization doesn't save much: basically, it saves one
2091 unsuccessful dictionary lookup per global (or built-in) variable
2092 reference. On the (slow!) Mac Plus, with 4 local variables,
2093 this saving was measured to be about 0.18 ms. We might save more
2094 by using a different data structure to hold local variables, like
2095 an array indexed by variable number.
2096
2097 NB: this modifies the string object co->co_code!
2098*/
2099
2100static void
2101optimizer(co)
2102 codeobject *co;
2103{
Guido van Rossum0a697f61991-04-16 08:39:12 +00002104 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00002105 object *locals;
2106 int opcode;
2107 int oparg;
2108 object *name;
2109 int star_used;
Guido van Rossum0a697f61991-04-16 08:39:12 +00002110
Guido van Rossum282914b1991-04-04 10:42:56 +00002111#define NEXTOP() (*next_instr++)
2112#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
2113#define GETITEM(v, i) (getlistitem((v), (i)))
2114#define GETNAMEOBJ(i) (GETITEM(co->co_names, (i)))
2115
2116 locals = newdictobject();
2117 if (locals == NULL) {
2118 err_clear();
2119 return; /* For now, this is OK */
2120 }
2121
Guido van Rossum0a697f61991-04-16 08:39:12 +00002122 next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002123 for (;;) {
2124 opcode = NEXTOP();
2125 if (opcode == STOP_CODE)
2126 break;
2127 if (HAS_ARG(opcode))
2128 oparg = NEXTARG();
2129 if (opcode == STORE_NAME || opcode == IMPORT_FROM) {
2130 name = GETNAMEOBJ(oparg);
2131 if (dict2insert(locals, name, None) != 0) {
2132 DECREF(locals);
2133 return; /* Sorry */
2134 }
2135 }
2136 }
2137
2138 star_used = (dictlookup(locals, "*") != NULL);
Guido van Rossum0a697f61991-04-16 08:39:12 +00002139 next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002140 for (;;) {
2141 cur_instr = next_instr;
2142 opcode = NEXTOP();
2143 if (opcode == STOP_CODE)
2144 break;
2145 if (HAS_ARG(opcode))
2146 oparg = NEXTARG();
2147 if (opcode == LOAD_NAME) {
2148 name = GETNAMEOBJ(oparg);
Guido van Rossum83163251991-08-16 08:58:43 +00002149 if (dict2lookup(locals, name) != NULL)
Guido van Rossum282914b1991-04-04 10:42:56 +00002150 *cur_instr = LOAD_LOCAL;
Guido van Rossum83163251991-08-16 08:58:43 +00002151 else {
2152 err_clear();
2153 if (!star_used)
2154 *cur_instr = LOAD_GLOBAL;
2155 }
Guido van Rossum282914b1991-04-04 10:42:56 +00002156 }
2157 }
2158
2159 DECREF(locals);
2160}
2161
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002162codeobject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002163compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002164 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002165 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002166{
2167 struct compiling sc;
2168 codeobject *co;
Guido van Rossuma082ce41991-06-04 19:41:56 +00002169 object *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002170 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002171 return NULL;
2172 compile_node(&sc, n);
2173 com_done(&sc);
Guido van Rossuma082ce41991-06-04 19:41:56 +00002174 if (sc.c_errors == 0 && (v = newstringobject(filename)) != NULL) {
2175 co = newcodeobject(sc.c_code, sc.c_consts, sc.c_names, v);
2176 DECREF(v);
2177 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002178 else
2179 co = NULL;
2180 com_free(&sc);
Guido van Rossumfb8edfc1991-05-14 11:56:03 +00002181 if (co != NULL && filename[0] != '<')
Guido van Rossum282914b1991-04-04 10:42:56 +00002182 optimizer(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002183 return co;
2184}