blob: 116b6bb1c7cced2d3dfd599dab54db40cca52031 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum9bfef441993-03-29 10:43:31 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, 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'
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 Rossum9bfef441993-03-29 10:43:31 +000053 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000054 {NULL} /* Sentinel */
55};
56
57static object *
58code_getattr(co, name)
59 codeobject *co;
60 char *name;
61{
62 return getmember((char *)co, code_memberlist, name);
63}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000064
65static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000066code_dealloc(co)
67 codeobject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +000068{
Guido van Rossum3f5da241990-12-20 15:06:42 +000069 XDECREF(co->co_code);
70 XDECREF(co->co_consts);
71 XDECREF(co->co_names);
72 XDECREF(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +000073 XDECREF(co->co_name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000074 DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +000075}
76
Guido van Rossum2dff9911992-09-03 20:50:59 +000077static object *
78code_repr(co)
79 codeobject *co;
80{
81 char buf[500];
82 int lineno = -1;
83 char *p = GETSTRINGVALUE(co->co_code);
84 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +000085 char *name = "???";
Guido van Rossum2dff9911992-09-03 20:50:59 +000086 if (*p == SET_LINENO)
87 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
88 if (co->co_filename && is_stringobject(co->co_filename))
89 filename = getstringvalue(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +000090 if (co->co_name && is_stringobject(co->co_name))
91 name = getstringvalue(co->co_name);
92 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
93 name, (long)co, filename, lineno);
Guido van Rossum2dff9911992-09-03 20:50:59 +000094 return newstringobject(buf);
95}
96
Guido van Rossum10dc2e81990-11-18 17:27:39 +000097typeobject Codetype = {
98 OB_HEAD_INIT(&Typetype)
99 0,
100 "code",
101 sizeof(codeobject),
102 0,
103 code_dealloc, /*tp_dealloc*/
104 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105 code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000106 0, /*tp_setattr*/
107 0, /*tp_compare*/
Guido van Rossum2dff9911992-09-03 20:50:59 +0000108 code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000109 0, /*tp_as_number*/
110 0, /*tp_as_sequence*/
111 0, /*tp_as_mapping*/
112};
113
Guido van Rossuma082ce41991-06-04 19:41:56 +0000114codeobject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000115newcodeobject(code, consts, names, filename, name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000116 object *code;
117 object *consts;
118 object *names;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000119 object *filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000120 object *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000121{
122 codeobject *co;
123 int i;
124 /* Check argument types */
125 if (code == NULL || !is_stringobject(code) ||
126 consts == NULL || !is_listobject(consts) ||
Guido van Rossum9bfef441993-03-29 10:43:31 +0000127 names == NULL || !is_listobject(names) ||
128 name == NULL || !is_stringobject(name)) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000129 err_badcall();
130 return NULL;
131 }
132 /* Make sure the list of names contains only strings */
133 for (i = getlistsize(names); --i >= 0; ) {
134 object *v = getlistitem(names, i);
135 if (v == NULL || !is_stringobject(v)) {
136 err_badcall();
137 return NULL;
138 }
139 }
140 co = NEWOBJ(codeobject, &Codetype);
141 if (co != NULL) {
142 INCREF(code);
143 co->co_code = (stringobject *)code;
144 INCREF(consts);
145 co->co_consts = consts;
146 INCREF(names);
147 co->co_names = names;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000148 INCREF(filename);
149 co->co_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000150 INCREF(name);
151 co->co_name = name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000152 }
153 return co;
154}
155
156
157/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000158
159#define MAXBLOCKS 20 /* Max static block nesting within a function */
160
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000161struct compiling {
162 object *c_code; /* string */
163 object *c_consts; /* list of objects */
164 object *c_names; /* list of strings (names) */
Guido van Rossumc5e96291991-12-10 13:53:51 +0000165 object *c_globals; /* dictionary */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000166 int c_nexti; /* index into c_code */
167 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000168 int c_infunction; /* set when compiling a function */
169 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000170 int c_begin; /* begin of current loop, for 'continue' */
171 int c_block[MAXBLOCKS]; /* stack of block types */
172 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000174 char *c_name; /* name of object (e.g. function) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000175};
176
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000177
178/* Interface to the block stack */
179
180static void
181block_push(c, type)
182 struct compiling *c;
183 int type;
184{
185 if (c->c_nblocks >= MAXBLOCKS) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +0000186 err_setstr(SystemError, "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000187 c->c_errors++;
188 }
189 else {
190 c->c_block[c->c_nblocks++] = type;
191 }
192}
193
194static void
195block_pop(c, type)
196 struct compiling *c;
197 int type;
198{
199 if (c->c_nblocks > 0)
200 c->c_nblocks--;
201 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
202 err_setstr(SystemError, "bad block pop");
203 c->c_errors++;
204 }
205}
206
207
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000208/* Prototypes */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000209
Guido van Rossum3f5da241990-12-20 15:06:42 +0000210static int com_init PROTO((struct compiling *, char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000211static void com_free PROTO((struct compiling *));
212static void com_done PROTO((struct compiling *));
213static void com_node PROTO((struct compiling *, struct _node *));
214static void com_addbyte PROTO((struct compiling *, int));
215static void com_addint PROTO((struct compiling *, int));
216static void com_addoparg PROTO((struct compiling *, int, int));
217static void com_addfwref PROTO((struct compiling *, int, int *));
218static void com_backpatch PROTO((struct compiling *, int));
219static int com_add PROTO((struct compiling *, object *, object *));
220static int com_addconst PROTO((struct compiling *, object *));
221static int com_addname PROTO((struct compiling *, object *));
222static void com_addopname PROTO((struct compiling *, int, node *));
Guido van Rossum288a60f1991-12-16 13:05:10 +0000223static void com_list PROTO((struct compiling *, node *, int));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000224
225static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000226com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000227 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000229{
Guido van Rossum62d46241991-04-03 19:00:23 +0000230 if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000231 goto fail_3;
232 if ((c->c_consts = newlistobject(0)) == NULL)
233 goto fail_2;
234 if ((c->c_names = newlistobject(0)) == NULL)
235 goto fail_1;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000236 if ((c->c_globals = newdictobject()) == NULL)
237 goto fail_0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000238 c->c_nexti = 0;
239 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240 c->c_infunction = 0;
241 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000242 c->c_begin = 0;
243 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000244 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000245 c->c_name = "?";
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000246 return 1;
247
Guido van Rossumc5e96291991-12-10 13:53:51 +0000248 fail_0:
249 DECREF(c->c_names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000250 fail_1:
251 DECREF(c->c_consts);
252 fail_2:
253 DECREF(c->c_code);
254 fail_3:
255 return 0;
256}
257
258static void
259com_free(c)
260 struct compiling *c;
261{
262 XDECREF(c->c_code);
263 XDECREF(c->c_consts);
264 XDECREF(c->c_names);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000265 XDECREF(c->c_globals);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000266}
267
268static void
269com_done(c)
270 struct compiling *c;
271{
272 if (c->c_code != NULL)
273 resizestring(&c->c_code, c->c_nexti);
274}
275
276static void
277com_addbyte(c, byte)
278 struct compiling *c;
279 int byte;
280{
281 int len;
282 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000283 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000284 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
285 abort();
Guido van Rossum01cfd441991-10-20 20:12:38 +0000286 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000287 err_setstr(SystemError, "com_addbyte: byte out of range");
288 c->c_errors++;
289 }
290 if (c->c_code == NULL)
291 return;
292 len = getstringsize(c->c_code);
293 if (c->c_nexti >= len) {
294 if (resizestring(&c->c_code, len+1000) != 0) {
295 c->c_errors++;
296 return;
297 }
298 }
299 getstringvalue(c->c_code)[c->c_nexti++] = byte;
300}
301
302static void
303com_addint(c, x)
304 struct compiling *c;
305 int x;
306{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000307 com_addbyte(c, x & 0xff);
308 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000309}
310
311static void
312com_addoparg(c, op, arg)
313 struct compiling *c;
314 int op;
315 int arg;
316{
317 com_addbyte(c, op);
318 com_addint(c, arg);
319}
320
321static void
322com_addfwref(c, op, p_anchor)
323 struct compiling *c;
324 int op;
325 int *p_anchor;
326{
327 /* Compile a forward reference for backpatching */
328 int here;
329 int anchor;
330 com_addbyte(c, op);
331 here = c->c_nexti;
332 anchor = *p_anchor;
333 *p_anchor = here;
334 com_addint(c, anchor == 0 ? 0 : here - anchor);
335}
336
337static void
338com_backpatch(c, anchor)
339 struct compiling *c;
340 int anchor; /* Must be nonzero */
341{
342 unsigned char *code = (unsigned char *) getstringvalue(c->c_code);
343 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000344 int dist;
345 int prev;
346 for (;;) {
347 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000348 prev = code[anchor] + (code[anchor+1] << 8);
349 dist = target - (anchor+2);
350 code[anchor] = dist & 0xff;
351 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000352 if (!prev)
353 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000354 anchor -= prev;
355 }
356}
357
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000358/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000359
360static int
361com_add(c, list, v)
362 struct compiling *c;
363 object *list;
364 object *v;
365{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000366 int n = getlistsize(list);
367 int i;
368 for (i = n; --i >= 0; ) {
369 object *w = getlistitem(list, i);
Guido van Rossumefc0bd01991-07-01 18:44:20 +0000370 if (v->ob_type == w->ob_type && cmpobject(v, w) == 0)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000371 return i;
372 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000373 if (addlistitem(list, v) != 0)
374 c->c_errors++;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000375 return n;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000376}
377
378static int
379com_addconst(c, v)
380 struct compiling *c;
381 object *v;
382{
383 return com_add(c, c->c_consts, v);
384}
385
386static int
387com_addname(c, v)
388 struct compiling *c;
389 object *v;
390{
391 return com_add(c, c->c_names, v);
392}
393
394static void
395com_addopname(c, op, n)
396 struct compiling *c;
397 int op;
398 node *n;
399{
400 object *v;
401 int i;
402 char *name;
403 if (TYPE(n) == STAR)
404 name = "*";
405 else {
406 REQ(n, NAME);
407 name = STR(n);
408 }
409 if ((v = newstringobject(name)) == NULL) {
410 c->c_errors++;
411 i = 255;
412 }
413 else {
414 i = com_addname(c, v);
415 DECREF(v);
416 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000417 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
418 switch (op) {
419 case LOAD_NAME:
420 case STORE_NAME:
421 case DELETE_NAME:
422 if (dictlookup(c->c_globals, name) != NULL) {
423 switch (op) {
424 case LOAD_NAME: op = LOAD_GLOBAL; break;
425 case STORE_NAME: op = STORE_GLOBAL; break;
426 case DELETE_NAME: op = DELETE_GLOBAL; break;
427 }
428 }
429 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000430 com_addoparg(c, op, i);
431}
432
433static object *
434parsenumber(s)
435 char *s;
436{
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000437 extern long strtol PROTO((const char *, char **, int));
438 extern unsigned long strtoul PROTO((const char *, char **, int));
439 extern double strtod PROTO((const char *, char **));
Guido van Rossumc5e96291991-12-10 13:53:51 +0000440 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000441 long x;
Guido van Rossum282914b1991-04-04 10:42:56 +0000442 double xx;
443 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000444 end = s + strlen(s) - 1;
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000445 if (*end == 'l' || *end == 'L')
Guido van Rossumc5e96291991-12-10 13:53:51 +0000446 return long_scan(s, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000447 if (s[0] == '0')
448 x = (long) strtoul(s, &end, 0);
449 else
450 x = strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000451 if (*end == '\0') {
452 if (errno != 0) {
Guido van Rossumefb087b1991-12-16 15:41:41 +0000453 err_setstr(OverflowError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000454 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000455 return NULL;
456 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000457 return newintobject(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000458 }
459 errno = 0;
460 xx = strtod(s, &end);
461 if (*end == '\0') {
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000462#ifndef BROKEN_STRTOD
463 /* Some strtod() versions (e.g., in older SunOS systems)
464 set errno incorrectly; better to ignore overflows
465 than not to be able to use float literals at all! */
Guido van Rossum282914b1991-04-04 10:42:56 +0000466 if (errno != 0) {
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000467 err_setstr(OverflowError, "float literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000468 return NULL;
469 }
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000470#endif
Guido van Rossum282914b1991-04-04 10:42:56 +0000471 return newfloatobject(xx);
472 }
Guido van Rossumefb087b1991-12-16 15:41:41 +0000473 err_setstr(SystemError, "bad number syntax?!?!");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000474 return NULL;
475}
476
477static object *
478parsestr(s)
479 char *s;
480{
481 object *v;
482 int len;
483 char *buf;
484 char *p;
485 int c;
486 if (*s != '\'') {
487 err_badcall();
488 return NULL;
489 }
490 s++;
491 len = strlen(s);
492 if (s[--len] != '\'') {
493 err_badcall();
494 return NULL;
495 }
496 if (strchr(s, '\\') == NULL)
497 return newsizedstringobject(s, len);
498 v = newsizedstringobject((char *)NULL, len);
499 p = buf = getstringvalue(v);
500 while (*s != '\0' && *s != '\'') {
501 if (*s != '\\') {
502 *p++ = *s++;
503 continue;
504 }
505 s++;
506 switch (*s++) {
507 /* XXX This assumes ASCII! */
508 case '\\': *p++ = '\\'; break;
509 case '\'': *p++ = '\''; break;
510 case 'b': *p++ = '\b'; break;
511 case 'f': *p++ = '\014'; break; /* FF */
512 case 't': *p++ = '\t'; break;
513 case 'n': *p++ = '\n'; break;
514 case 'r': *p++ = '\r'; break;
515 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000516 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
517 case '0': case '1': case '2': case '3':
518 case '4': case '5': case '6': case '7':
519 c = s[-1] - '0';
520 if ('0' <= *s && *s <= '7') {
521 c = (c<<3) + *s++ - '0';
522 if ('0' <= *s && *s <= '7')
523 c = (c<<3) + *s++ - '0';
524 }
525 *p++ = c;
526 break;
527 case 'x':
528 if (isxdigit(*s)) {
529 sscanf(s, "%x", &c);
530 *p++ = c;
531 do {
532 s++;
533 } while (isxdigit(*s));
534 break;
535 }
536 /* FALLTHROUGH */
537 default: *p++ = '\\'; *p++ = s[-1]; break;
538 }
539 }
540 resizestring(&v, (int)(p - buf));
541 return v;
542}
543
544static void
545com_list_constructor(c, n)
546 struct compiling *c;
547 node *n;
548{
549 int len;
550 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000551 if (TYPE(n) != testlist)
552 REQ(n, exprlist);
553 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
554 len = (NCH(n) + 1) / 2;
555 for (i = 0; i < NCH(n); i += 2)
556 com_node(c, CHILD(n, i));
557 com_addoparg(c, BUILD_LIST, len);
558}
559
560static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000561com_dictmaker(c, n)
562 struct compiling *c;
563 node *n;
564{
565 int i;
566 /* dictmaker: test ':' test (',' test ':' value)* [','] */
567 for (i = 0; i+2 < NCH(n); i += 4) {
568 /* We must arrange things just right for STORE_SUBSCR.
569 It wants the stack to look like (value) (dict) (key) */
570 com_addbyte(c, DUP_TOP);
571 com_node(c, CHILD(n, i+2)); /* value */
572 com_addbyte(c, ROT_TWO);
573 com_node(c, CHILD(n, i)); /* key */
574 com_addbyte(c, STORE_SUBSCR);
575 }
576}
577
578static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000579com_atom(c, n)
580 struct compiling *c;
581 node *n;
582{
583 node *ch;
584 object *v;
585 int i;
586 REQ(n, atom);
587 ch = CHILD(n, 0);
588 switch (TYPE(ch)) {
589 case LPAR:
590 if (TYPE(CHILD(n, 1)) == RPAR)
591 com_addoparg(c, BUILD_TUPLE, 0);
592 else
593 com_node(c, CHILD(n, 1));
594 break;
595 case LSQB:
596 if (TYPE(CHILD(n, 1)) == RSQB)
597 com_addoparg(c, BUILD_LIST, 0);
598 else
599 com_list_constructor(c, CHILD(n, 1));
600 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000601 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000602 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000603 if (TYPE(CHILD(n, 1)) != RBRACE)
604 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000605 break;
606 case BACKQUOTE:
607 com_node(c, CHILD(n, 1));
608 com_addbyte(c, UNARY_CONVERT);
609 break;
610 case NUMBER:
611 if ((v = parsenumber(STR(ch))) == NULL) {
612 c->c_errors++;
613 i = 255;
614 }
615 else {
616 i = com_addconst(c, v);
617 DECREF(v);
618 }
619 com_addoparg(c, LOAD_CONST, i);
620 break;
621 case STRING:
622 if ((v = parsestr(STR(ch))) == NULL) {
623 c->c_errors++;
624 i = 255;
625 }
626 else {
627 i = com_addconst(c, v);
628 DECREF(v);
629 }
630 com_addoparg(c, LOAD_CONST, i);
631 break;
632 case NAME:
633 com_addopname(c, LOAD_NAME, ch);
634 break;
635 default:
636 fprintf(stderr, "node type %d\n", TYPE(ch));
637 err_setstr(SystemError, "com_atom: unexpected node type");
638 c->c_errors++;
639 }
640}
641
642static void
643com_slice(c, n, op)
644 struct compiling *c;
645 node *n;
646 int op;
647{
648 if (NCH(n) == 1) {
649 com_addbyte(c, op);
650 }
651 else if (NCH(n) == 2) {
652 if (TYPE(CHILD(n, 0)) != COLON) {
653 com_node(c, CHILD(n, 0));
654 com_addbyte(c, op+1);
655 }
656 else {
657 com_node(c, CHILD(n, 1));
658 com_addbyte(c, op+2);
659 }
660 }
661 else {
662 com_node(c, CHILD(n, 0));
663 com_node(c, CHILD(n, 2));
664 com_addbyte(c, op+3);
665 }
666}
667
668static void
669com_apply_subscript(c, n)
670 struct compiling *c;
671 node *n;
672{
673 REQ(n, subscript);
674 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) {
675 /* It's a single subscript */
676 com_node(c, CHILD(n, 0));
677 com_addbyte(c, BINARY_SUBSCR);
678 }
679 else {
680 /* It's a slice: [expr] ':' [expr] */
681 com_slice(c, n, SLICE);
682 }
683}
684
685static void
686com_call_function(c, n)
687 struct compiling *c;
688 node *n; /* EITHER testlist OR ')' */
689{
690 if (TYPE(n) == RPAR) {
Guido van Rossum288a60f1991-12-16 13:05:10 +0000691 com_addoparg(c, BUILD_TUPLE, 0);
692 com_addbyte(c, BINARY_CALL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000693 }
694 else {
Guido van Rossum288a60f1991-12-16 13:05:10 +0000695 REQ(n, testlist);
696 com_list(c, n, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000697 com_addbyte(c, BINARY_CALL);
698 }
699}
700
701static void
702com_select_member(c, n)
703 struct compiling *c;
704 node *n;
705{
706 com_addopname(c, LOAD_ATTR, n);
707}
708
709static void
710com_apply_trailer(c, n)
711 struct compiling *c;
712 node *n;
713{
714 REQ(n, trailer);
715 switch (TYPE(CHILD(n, 0))) {
716 case LPAR:
717 com_call_function(c, CHILD(n, 1));
718 break;
719 case DOT:
720 com_select_member(c, CHILD(n, 1));
721 break;
722 case LSQB:
723 com_apply_subscript(c, CHILD(n, 1));
724 break;
725 default:
726 err_setstr(SystemError,
727 "com_apply_trailer: unknown trailer type");
728 c->c_errors++;
729 }
730}
731
732static void
733com_factor(c, n)
734 struct compiling *c;
735 node *n;
736{
737 int i;
738 REQ(n, factor);
739 if (TYPE(CHILD(n, 0)) == PLUS) {
740 com_factor(c, CHILD(n, 1));
741 com_addbyte(c, UNARY_POSITIVE);
742 }
743 else if (TYPE(CHILD(n, 0)) == MINUS) {
744 com_factor(c, CHILD(n, 1));
745 com_addbyte(c, UNARY_NEGATIVE);
746 }
Guido van Rossum7928cd71991-10-24 14:59:31 +0000747 else if (TYPE(CHILD(n, 0)) == TILDE) {
748 com_factor(c, CHILD(n, 1));
749 com_addbyte(c, UNARY_INVERT);
750 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000751 else {
752 com_atom(c, CHILD(n, 0));
753 for (i = 1; i < NCH(n); i++)
754 com_apply_trailer(c, CHILD(n, i));
755 }
756}
757
758static void
759com_term(c, n)
760 struct compiling *c;
761 node *n;
762{
763 int i;
764 int op;
765 REQ(n, term);
766 com_factor(c, CHILD(n, 0));
767 for (i = 2; i < NCH(n); i += 2) {
768 com_factor(c, CHILD(n, i));
769 switch (TYPE(CHILD(n, i-1))) {
770 case STAR:
771 op = BINARY_MULTIPLY;
772 break;
773 case SLASH:
774 op = BINARY_DIVIDE;
775 break;
776 case PERCENT:
777 op = BINARY_MODULO;
778 break;
779 default:
780 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +0000781 "com_term: operator not *, / or %");
782 c->c_errors++;
783 op = 255;
784 }
785 com_addbyte(c, op);
786 }
787}
788
789static void
790com_arith_expr(c, n)
791 struct compiling *c;
792 node *n;
793{
794 int i;
795 int op;
796 REQ(n, arith_expr);
797 com_term(c, CHILD(n, 0));
798 for (i = 2; i < NCH(n); i += 2) {
799 com_term(c, CHILD(n, i));
800 switch (TYPE(CHILD(n, i-1))) {
801 case PLUS:
802 op = BINARY_ADD;
803 break;
804 case MINUS:
805 op = BINARY_SUBTRACT;
806 break;
807 default:
808 err_setstr(SystemError,
809 "com_arith_expr: operator not + or -");
810 c->c_errors++;
811 op = 255;
812 }
813 com_addbyte(c, op);
814 }
815}
816
817static void
818com_shift_expr(c, n)
819 struct compiling *c;
820 node *n;
821{
822 int i;
823 int op;
824 REQ(n, shift_expr);
825 com_arith_expr(c, CHILD(n, 0));
826 for (i = 2; i < NCH(n); i += 2) {
827 com_arith_expr(c, CHILD(n, i));
828 switch (TYPE(CHILD(n, i-1))) {
829 case LEFTSHIFT:
830 op = BINARY_LSHIFT;
831 break;
832 case RIGHTSHIFT:
833 op = BINARY_RSHIFT;
834 break;
835 default:
836 err_setstr(SystemError,
837 "com_shift_expr: operator not << or >>");
838 c->c_errors++;
839 op = 255;
840 }
841 com_addbyte(c, op);
842 }
843}
844
845static void
846com_and_expr(c, n)
847 struct compiling *c;
848 node *n;
849{
850 int i;
851 int op;
852 REQ(n, and_expr);
853 com_shift_expr(c, CHILD(n, 0));
854 for (i = 2; i < NCH(n); i += 2) {
855 com_shift_expr(c, CHILD(n, i));
856 if (TYPE(CHILD(n, i-1)) == AMPER) {
857 op = BINARY_AND;
858 }
859 else {
860 err_setstr(SystemError,
861 "com_and_expr: operator not &");
862 c->c_errors++;
863 op = 255;
864 }
865 com_addbyte(c, op);
866 }
867}
868
869static void
870com_xor_expr(c, n)
871 struct compiling *c;
872 node *n;
873{
874 int i;
875 int op;
876 REQ(n, xor_expr);
877 com_and_expr(c, CHILD(n, 0));
878 for (i = 2; i < NCH(n); i += 2) {
879 com_and_expr(c, CHILD(n, i));
880 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
881 op = BINARY_XOR;
882 }
883 else {
884 err_setstr(SystemError,
885 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000886 c->c_errors++;
887 op = 255;
888 }
889 com_addbyte(c, op);
890 }
891}
892
893static void
894com_expr(c, n)
895 struct compiling *c;
896 node *n;
897{
898 int i;
899 int op;
900 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000901 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000902 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +0000903 com_xor_expr(c, CHILD(n, i));
904 if (TYPE(CHILD(n, i-1)) == VBAR) {
905 op = BINARY_OR;
906 }
907 else {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000908 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +0000909 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000910 c->c_errors++;
911 op = 255;
912 }
913 com_addbyte(c, op);
914 }
915}
916
917static enum cmp_op
918cmp_type(n)
919 node *n;
920{
921 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +0000922 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000923 | 'in' | 'not' 'in' | 'is' | 'is' not' */
924 if (NCH(n) == 1) {
925 n = CHILD(n, 0);
926 switch (TYPE(n)) {
927 case LESS: return LT;
928 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +0000929 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000930 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +0000931 case LESSEQUAL: return LE;
932 case GREATEREQUAL: return GE;
933 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000934 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
935 if (strcmp(STR(n), "is") == 0) return IS;
936 }
937 }
938 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000939 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000940 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
941 return NOT_IN;
942 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
943 return IS_NOT;
944 }
945 }
946 return BAD;
947}
948
949static void
950com_comparison(c, n)
951 struct compiling *c;
952 node *n;
953{
954 int i;
955 enum cmp_op op;
956 int anchor;
957 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
958 com_expr(c, CHILD(n, 0));
959 if (NCH(n) == 1)
960 return;
961
962 /****************************************************************
963 The following code is generated for all but the last
964 comparison in a chain:
965
966 label: on stack: opcode: jump to:
967
968 a <code to load b>
969 a, b DUP_TOP
970 a, b, b ROT_THREE
971 b, a, b COMPARE_OP
972 b, 0-or-1 JUMP_IF_FALSE L1
973 b, 1 POP_TOP
974 b
975
976 We are now ready to repeat this sequence for the next
977 comparison in the chain.
978
979 For the last we generate:
980
981 b <code to load c>
982 b, c COMPARE_OP
983 0-or-1
984
985 If there were any jumps to L1 (i.e., there was more than one
986 comparison), we generate:
987
988 0-or-1 JUMP_FORWARD L2
989 L1: b, 0 ROT_TWO
990 0, b POP_TOP
991 0
992 L2:
993 ****************************************************************/
994
995 anchor = 0;
996
997 for (i = 2; i < NCH(n); i += 2) {
998 com_expr(c, CHILD(n, i));
999 if (i+2 < NCH(n)) {
1000 com_addbyte(c, DUP_TOP);
1001 com_addbyte(c, ROT_THREE);
1002 }
1003 op = cmp_type(CHILD(n, i-1));
1004 if (op == BAD) {
1005 err_setstr(SystemError,
1006 "com_comparison: unknown comparison op");
1007 c->c_errors++;
1008 }
1009 com_addoparg(c, COMPARE_OP, op);
1010 if (i+2 < NCH(n)) {
1011 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1012 com_addbyte(c, POP_TOP);
1013 }
1014 }
1015
1016 if (anchor) {
1017 int anchor2 = 0;
1018 com_addfwref(c, JUMP_FORWARD, &anchor2);
1019 com_backpatch(c, anchor);
1020 com_addbyte(c, ROT_TWO);
1021 com_addbyte(c, POP_TOP);
1022 com_backpatch(c, anchor2);
1023 }
1024}
1025
1026static void
1027com_not_test(c, n)
1028 struct compiling *c;
1029 node *n;
1030{
1031 REQ(n, not_test); /* 'not' not_test | comparison */
1032 if (NCH(n) == 1) {
1033 com_comparison(c, CHILD(n, 0));
1034 }
1035 else {
1036 com_not_test(c, CHILD(n, 1));
1037 com_addbyte(c, UNARY_NOT);
1038 }
1039}
1040
1041static void
1042com_and_test(c, n)
1043 struct compiling *c;
1044 node *n;
1045{
1046 int i;
1047 int anchor;
1048 REQ(n, and_test); /* not_test ('and' not_test)* */
1049 anchor = 0;
1050 i = 0;
1051 for (;;) {
1052 com_not_test(c, CHILD(n, i));
1053 if ((i += 2) >= NCH(n))
1054 break;
1055 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1056 com_addbyte(c, POP_TOP);
1057 }
1058 if (anchor)
1059 com_backpatch(c, anchor);
1060}
1061
1062static void
1063com_test(c, n)
1064 struct compiling *c;
1065 node *n;
1066{
1067 int i;
1068 int anchor;
1069 REQ(n, test); /* and_test ('and' and_test)* */
1070 anchor = 0;
1071 i = 0;
1072 for (;;) {
1073 com_and_test(c, CHILD(n, i));
1074 if ((i += 2) >= NCH(n))
1075 break;
1076 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1077 com_addbyte(c, POP_TOP);
1078 }
1079 if (anchor)
1080 com_backpatch(c, anchor);
1081}
1082
1083static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001084com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001085 struct compiling *c;
1086 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001087 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001088{
1089 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001090 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001091 com_node(c, CHILD(n, 0));
1092 }
1093 else {
1094 int i;
1095 int len;
1096 len = (NCH(n) + 1) / 2;
1097 for (i = 0; i < NCH(n); i += 2)
1098 com_node(c, CHILD(n, i));
1099 com_addoparg(c, BUILD_TUPLE, len);
1100 }
1101}
1102
1103
1104/* Begin of assignment compilation */
1105
1106static void com_assign_name PROTO((struct compiling *, node *, int));
1107static void com_assign PROTO((struct compiling *, node *, int));
1108
1109static void
1110com_assign_attr(c, n, assigning)
1111 struct compiling *c;
1112 node *n;
1113 int assigning;
1114{
1115 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
1116}
1117
1118static void
1119com_assign_slice(c, n, assigning)
1120 struct compiling *c;
1121 node *n;
1122 int assigning;
1123{
1124 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE);
1125}
1126
1127static void
1128com_assign_subscript(c, n, assigning)
1129 struct compiling *c;
1130 node *n;
1131 int assigning;
1132{
1133 com_node(c, n);
1134 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR);
1135}
1136
1137static void
1138com_assign_trailer(c, n, assigning)
1139 struct compiling *c;
1140 node *n;
1141 int assigning;
1142{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001143 REQ(n, trailer);
1144 switch (TYPE(CHILD(n, 0))) {
1145 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001146 err_setstr(SyntaxError, "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001147 c->c_errors++;
1148 break;
1149 case DOT: /* '.' NAME */
1150 com_assign_attr(c, CHILD(n, 1), assigning);
1151 break;
1152 case LSQB: /* '[' subscript ']' */
1153 n = CHILD(n, 1);
1154 REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */
1155 if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON)
1156 com_assign_slice(c, n, assigning);
1157 else
1158 com_assign_subscript(c, CHILD(n, 0), assigning);
1159 break;
1160 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001161 err_setstr(SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001162 c->c_errors++;
1163 }
1164}
1165
1166static void
1167com_assign_tuple(c, n, assigning)
1168 struct compiling *c;
1169 node *n;
1170 int assigning;
1171{
1172 int i;
1173 if (TYPE(n) != testlist)
1174 REQ(n, exprlist);
1175 if (assigning)
1176 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
1177 for (i = 0; i < NCH(n); i += 2)
1178 com_assign(c, CHILD(n, i), assigning);
1179}
1180
1181static void
1182com_assign_list(c, n, assigning)
1183 struct compiling *c;
1184 node *n;
1185 int assigning;
1186{
1187 int i;
1188 if (assigning)
1189 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2);
1190 for (i = 0; i < NCH(n); i += 2)
1191 com_assign(c, CHILD(n, i), assigning);
1192}
1193
1194static void
1195com_assign_name(c, n, assigning)
1196 struct compiling *c;
1197 node *n;
1198 int assigning;
1199{
1200 REQ(n, NAME);
1201 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
1202}
1203
1204static void
1205com_assign(c, n, assigning)
1206 struct compiling *c;
1207 node *n;
1208 int assigning;
1209{
1210 /* Loop to avoid trivial recursion */
1211 for (;;) {
1212 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001213
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001214 case exprlist:
1215 case testlist:
1216 if (NCH(n) > 1) {
1217 com_assign_tuple(c, n, assigning);
1218 return;
1219 }
1220 n = CHILD(n, 0);
1221 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001222
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001223 case test:
1224 case and_test:
1225 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001226 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001227 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001228 case xor_expr:
1229 case and_expr:
1230 case shift_expr:
1231 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001232 case term:
1233 if (NCH(n) > 1) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001234 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001235 "can't assign to operator");
1236 c->c_errors++;
1237 return;
1238 }
1239 n = CHILD(n, 0);
1240 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001241
Guido van Rossum7928cd71991-10-24 14:59:31 +00001242 case factor: /* ('+'|'-'|'~') factor | atom trailer* */
1243 if (TYPE(CHILD(n, 0)) != atom) { /* '+'|'-'|'~' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001244 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001245 "can't assign to operator");
1246 c->c_errors++;
1247 return;
1248 }
1249 if (NCH(n) > 1) { /* trailer present */
1250 int i;
1251 com_node(c, CHILD(n, 0));
1252 for (i = 1; i+1 < NCH(n); i++) {
1253 com_apply_trailer(c, CHILD(n, i));
1254 } /* NB i is still alive */
1255 com_assign_trailer(c,
1256 CHILD(n, i), assigning);
1257 return;
1258 }
1259 n = CHILD(n, 0);
1260 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001261
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001262 case atom:
1263 switch (TYPE(CHILD(n, 0))) {
1264 case LPAR:
1265 n = CHILD(n, 1);
1266 if (TYPE(n) == RPAR) {
1267 /* XXX Should allow () = () ??? */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001268 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001269 "can't assign to ()");
1270 c->c_errors++;
1271 return;
1272 }
1273 break;
1274 case LSQB:
1275 n = CHILD(n, 1);
1276 if (TYPE(n) == RSQB) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001277 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001278 "can't assign to []");
1279 c->c_errors++;
1280 return;
1281 }
1282 com_assign_list(c, n, assigning);
1283 return;
1284 case NAME:
1285 com_assign_name(c, CHILD(n, 0), assigning);
1286 return;
1287 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001288 err_setstr(SyntaxError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +00001289 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001290 c->c_errors++;
1291 return;
1292 }
1293 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001294
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001295 default:
1296 fprintf(stderr, "node type %d\n", TYPE(n));
1297 err_setstr(SystemError, "com_assign: bad node");
1298 c->c_errors++;
1299 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001300
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001301 }
1302 }
1303}
1304
1305static void
1306com_expr_stmt(c, n)
1307 struct compiling *c;
1308 node *n;
1309{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001310 REQ(n, expr_stmt); /* exprlist ('=' exprlist)* */
1311 com_node(c, CHILD(n, NCH(n)-1));
1312 if (NCH(n) == 1) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001313 com_addbyte(c, PRINT_EXPR);
1314 }
1315 else {
1316 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001317 for (i = 0; i < NCH(n)-2; i+=2) {
1318 if (i+2 < NCH(n)-2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001319 com_addbyte(c, DUP_TOP);
1320 com_assign(c, CHILD(n, i), 1/*assign*/);
1321 }
1322 }
1323}
1324
1325static void
1326com_print_stmt(c, n)
1327 struct compiling *c;
1328 node *n;
1329{
1330 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001331 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
1332 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001333 com_node(c, CHILD(n, i));
1334 com_addbyte(c, PRINT_ITEM);
1335 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001336 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001337 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001338 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001339}
1340
1341static void
1342com_return_stmt(c, n)
1343 struct compiling *c;
1344 node *n;
1345{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001346 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347 if (!c->c_infunction) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001348 err_setstr(SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001349 c->c_errors++;
1350 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001351 if (NCH(n) < 2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001352 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1353 else
1354 com_node(c, CHILD(n, 1));
1355 com_addbyte(c, RETURN_VALUE);
1356}
1357
1358static void
1359com_raise_stmt(c, n)
1360 struct compiling *c;
1361 node *n;
1362{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001363 REQ(n, raise_stmt); /* 'raise' test [',' test] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001364 com_node(c, CHILD(n, 1));
1365 if (NCH(n) > 3)
1366 com_node(c, CHILD(n, 3));
1367 else
1368 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1369 com_addbyte(c, RAISE_EXCEPTION);
1370}
1371
1372static void
1373com_import_stmt(c, n)
1374 struct compiling *c;
1375 node *n;
1376{
1377 int i;
1378 REQ(n, import_stmt);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001379 /* 'import' NAME (',' NAME)* |
1380 'from' NAME 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001381 if (STR(CHILD(n, 0))[0] == 'f') {
1382 /* 'from' NAME 'import' ... */
1383 REQ(CHILD(n, 1), NAME);
1384 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
1385 for (i = 3; i < NCH(n); i += 2)
1386 com_addopname(c, IMPORT_FROM, CHILD(n, i));
1387 com_addbyte(c, POP_TOP);
1388 }
1389 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001390 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001391 for (i = 1; i < NCH(n); i += 2) {
1392 com_addopname(c, IMPORT_NAME, CHILD(n, i));
1393 com_addopname(c, STORE_NAME, CHILD(n, i));
1394 }
1395 }
1396}
1397
1398static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001399com_global_stmt(c, n)
1400 struct compiling *c;
1401 node *n;
1402{
1403 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001404 REQ(n, global_stmt);
1405 /* 'global' NAME (',' NAME)* */
1406 for (i = 1; i < NCH(n); i += 2) {
1407 if (dictinsert(c->c_globals, STR(CHILD(n, i)), None) != 0)
1408 c->c_errors++;
1409 }
1410}
1411
1412static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001413com_if_stmt(c, n)
1414 struct compiling *c;
1415 node *n;
1416{
1417 int i;
1418 int anchor = 0;
1419 REQ(n, if_stmt);
1420 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
1421 for (i = 0; i+3 < NCH(n); i+=4) {
1422 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001423 node *ch = CHILD(n, i+1);
1424 if (i > 0)
1425 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001426 com_node(c, CHILD(n, i+1));
1427 com_addfwref(c, JUMP_IF_FALSE, &a);
1428 com_addbyte(c, POP_TOP);
1429 com_node(c, CHILD(n, i+3));
1430 com_addfwref(c, JUMP_FORWARD, &anchor);
1431 com_backpatch(c, a);
1432 com_addbyte(c, POP_TOP);
1433 }
1434 if (i+2 < NCH(n))
1435 com_node(c, CHILD(n, i+2));
1436 com_backpatch(c, anchor);
1437}
1438
1439static void
1440com_while_stmt(c, n)
1441 struct compiling *c;
1442 node *n;
1443{
1444 int break_anchor = 0;
1445 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001446 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001447 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
1448 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001449 block_push(c, SETUP_LOOP);
1450 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001451 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001452 com_node(c, CHILD(n, 1));
1453 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1454 com_addbyte(c, POP_TOP);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001455 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001456 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001457 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001458 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1459 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001460 com_backpatch(c, anchor);
1461 com_addbyte(c, POP_TOP);
1462 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001463 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001464 if (NCH(n) > 4)
1465 com_node(c, CHILD(n, 6));
1466 com_backpatch(c, break_anchor);
1467}
1468
1469static void
1470com_for_stmt(c, n)
1471 struct compiling *c;
1472 node *n;
1473{
1474 object *v;
1475 int break_anchor = 0;
1476 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001477 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001478 REQ(n, for_stmt);
1479 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
1480 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001481 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001482 com_node(c, CHILD(n, 3));
1483 v = newintobject(0L);
1484 if (v == NULL)
1485 c->c_errors++;
1486 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1487 XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001488 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001489 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001490 com_addfwref(c, FOR_LOOP, &anchor);
1491 com_assign(c, CHILD(n, 1), 1/*assigning*/);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001492 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001493 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001494 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001495 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1496 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001497 com_backpatch(c, anchor);
1498 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001499 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001500 if (NCH(n) > 8)
1501 com_node(c, CHILD(n, 8));
1502 com_backpatch(c, break_anchor);
1503}
1504
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001505/* Although 'execpt' and 'finally' clauses can be combined
1506 syntactically, they are compiled separately. In fact,
1507 try: S
1508 except E1: S1
1509 except E2: S2
1510 ...
1511 finally: Sf
1512 is equivalent to
1513 try:
1514 try: S
1515 except E1: S1
1516 except E2: S2
1517 ...
1518 finally: Sf
1519 meaning that the 'finally' clause is entered even if things
1520 go wrong again in an exception handler. Note that this is
1521 not the case for exception handlers: at most one is entered.
1522
1523 Code generated for "try: S finally: Sf" is as follows:
1524
1525 SETUP_FINALLY L
1526 <code for S>
1527 POP_BLOCK
1528 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001529 L: <code for Sf>
1530 END_FINALLY
1531
1532 The special instructions use the block stack. Each block
1533 stack entry contains the instruction that created it (here
1534 SETUP_FINALLY), the level of the value stack at the time the
1535 block stack entry was created, and a label (here L).
1536
1537 SETUP_FINALLY:
1538 Pushes the current value stack level and the label
1539 onto the block stack.
1540 POP_BLOCK:
1541 Pops en entry from the block stack, and pops the value
1542 stack until its level is the same as indicated on the
1543 block stack. (The label is ignored.)
1544 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001545 Pops a variable number of entries from the *value* stack
1546 and re-raises the exception they specify. The number of
1547 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001548
1549 The block stack is unwound when an exception is raised:
1550 when a SETUP_FINALLY entry is found, the exception is pushed
1551 onto the value stack (and the exception condition is cleared),
1552 and the interpreter jumps to the label gotten from the block
1553 stack.
1554
1555 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00001556 (The contents of the value stack is shown in [], with the top
1557 at the right; 'tb' is trace-back info, 'val' the exception's
1558 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001559
1560 Value stack Label Instruction Argument
1561 [] SETUP_EXCEPT L1
1562 [] <code for S>
1563 [] POP_BLOCK
1564 [] JUMP_FORWARD L0
1565
Guido van Rossum3f5da241990-12-20 15:06:42 +00001566 [tb, val, exc] L1: DUP )
1567 [tb, val, exc, exc] <evaluate E1> )
1568 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1569 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1570 [tb, val, exc, 1] POP )
1571 [tb, val, exc] POP
1572 [tb, val] <assign to V1> (or POP if no V1)
1573 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001574 [] <code for S1>
1575 JUMP_FORWARD L0
1576
Guido van Rossum3f5da241990-12-20 15:06:42 +00001577 [tb, val, exc, 0] L2: POP
1578 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001579 .............................etc.......................
1580
Guido van Rossum3f5da241990-12-20 15:06:42 +00001581 [tb, val, exc, 0] Ln+1: POP
1582 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001583
1584 [] L0: <next statement>
1585
1586 Of course, parts are not generated if Vi or Ei is not present.
1587*/
1588
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001589static void
1590com_try_stmt(c, n)
1591 struct compiling *c;
1592 node *n;
1593{
1594 int finally_anchor = 0;
1595 int except_anchor = 0;
1596 REQ(n, try_stmt);
Guido van Rossum94fb82e1992-04-05 14:24:50 +00001597 /* 'try' ':' suite (except_clause ':' suite)*
1598 | 'try' ':' 'finally' ':' suite */
1599
1600 /* XXX This can be simplified because except and finally can
1601 no longer be mixed in a single try statement */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001602
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001603 if (NCH(n) > 3 && TYPE(CHILD(n, NCH(n)-3)) != except_clause) {
1604 /* Have a 'finally' clause */
1605 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001606 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001607 }
1608 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == except_clause) {
1609 /* Have an 'except' clause */
1610 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001611 block_push(c, SETUP_EXCEPT);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001612 }
1613 com_node(c, CHILD(n, 2));
1614 if (except_anchor) {
1615 int end_anchor = 0;
1616 int i;
1617 node *ch;
1618 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001619 block_pop(c, SETUP_EXCEPT);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001620 com_addfwref(c, JUMP_FORWARD, &end_anchor);
1621 com_backpatch(c, except_anchor);
1622 for (i = 3;
1623 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
1624 i += 3) {
1625 /* except_clause: 'except' [expr [',' expr]] */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001626 if (except_anchor == 0) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001627 err_setstr(SyntaxError,
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001628 "default 'except:' must be last");
1629 c->c_errors++;
1630 break;
1631 }
1632 except_anchor = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001633 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001634 if (NCH(ch) > 1) {
1635 com_addbyte(c, DUP_TOP);
1636 com_node(c, CHILD(ch, 1));
1637 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001638 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001639 com_addbyte(c, POP_TOP);
1640 }
1641 com_addbyte(c, POP_TOP);
1642 if (NCH(ch) > 3)
1643 com_assign(c, CHILD(ch, 3), 1/*assigning*/);
1644 else
1645 com_addbyte(c, POP_TOP);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001646 com_addbyte(c, POP_TOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001647 com_node(c, CHILD(n, i+2));
1648 com_addfwref(c, JUMP_FORWARD, &end_anchor);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001649 if (except_anchor) {
1650 com_backpatch(c, except_anchor);
1651 com_addbyte(c, POP_TOP);
1652 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001653 }
1654 com_addbyte(c, END_FINALLY);
1655 com_backpatch(c, end_anchor);
1656 }
1657 if (finally_anchor) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001658 node *ch;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001659 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001660 block_pop(c, SETUP_FINALLY);
1661 block_push(c, END_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001662 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001663 com_backpatch(c, finally_anchor);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001664 ch = CHILD(n, NCH(n)-1);
1665 com_addoparg(c, SET_LINENO, ch->n_lineno);
1666 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001667 com_addbyte(c, END_FINALLY);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001668 block_pop(c, END_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001669 }
1670}
1671
1672static void
1673com_suite(c, n)
1674 struct compiling *c;
1675 node *n;
1676{
1677 REQ(n, suite);
1678 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
1679 if (NCH(n) == 1) {
1680 com_node(c, CHILD(n, 0));
1681 }
1682 else {
1683 int i;
1684 for (i = 0; i < NCH(n); i++) {
1685 node *ch = CHILD(n, i);
1686 if (TYPE(ch) == stmt)
1687 com_node(c, ch);
1688 }
1689 }
1690}
1691
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001692/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001693static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001694com_continue_stmt(c, n)
1695 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001696 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001697{
1698 int i = c->c_nblocks;
1699 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
1700 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1701 }
1702 else {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001703 err_setstr(SyntaxError, "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001704 c->c_errors++;
1705 }
1706 /* XXX Could allow it inside a 'finally' clause
1707 XXX if we could pop the exception still on the stack */
1708}
1709
1710static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001711com_funcdef(c, n)
1712 struct compiling *c;
1713 node *n;
1714{
1715 object *v;
1716 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001717 v = (object *)compile(n, c->c_filename);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001718 if (v == NULL)
1719 c->c_errors++;
1720 else {
1721 int i = com_addconst(c, v);
1722 com_addoparg(c, LOAD_CONST, i);
1723 com_addbyte(c, BUILD_FUNCTION);
1724 com_addopname(c, STORE_NAME, CHILD(n, 1));
1725 DECREF(v);
1726 }
1727}
1728
1729static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001730com_oldbases(c, n)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001731 struct compiling *c;
1732 node *n;
1733{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001734 int i;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001735 REQ(n, baselist);
1736 /*
1737 baselist: atom arguments (',' atom arguments)*
Guido van Rossumc5e96291991-12-10 13:53:51 +00001738 arguments: '(' ')'
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001739 */
1740 for (i = 0; i < NCH(n); i += 3)
1741 com_node(c, CHILD(n, i));
1742 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 3);
1743}
1744
1745static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001746com_newbases(c, n)
1747 struct compiling *c;
1748 node *n;
1749{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001750 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001751 REQ(n, testlist);
1752 /* testlist: test (',' test)* [','] */
1753 for (i = 0; i < NCH(n); i += 2)
1754 com_node(c, CHILD(n, i));
1755 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2);
1756}
1757
1758static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001759com_classdef(c, n)
1760 struct compiling *c;
1761 node *n;
1762{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001763 object *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001764 REQ(n, classdef);
1765 /*
Guido van Rossumc5e96291991-12-10 13:53:51 +00001766 classdef: 'class' NAME
1767 ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001768 baselist: atom arguments (',' atom arguments)*
Guido van Rossumc5e96291991-12-10 13:53:51 +00001769 arguments: '(' ')'
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001770 */
Guido van Rossumc5e96291991-12-10 13:53:51 +00001771 /* This piece of code must push a tuple on the stack (the bases) */
1772 if (TYPE(CHILD(n, 2)) != LPAR) {
1773 /* New syntax without base classes:
1774 class NAME ':' suite
1775 ___________^
1776 */
1777 com_addoparg(c, BUILD_TUPLE, 0);
1778 }
1779 else {
1780 if (TYPE(CHILD(n, 3)) == RPAR) {
1781 /* Old syntax with or without base classes:
1782 class NAME '(' ')' ['=' baselist] ':' suite
1783 _______________^....^...^
1784 */
1785 if (TYPE(CHILD(n, 4)) == EQUAL)
1786 com_oldbases(c, CHILD(n, 5));
1787 else
1788 com_addoparg(c, BUILD_TUPLE, 0);
1789 }
1790 else {
1791 /* New syntax with base classes:
1792 class NAME '(' testlist ')' ':' suite
1793 _______________^
1794 */
1795 com_newbases(c, CHILD(n, 3));
1796 }
1797 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001798 v = (object *)compile(n, c->c_filename);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001799 if (v == NULL)
1800 c->c_errors++;
1801 else {
1802 int i = com_addconst(c, v);
1803 com_addoparg(c, LOAD_CONST, i);
1804 com_addbyte(c, BUILD_FUNCTION);
1805 com_addbyte(c, UNARY_CALL);
1806 com_addbyte(c, BUILD_CLASS);
1807 com_addopname(c, STORE_NAME, CHILD(n, 1));
1808 DECREF(v);
1809 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001810}
1811
1812static void
1813com_node(c, n)
1814 struct compiling *c;
1815 node *n;
1816{
1817 switch (TYPE(n)) {
1818
1819 /* Definition nodes */
1820
1821 case funcdef:
1822 com_funcdef(c, n);
1823 break;
1824 case classdef:
1825 com_classdef(c, n);
1826 break;
1827
1828 /* Trivial parse tree nodes */
1829
1830 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001831 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001832 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001833 com_node(c, CHILD(n, 0));
1834 break;
1835
1836 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001837 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
1838 com_addoparg(c, SET_LINENO, n->n_lineno);
1839 {
1840 int i;
1841 for (i = 0; i < NCH(n)-1; i += 2)
1842 com_node(c, CHILD(n, i));
1843 }
1844 break;
1845
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001846 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001847 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001848 com_node(c, CHILD(n, 0));
1849 break;
1850
1851 /* Statement nodes */
1852
1853 case expr_stmt:
1854 com_expr_stmt(c, n);
1855 break;
1856 case print_stmt:
1857 com_print_stmt(c, n);
1858 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001859 case del_stmt: /* 'del' exprlist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001860 com_assign(c, CHILD(n, 1), 0/*delete*/);
1861 break;
1862 case pass_stmt:
1863 break;
1864 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001865 if (c->c_loops == 0) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001866 err_setstr(SyntaxError, "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001867 c->c_errors++;
1868 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001869 com_addbyte(c, BREAK_LOOP);
1870 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001871 case continue_stmt:
1872 com_continue_stmt(c, n);
1873 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001874 case return_stmt:
1875 com_return_stmt(c, n);
1876 break;
1877 case raise_stmt:
1878 com_raise_stmt(c, n);
1879 break;
1880 case import_stmt:
1881 com_import_stmt(c, n);
1882 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001883 case global_stmt:
1884 com_global_stmt(c, n);
1885 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001886 case if_stmt:
1887 com_if_stmt(c, n);
1888 break;
1889 case while_stmt:
1890 com_while_stmt(c, n);
1891 break;
1892 case for_stmt:
1893 com_for_stmt(c, n);
1894 break;
1895 case try_stmt:
1896 com_try_stmt(c, n);
1897 break;
1898 case suite:
1899 com_suite(c, n);
1900 break;
1901
1902 /* Expression nodes */
1903
1904 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00001905 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001906 break;
1907 case test:
1908 com_test(c, n);
1909 break;
1910 case and_test:
1911 com_and_test(c, n);
1912 break;
1913 case not_test:
1914 com_not_test(c, n);
1915 break;
1916 case comparison:
1917 com_comparison(c, n);
1918 break;
1919 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00001920 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001921 break;
1922 case expr:
1923 com_expr(c, n);
1924 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001925 case xor_expr:
1926 com_xor_expr(c, n);
1927 break;
1928 case and_expr:
1929 com_and_expr(c, n);
1930 break;
1931 case shift_expr:
1932 com_shift_expr(c, n);
1933 break;
1934 case arith_expr:
1935 com_arith_expr(c, n);
1936 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001937 case term:
1938 com_term(c, n);
1939 break;
1940 case factor:
1941 com_factor(c, n);
1942 break;
1943 case atom:
1944 com_atom(c, n);
1945 break;
1946
1947 default:
1948 fprintf(stderr, "node type %d\n", TYPE(n));
1949 err_setstr(SystemError, "com_node: unexpected node type");
1950 c->c_errors++;
1951 }
1952}
1953
1954static void com_fplist PROTO((struct compiling *, node *));
1955
1956static void
1957com_fpdef(c, n)
1958 struct compiling *c;
1959 node *n;
1960{
1961 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
1962 if (TYPE(CHILD(n, 0)) == LPAR)
1963 com_fplist(c, CHILD(n, 1));
1964 else
1965 com_addopname(c, STORE_NAME, CHILD(n, 0));
1966}
1967
1968static void
1969com_fplist(c, n)
1970 struct compiling *c;
1971 node *n;
1972{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00001973 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001974 if (NCH(n) == 1) {
1975 com_fpdef(c, CHILD(n, 0));
1976 }
1977 else {
1978 int i;
1979 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
1980 for (i = 0; i < NCH(n); i += 2)
1981 com_fpdef(c, CHILD(n, i));
1982 }
1983}
1984
1985static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00001986com_arglist(c, n)
1987 struct compiling *c;
1988 node *n;
1989{
1990 int i, nargs, op;
1991 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001992 /* varargslist:
Guido van Rossum94fb82e1992-04-05 14:24:50 +00001993 (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [','] */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00001994 op = UNPACK_ARG;
1995 nargs = (NCH(n) + 1) / 2;
1996 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumacbefef1992-01-19 16:33:51 +00001997 int t = TYPE(CHILD(n, i));
Guido van Rossum94fb82e1992-04-05 14:24:50 +00001998 if (t == STAR) {
Guido van Rossum49d6dc41992-01-14 18:30:26 +00001999 op = UNPACK_VARARG;
2000 nargs = i/2;
2001 break;
2002 }
2003 }
2004 com_addoparg(c, op, nargs);
2005 for (i = 0; i < 2*nargs; i += 2)
2006 com_fpdef(c, CHILD(n, i));
2007 if (op == UNPACK_VARARG)
2008 com_addopname(c, STORE_NAME, CHILD(n, 2*nargs+1));
2009}
2010
2011static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002012com_file_input(c, n)
2013 struct compiling *c;
2014 node *n;
2015{
2016 int i;
2017 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
2018 for (i = 0; i < NCH(n); i++) {
2019 node *ch = CHILD(n, i);
2020 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
2021 com_node(c, ch);
2022 }
2023}
2024
2025/* Top-level compile-node interface */
2026
2027static void
2028compile_funcdef(c, n)
2029 struct compiling *c;
2030 node *n;
2031{
2032 node *ch;
2033 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002034 c->c_name = STR(CHILD(n, 1));
2035 com_addoparg(c, RESERVE_FAST, 0); /* Patched up later */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002036 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
2037 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002038 if (TYPE(ch) == RPAR)
Guido van Rossum288a60f1991-12-16 13:05:10 +00002039 com_addoparg(c, UNPACK_ARG, 0);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002040 else
2041 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002042 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002043 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002044 c->c_infunction = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002045 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2046 com_addbyte(c, RETURN_VALUE);
2047}
2048
2049static void
2050compile_node(c, n)
2051 struct compiling *c;
2052 node *n;
2053{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002054 com_addoparg(c, SET_LINENO, n->n_lineno);
2055
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002056 switch (TYPE(n)) {
2057
Guido van Rossum4c417781991-01-21 16:09:22 +00002058 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002059 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
2060 n = CHILD(n, 0);
2061 if (TYPE(n) != NEWLINE)
2062 com_node(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002063 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2064 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002065 break;
2066
Guido van Rossum4c417781991-01-21 16:09:22 +00002067 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002068 com_file_input(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002069 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2070 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002071 break;
2072
Guido van Rossum4c417781991-01-21 16:09:22 +00002073 case expr_input: /* Built-in function eval() */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002074 com_node(c, CHILD(n, 0));
2075 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002076 break;
2077
Guido van Rossum4c417781991-01-21 16:09:22 +00002078 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00002079 com_node(c, CHILD(n, 0));
2080 com_addbyte(c, RETURN_VALUE);
2081 break;
2082
2083 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002084 compile_funcdef(c, n);
2085 break;
2086
Guido van Rossum4c417781991-01-21 16:09:22 +00002087 case classdef: /* A class definition */
Guido van Rossumc5e96291991-12-10 13:53:51 +00002088 /* classdef: 'class' NAME
2089 ['(' testlist ')' |'(' ')' ['=' baselist]]
2090 ':' suite */
Guido van Rossumc5e96291991-12-10 13:53:51 +00002091 com_node(c, CHILD(n, NCH(n)-1)); /* The suite */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002092 com_addbyte(c, LOAD_LOCALS);
2093 com_addbyte(c, RETURN_VALUE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002094 break;
2095
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002096 default:
2097 fprintf(stderr, "node type %d\n", TYPE(n));
2098 err_setstr(SystemError, "compile_node: unexpected node type");
2099 c->c_errors++;
2100 }
2101}
2102
Guido van Rossum282914b1991-04-04 10:42:56 +00002103/* Optimization for local and global variables.
2104
Guido van Rossum9bfef441993-03-29 10:43:31 +00002105 XXX Need to update this text for LOAD_FAST stuff...
2106
Guido van Rossum282914b1991-04-04 10:42:56 +00002107 Attempt to replace all LOAD_NAME instructions that refer to a local
2108 variable with LOAD_LOCAL instructions, and all that refer to a global
2109 variable with LOAD_GLOBAL instructions.
2110
2111 To find all local variables, we check all STORE_NAME and IMPORT_FROM
2112 instructions. This yields all local variables, including arguments,
2113 function definitions, class definitions and import statements.
2114
2115 There is one leak: 'from foo import *' introduces local variables
2116 that we can't know while compiling. If this is the case, LOAD_GLOBAL
2117 instructions are not generated -- LOAD_NAME is left in place for
2118 globals, since it first checks for globals (LOAD_LOCAL is still used
2119 for recognized locals, since it doesn't hurt).
2120
2121 This optimization means that using the same name as a global and
2122 as a local variable within the same scope is now illegal, which
2123 is a change to the language! Also using eval() to introduce new
2124 local variables won't work. But both were bad practice at best.
2125
2126 The optimization doesn't save much: basically, it saves one
2127 unsuccessful dictionary lookup per global (or built-in) variable
2128 reference. On the (slow!) Mac Plus, with 4 local variables,
2129 this saving was measured to be about 0.18 ms. We might save more
2130 by using a different data structure to hold local variables, like
2131 an array indexed by variable number.
2132
2133 NB: this modifies the string object co->co_code!
2134*/
2135
2136static void
2137optimizer(co)
2138 codeobject *co;
2139{
Guido van Rossum0a697f61991-04-16 08:39:12 +00002140 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00002141 object *locals;
2142 int opcode;
2143 int oparg;
2144 object *name;
2145 int star_used;
Guido van Rossum0a697f61991-04-16 08:39:12 +00002146
Guido van Rossum282914b1991-04-04 10:42:56 +00002147#define NEXTOP() (*next_instr++)
2148#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
2149#define GETITEM(v, i) (getlistitem((v), (i)))
2150#define GETNAMEOBJ(i) (GETITEM(co->co_names, (i)))
2151
2152 locals = newdictobject();
2153 if (locals == NULL) {
2154 err_clear();
2155 return; /* For now, this is OK */
2156 }
2157
Guido van Rossum0a697f61991-04-16 08:39:12 +00002158 next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002159 for (;;) {
2160 opcode = NEXTOP();
2161 if (opcode == STOP_CODE)
2162 break;
2163 if (HAS_ARG(opcode))
2164 oparg = NEXTARG();
2165 if (opcode == STORE_NAME || opcode == IMPORT_FROM) {
2166 name = GETNAMEOBJ(oparg);
2167 if (dict2insert(locals, name, None) != 0) {
2168 DECREF(locals);
2169 return; /* Sorry */
2170 }
2171 }
2172 }
2173
2174 star_used = (dictlookup(locals, "*") != NULL);
Guido van Rossum0a697f61991-04-16 08:39:12 +00002175 next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002176 for (;;) {
2177 cur_instr = next_instr;
2178 opcode = NEXTOP();
2179 if (opcode == STOP_CODE)
2180 break;
2181 if (HAS_ARG(opcode))
2182 oparg = NEXTARG();
2183 if (opcode == LOAD_NAME) {
2184 name = GETNAMEOBJ(oparg);
Guido van Rossum83163251991-08-16 08:58:43 +00002185 if (dict2lookup(locals, name) != NULL)
Guido van Rossum282914b1991-04-04 10:42:56 +00002186 *cur_instr = LOAD_LOCAL;
Guido van Rossum83163251991-08-16 08:58:43 +00002187 else {
2188 err_clear();
2189 if (!star_used)
2190 *cur_instr = LOAD_GLOBAL;
2191 }
Guido van Rossum282914b1991-04-04 10:42:56 +00002192 }
2193 }
2194
2195 DECREF(locals);
2196}
2197
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002198codeobject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002199compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002200 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002201 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002202{
2203 struct compiling sc;
2204 codeobject *co;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002205 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002206 return NULL;
2207 compile_node(&sc, n);
2208 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002209 co = NULL;
2210 if (sc.c_errors == 0) {
2211 object *v, *w;
2212 v = newstringobject(sc.c_filename);
2213 w = newstringobject(sc.c_name);
2214 if (v != NULL && w != NULL)
2215 co = newcodeobject(sc.c_code, sc.c_consts,
2216 sc.c_names, v, w);
2217 XDECREF(v);
2218 XDECREF(w);
Guido van Rossuma082ce41991-06-04 19:41:56 +00002219 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002220 com_free(&sc);
Guido van Rossumfb8edfc1991-05-14 11:56:03 +00002221 if (co != NULL && filename[0] != '<')
Guido van Rossum282914b1991-04-04 10:42:56 +00002222 optimizer(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002223 return co;
2224}