Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 25 | /* Compile an expression node to intermediate code */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | /* 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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 33 | #include "allobjects.h" |
| 34 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 35 | #include "node.h" |
| 36 | #include "token.h" |
| 37 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 38 | #include "compile.h" |
| 39 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | #include "structmember.h" |
| 41 | |
| 42 | #include <ctype.h> |
| 43 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 44 | extern int errno; |
| 45 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | #define OFF(x) offsetof(codeobject, x) |
| 47 | |
| 48 | static struct memberlist code_memberlist[] = { |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 49 | {"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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 53 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 54 | {NULL} /* Sentinel */ |
| 55 | }; |
| 56 | |
| 57 | static object * |
| 58 | code_getattr(co, name) |
| 59 | codeobject *co; |
| 60 | char *name; |
| 61 | { |
| 62 | return getmember((char *)co, code_memberlist, name); |
| 63 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 64 | |
| 65 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 66 | code_dealloc(co) |
| 67 | codeobject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 68 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 69 | XDECREF(co->co_code); |
| 70 | XDECREF(co->co_consts); |
| 71 | XDECREF(co->co_names); |
| 72 | XDECREF(co->co_filename); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 73 | XDECREF(co->co_name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 74 | DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 77 | static object * |
| 78 | code_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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 85 | char *name = "???"; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 86 | 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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 90 | 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 Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 94 | return newstringobject(buf); |
| 95 | } |
| 96 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 97 | typeobject 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 105 | code_getattr, /*tp_getattr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 106 | 0, /*tp_setattr*/ |
| 107 | 0, /*tp_compare*/ |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 108 | code_repr, /*tp_repr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 109 | 0, /*tp_as_number*/ |
| 110 | 0, /*tp_as_sequence*/ |
| 111 | 0, /*tp_as_mapping*/ |
| 112 | }; |
| 113 | |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 114 | codeobject * |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 115 | newcodeobject(code, consts, names, filename, name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 116 | object *code; |
| 117 | object *consts; |
| 118 | object *names; |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 119 | object *filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 120 | object *name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 121 | { |
| 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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 127 | names == NULL || !is_listobject(names) || |
| 128 | name == NULL || !is_stringobject(name)) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 129 | 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 Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 148 | INCREF(filename); |
| 149 | co->co_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 150 | INCREF(name); |
| 151 | co->co_name = name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 152 | } |
| 153 | return co; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 158 | |
| 159 | #define MAXBLOCKS 20 /* Max static block nesting within a function */ |
| 160 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 161 | struct compiling { |
| 162 | object *c_code; /* string */ |
| 163 | object *c_consts; /* list of objects */ |
| 164 | object *c_names; /* list of strings (names) */ |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 165 | object *c_globals; /* dictionary */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 166 | int c_nexti; /* index into c_code */ |
| 167 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 168 | int c_infunction; /* set when compiling a function */ |
| 169 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 170 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 173 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 174 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 177 | |
| 178 | /* Interface to the block stack */ |
| 179 | |
| 180 | static void |
| 181 | block_push(c, type) |
| 182 | struct compiling *c; |
| 183 | int type; |
| 184 | { |
| 185 | if (c->c_nblocks >= MAXBLOCKS) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 186 | err_setstr(SystemError, "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 187 | c->c_errors++; |
| 188 | } |
| 189 | else { |
| 190 | c->c_block[c->c_nblocks++] = type; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | block_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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 208 | /* Prototypes */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 210 | static int com_init PROTO((struct compiling *, char *)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 211 | static void com_free PROTO((struct compiling *)); |
| 212 | static void com_done PROTO((struct compiling *)); |
| 213 | static void com_node PROTO((struct compiling *, struct _node *)); |
| 214 | static void com_addbyte PROTO((struct compiling *, int)); |
| 215 | static void com_addint PROTO((struct compiling *, int)); |
| 216 | static void com_addoparg PROTO((struct compiling *, int, int)); |
| 217 | static void com_addfwref PROTO((struct compiling *, int, int *)); |
| 218 | static void com_backpatch PROTO((struct compiling *, int)); |
| 219 | static int com_add PROTO((struct compiling *, object *, object *)); |
| 220 | static int com_addconst PROTO((struct compiling *, object *)); |
| 221 | static int com_addname PROTO((struct compiling *, object *)); |
| 222 | static void com_addopname PROTO((struct compiling *, int, node *)); |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 223 | static void com_list PROTO((struct compiling *, node *, int)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 224 | |
| 225 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 226 | com_init(c, filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 227 | struct compiling *c; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 228 | char *filename; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 229 | { |
Guido van Rossum | 62d4624 | 1991-04-03 19:00:23 +0000 | [diff] [blame] | 230 | if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 231 | 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 Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 236 | if ((c->c_globals = newdictobject()) == NULL) |
| 237 | goto fail_0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 238 | c->c_nexti = 0; |
| 239 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | c->c_infunction = 0; |
| 241 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 242 | c->c_begin = 0; |
| 243 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 244 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 245 | c->c_name = "?"; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 246 | return 1; |
| 247 | |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 248 | fail_0: |
| 249 | DECREF(c->c_names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 250 | fail_1: |
| 251 | DECREF(c->c_consts); |
| 252 | fail_2: |
| 253 | DECREF(c->c_code); |
| 254 | fail_3: |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static void |
| 259 | com_free(c) |
| 260 | struct compiling *c; |
| 261 | { |
| 262 | XDECREF(c->c_code); |
| 263 | XDECREF(c->c_consts); |
| 264 | XDECREF(c->c_names); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 265 | XDECREF(c->c_globals); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void |
| 269 | com_done(c) |
| 270 | struct compiling *c; |
| 271 | { |
| 272 | if (c->c_code != NULL) |
| 273 | resizestring(&c->c_code, c->c_nexti); |
| 274 | } |
| 275 | |
| 276 | static void |
| 277 | com_addbyte(c, byte) |
| 278 | struct compiling *c; |
| 279 | int byte; |
| 280 | { |
| 281 | int len; |
| 282 | if (byte < 0 || byte > 255) { |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 283 | /* |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 284 | fprintf(stderr, "XXX compiling bad byte: %d\n", byte); |
| 285 | abort(); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 286 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 287 | 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 | |
| 302 | static void |
| 303 | com_addint(c, x) |
| 304 | struct compiling *c; |
| 305 | int x; |
| 306 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 307 | com_addbyte(c, x & 0xff); |
| 308 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void |
| 312 | com_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 | |
| 321 | static void |
| 322 | com_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 | |
| 337 | static void |
| 338 | com_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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 344 | int dist; |
| 345 | int prev; |
| 346 | for (;;) { |
| 347 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 348 | 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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 352 | if (!prev) |
| 353 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 354 | anchor -= prev; |
| 355 | } |
| 356 | } |
| 357 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 358 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 359 | |
| 360 | static int |
| 361 | com_add(c, list, v) |
| 362 | struct compiling *c; |
| 363 | object *list; |
| 364 | object *v; |
| 365 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 366 | int n = getlistsize(list); |
| 367 | int i; |
| 368 | for (i = n; --i >= 0; ) { |
| 369 | object *w = getlistitem(list, i); |
Guido van Rossum | efc0bd0 | 1991-07-01 18:44:20 +0000 | [diff] [blame] | 370 | if (v->ob_type == w->ob_type && cmpobject(v, w) == 0) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 371 | return i; |
| 372 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 373 | if (addlistitem(list, v) != 0) |
| 374 | c->c_errors++; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 375 | return n; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int |
| 379 | com_addconst(c, v) |
| 380 | struct compiling *c; |
| 381 | object *v; |
| 382 | { |
| 383 | return com_add(c, c->c_consts, v); |
| 384 | } |
| 385 | |
| 386 | static int |
| 387 | com_addname(c, v) |
| 388 | struct compiling *c; |
| 389 | object *v; |
| 390 | { |
| 391 | return com_add(c, c->c_names, v); |
| 392 | } |
| 393 | |
| 394 | static void |
| 395 | com_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 Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 417 | /* 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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 430 | com_addoparg(c, op, i); |
| 431 | } |
| 432 | |
| 433 | static object * |
| 434 | parsenumber(s) |
| 435 | char *s; |
| 436 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 437 | 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 Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 440 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 441 | long x; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 442 | double xx; |
| 443 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 444 | end = s + strlen(s) - 1; |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 445 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 446 | return long_scan(s, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 447 | if (s[0] == '0') |
| 448 | x = (long) strtoul(s, &end, 0); |
| 449 | else |
| 450 | x = strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 451 | if (*end == '\0') { |
| 452 | if (errno != 0) { |
Guido van Rossum | efb087b | 1991-12-16 15:41:41 +0000 | [diff] [blame] | 453 | err_setstr(OverflowError, |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 454 | "integer literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 455 | return NULL; |
| 456 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 457 | return newintobject(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 458 | } |
| 459 | errno = 0; |
| 460 | xx = strtod(s, &end); |
| 461 | if (*end == '\0') { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 462 | #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 Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 466 | if (errno != 0) { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 467 | err_setstr(OverflowError, "float literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 468 | return NULL; |
| 469 | } |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 470 | #endif |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 471 | return newfloatobject(xx); |
| 472 | } |
Guido van Rossum | efb087b | 1991-12-16 15:41:41 +0000 | [diff] [blame] | 473 | err_setstr(SystemError, "bad number syntax?!?!"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | static object * |
| 478 | parsestr(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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 516 | 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 | |
| 544 | static void |
| 545 | com_list_constructor(c, n) |
| 546 | struct compiling *c; |
| 547 | node *n; |
| 548 | { |
| 549 | int len; |
| 550 | int i; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 551 | 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 | |
| 560 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 561 | com_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 | |
| 578 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 579 | com_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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 601 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 602 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 603 | if (TYPE(CHILD(n, 1)) != RBRACE) |
| 604 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 605 | 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 | |
| 642 | static void |
| 643 | com_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 | |
| 668 | static void |
| 669 | com_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 | |
| 685 | static void |
| 686 | com_call_function(c, n) |
| 687 | struct compiling *c; |
| 688 | node *n; /* EITHER testlist OR ')' */ |
| 689 | { |
| 690 | if (TYPE(n) == RPAR) { |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 691 | com_addoparg(c, BUILD_TUPLE, 0); |
| 692 | com_addbyte(c, BINARY_CALL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 693 | } |
| 694 | else { |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 695 | REQ(n, testlist); |
| 696 | com_list(c, n, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 697 | com_addbyte(c, BINARY_CALL); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | static void |
| 702 | com_select_member(c, n) |
| 703 | struct compiling *c; |
| 704 | node *n; |
| 705 | { |
| 706 | com_addopname(c, LOAD_ATTR, n); |
| 707 | } |
| 708 | |
| 709 | static void |
| 710 | com_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 | |
| 732 | static void |
| 733 | com_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 Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 747 | else if (TYPE(CHILD(n, 0)) == TILDE) { |
| 748 | com_factor(c, CHILD(n, 1)); |
| 749 | com_addbyte(c, UNARY_INVERT); |
| 750 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 751 | 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 | |
| 758 | static void |
| 759 | com_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 Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 781 | "com_term: operator not *, / or %"); |
| 782 | c->c_errors++; |
| 783 | op = 255; |
| 784 | } |
| 785 | com_addbyte(c, op); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | static void |
| 790 | com_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 | |
| 817 | static void |
| 818 | com_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 | |
| 845 | static void |
| 846 | com_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 | |
| 869 | static void |
| 870 | com_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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 886 | c->c_errors++; |
| 887 | op = 255; |
| 888 | } |
| 889 | com_addbyte(c, op); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | static void |
| 894 | com_expr(c, n) |
| 895 | struct compiling *c; |
| 896 | node *n; |
| 897 | { |
| 898 | int i; |
| 899 | int op; |
| 900 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 901 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 902 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 903 | com_xor_expr(c, CHILD(n, i)); |
| 904 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 905 | op = BINARY_OR; |
| 906 | } |
| 907 | else { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 908 | err_setstr(SystemError, |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 909 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 910 | c->c_errors++; |
| 911 | op = 255; |
| 912 | } |
| 913 | com_addbyte(c, op); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | static enum cmp_op |
| 918 | cmp_type(n) |
| 919 | node *n; |
| 920 | { |
| 921 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 922 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 923 | | '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 Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 929 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 930 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 931 | case LESSEQUAL: return LE; |
| 932 | case GREATEREQUAL: return GE; |
| 933 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 934 | 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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 939 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 940 | 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 | |
| 949 | static void |
| 950 | com_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 | |
| 1026 | static void |
| 1027 | com_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 | |
| 1041 | static void |
| 1042 | com_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 | |
| 1062 | static void |
| 1063 | com_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 | |
| 1083 | static void |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1084 | com_list(c, n, toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1085 | struct compiling *c; |
| 1086 | node *n; |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1087 | int toplevel; /* If nonzero, *always* build a tuple */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1088 | { |
| 1089 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1090 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1091 | 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 | |
| 1106 | static void com_assign_name PROTO((struct compiling *, node *, int)); |
| 1107 | static void com_assign PROTO((struct compiling *, node *, int)); |
| 1108 | |
| 1109 | static void |
| 1110 | com_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 | |
| 1118 | static void |
| 1119 | com_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 | |
| 1127 | static void |
| 1128 | com_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 | |
| 1137 | static void |
| 1138 | com_assign_trailer(c, n, assigning) |
| 1139 | struct compiling *c; |
| 1140 | node *n; |
| 1141 | int assigning; |
| 1142 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1143 | REQ(n, trailer); |
| 1144 | switch (TYPE(CHILD(n, 0))) { |
| 1145 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1146 | err_setstr(SyntaxError, "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1147 | 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 Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1161 | err_setstr(SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1162 | c->c_errors++; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | static void |
| 1167 | com_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 | |
| 1181 | static void |
| 1182 | com_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 | |
| 1194 | static void |
| 1195 | com_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 | |
| 1204 | static void |
| 1205 | com_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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1213 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1214 | 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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1222 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1223 | case test: |
| 1224 | case and_test: |
| 1225 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1226 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1227 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1228 | case xor_expr: |
| 1229 | case and_expr: |
| 1230 | case shift_expr: |
| 1231 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1232 | case term: |
| 1233 | if (NCH(n) > 1) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1234 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1235 | "can't assign to operator"); |
| 1236 | c->c_errors++; |
| 1237 | return; |
| 1238 | } |
| 1239 | n = CHILD(n, 0); |
| 1240 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1241 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1242 | case factor: /* ('+'|'-'|'~') factor | atom trailer* */ |
| 1243 | if (TYPE(CHILD(n, 0)) != atom) { /* '+'|'-'|'~' */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1244 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1245 | "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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1262 | 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 Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1268 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1269 | "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 Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1277 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1278 | "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 Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1288 | err_setstr(SyntaxError, |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 1289 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1290 | c->c_errors++; |
| 1291 | return; |
| 1292 | } |
| 1293 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1295 | 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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1300 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | static void |
| 1306 | com_expr_stmt(c, n) |
| 1307 | struct compiling *c; |
| 1308 | node *n; |
| 1309 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1310 | REQ(n, expr_stmt); /* exprlist ('=' exprlist)* */ |
| 1311 | com_node(c, CHILD(n, NCH(n)-1)); |
| 1312 | if (NCH(n) == 1) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1313 | com_addbyte(c, PRINT_EXPR); |
| 1314 | } |
| 1315 | else { |
| 1316 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1317 | for (i = 0; i < NCH(n)-2; i+=2) { |
| 1318 | if (i+2 < NCH(n)-2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1319 | com_addbyte(c, DUP_TOP); |
| 1320 | com_assign(c, CHILD(n, i), 1/*assign*/); |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | static void |
| 1326 | com_print_stmt(c, n) |
| 1327 | struct compiling *c; |
| 1328 | node *n; |
| 1329 | { |
| 1330 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1331 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
| 1332 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1333 | com_node(c, CHILD(n, i)); |
| 1334 | com_addbyte(c, PRINT_ITEM); |
| 1335 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1336 | if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1337 | com_addbyte(c, PRINT_NEWLINE); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1338 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | static void |
| 1342 | com_return_stmt(c, n) |
| 1343 | struct compiling *c; |
| 1344 | node *n; |
| 1345 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1346 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1347 | if (!c->c_infunction) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1348 | err_setstr(SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1349 | c->c_errors++; |
| 1350 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1351 | if (NCH(n) < 2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1352 | 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 | |
| 1358 | static void |
| 1359 | com_raise_stmt(c, n) |
| 1360 | struct compiling *c; |
| 1361 | node *n; |
| 1362 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1363 | REQ(n, raise_stmt); /* 'raise' test [',' test] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1364 | 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 | |
| 1372 | static void |
| 1373 | com_import_stmt(c, n) |
| 1374 | struct compiling *c; |
| 1375 | node *n; |
| 1376 | { |
| 1377 | int i; |
| 1378 | REQ(n, import_stmt); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1379 | /* 'import' NAME (',' NAME)* | |
| 1380 | 'from' NAME 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1381 | 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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1390 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1391 | 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 | |
| 1398 | static void |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1399 | com_global_stmt(c, n) |
| 1400 | struct compiling *c; |
| 1401 | node *n; |
| 1402 | { |
| 1403 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1404 | 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 | |
| 1412 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1413 | com_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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1423 | node *ch = CHILD(n, i+1); |
| 1424 | if (i > 0) |
| 1425 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1426 | 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 | |
| 1439 | static void |
| 1440 | com_while_stmt(c, n) |
| 1441 | struct compiling *c; |
| 1442 | node *n; |
| 1443 | { |
| 1444 | int break_anchor = 0; |
| 1445 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1446 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1447 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 1448 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1449 | block_push(c, SETUP_LOOP); |
| 1450 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1451 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1452 | com_node(c, CHILD(n, 1)); |
| 1453 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1454 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1455 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1456 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1457 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1458 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1459 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1460 | com_backpatch(c, anchor); |
| 1461 | com_addbyte(c, POP_TOP); |
| 1462 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1463 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1464 | if (NCH(n) > 4) |
| 1465 | com_node(c, CHILD(n, 6)); |
| 1466 | com_backpatch(c, break_anchor); |
| 1467 | } |
| 1468 | |
| 1469 | static void |
| 1470 | com_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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1477 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1478 | REQ(n, for_stmt); |
| 1479 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 1480 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1481 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1482 | 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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1488 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1489 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1490 | com_addfwref(c, FOR_LOOP, &anchor); |
| 1491 | com_assign(c, CHILD(n, 1), 1/*assigning*/); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1492 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1493 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1494 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1495 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1496 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1497 | com_backpatch(c, anchor); |
| 1498 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1499 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1500 | if (NCH(n) > 8) |
| 1501 | com_node(c, CHILD(n, 8)); |
| 1502 | com_backpatch(c, break_anchor); |
| 1503 | } |
| 1504 | |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1505 | /* 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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1529 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1545 | 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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1548 | |
| 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1556 | (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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1559 | |
| 1560 | Value stack Label Instruction Argument |
| 1561 | [] SETUP_EXCEPT L1 |
| 1562 | [] <code for S> |
| 1563 | [] POP_BLOCK |
| 1564 | [] JUMP_FORWARD L0 |
| 1565 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1566 | [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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1574 | [] <code for S1> |
| 1575 | JUMP_FORWARD L0 |
| 1576 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1577 | [tb, val, exc, 0] L2: POP |
| 1578 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1579 | .............................etc....................... |
| 1580 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1581 | [tb, val, exc, 0] Ln+1: POP |
| 1582 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1583 | |
| 1584 | [] L0: <next statement> |
| 1585 | |
| 1586 | Of course, parts are not generated if Vi or Ei is not present. |
| 1587 | */ |
| 1588 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1589 | static void |
| 1590 | com_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 Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 1597 | /* '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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1602 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1603 | 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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1606 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1607 | } |
| 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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1611 | block_push(c, SETUP_EXCEPT); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1612 | } |
| 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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1619 | block_pop(c, SETUP_EXCEPT); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1620 | 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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1626 | if (except_anchor == 0) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1627 | err_setstr(SyntaxError, |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1628 | "default 'except:' must be last"); |
| 1629 | c->c_errors++; |
| 1630 | break; |
| 1631 | } |
| 1632 | except_anchor = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1633 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1634 | 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 Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1638 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1639 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1646 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1647 | com_node(c, CHILD(n, i+2)); |
| 1648 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1649 | if (except_anchor) { |
| 1650 | com_backpatch(c, except_anchor); |
| 1651 | com_addbyte(c, POP_TOP); |
| 1652 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1653 | } |
| 1654 | com_addbyte(c, END_FINALLY); |
| 1655 | com_backpatch(c, end_anchor); |
| 1656 | } |
| 1657 | if (finally_anchor) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1658 | node *ch; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1659 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1660 | block_pop(c, SETUP_FINALLY); |
| 1661 | block_push(c, END_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1662 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1663 | com_backpatch(c, finally_anchor); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1664 | ch = CHILD(n, NCH(n)-1); |
| 1665 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 1666 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1667 | com_addbyte(c, END_FINALLY); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1668 | block_pop(c, END_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | static void |
| 1673 | com_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 Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1692 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1693 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1694 | com_continue_stmt(c, n) |
| 1695 | struct compiling *c; |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1696 | node *n; /* Not used, but passed for consistency */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1697 | { |
| 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 Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1703 | err_setstr(SyntaxError, "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1704 | 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 | |
| 1710 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1711 | com_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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1717 | v = (object *)compile(n, c->c_filename); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1718 | 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 | |
| 1729 | static void |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1730 | com_oldbases(c, n) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1731 | struct compiling *c; |
| 1732 | node *n; |
| 1733 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1734 | int i; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1735 | REQ(n, baselist); |
| 1736 | /* |
| 1737 | baselist: atom arguments (',' atom arguments)* |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1738 | arguments: '(' ')' |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1739 | */ |
| 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 | |
| 1745 | static void |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1746 | com_newbases(c, n) |
| 1747 | struct compiling *c; |
| 1748 | node *n; |
| 1749 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1750 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1751 | 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 | |
| 1758 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1759 | com_classdef(c, n) |
| 1760 | struct compiling *c; |
| 1761 | node *n; |
| 1762 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1763 | object *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1764 | REQ(n, classdef); |
| 1765 | /* |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1766 | classdef: 'class' NAME |
| 1767 | ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1768 | baselist: atom arguments (',' atom arguments)* |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1769 | arguments: '(' ')' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1770 | */ |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1771 | /* 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1798 | v = (object *)compile(n, c->c_filename); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1799 | 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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | static void |
| 1813 | com_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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1831 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1832 | case flow_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1833 | com_node(c, CHILD(n, 0)); |
| 1834 | break; |
| 1835 | |
| 1836 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1837 | /* 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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1846 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1847 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1848 | 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 Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1859 | case del_stmt: /* 'del' exprlist */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1860 | com_assign(c, CHILD(n, 1), 0/*delete*/); |
| 1861 | break; |
| 1862 | case pass_stmt: |
| 1863 | break; |
| 1864 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1865 | if (c->c_loops == 0) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1866 | err_setstr(SyntaxError, "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1867 | c->c_errors++; |
| 1868 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1869 | com_addbyte(c, BREAK_LOOP); |
| 1870 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1871 | case continue_stmt: |
| 1872 | com_continue_stmt(c, n); |
| 1873 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1874 | 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 Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1883 | case global_stmt: |
| 1884 | com_global_stmt(c, n); |
| 1885 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1886 | 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 Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1905 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1906 | 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 Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1920 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1921 | break; |
| 1922 | case expr: |
| 1923 | com_expr(c, n); |
| 1924 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1925 | 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 Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1937 | 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 | |
| 1954 | static void com_fplist PROTO((struct compiling *, node *)); |
| 1955 | |
| 1956 | static void |
| 1957 | com_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 | |
| 1968 | static void |
| 1969 | com_fplist(c, n) |
| 1970 | struct compiling *c; |
| 1971 | node *n; |
| 1972 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 1973 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1974 | 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 | |
| 1985 | static void |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 1986 | com_arglist(c, n) |
| 1987 | struct compiling *c; |
| 1988 | node *n; |
| 1989 | { |
| 1990 | int i, nargs, op; |
| 1991 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1992 | /* varargslist: |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 1993 | (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 1994 | op = UNPACK_ARG; |
| 1995 | nargs = (NCH(n) + 1) / 2; |
| 1996 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1997 | int t = TYPE(CHILD(n, i)); |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 1998 | if (t == STAR) { |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 1999 | 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 | |
| 2011 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2012 | com_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 | |
| 2027 | static void |
| 2028 | compile_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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 2034 | c->c_name = STR(CHILD(n, 1)); |
| 2035 | com_addoparg(c, RESERVE_FAST, 0); /* Patched up later */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2036 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 2037 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2038 | if (TYPE(ch) == RPAR) |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2039 | com_addoparg(c, UNPACK_ARG, 0); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2040 | else |
| 2041 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2042 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2043 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2044 | c->c_infunction = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2045 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2046 | com_addbyte(c, RETURN_VALUE); |
| 2047 | } |
| 2048 | |
| 2049 | static void |
| 2050 | compile_node(c, n) |
| 2051 | struct compiling *c; |
| 2052 | node *n; |
| 2053 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2054 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2055 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2056 | switch (TYPE(n)) { |
| 2057 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2058 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2059 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
| 2060 | n = CHILD(n, 0); |
| 2061 | if (TYPE(n) != NEWLINE) |
| 2062 | com_node(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2063 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2064 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2065 | break; |
| 2066 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2067 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2068 | com_file_input(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2069 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2070 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2071 | break; |
| 2072 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2073 | case expr_input: /* Built-in function eval() */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2074 | com_node(c, CHILD(n, 0)); |
| 2075 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2076 | break; |
| 2077 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2078 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2079 | com_node(c, CHILD(n, 0)); |
| 2080 | com_addbyte(c, RETURN_VALUE); |
| 2081 | break; |
| 2082 | |
| 2083 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2084 | compile_funcdef(c, n); |
| 2085 | break; |
| 2086 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2087 | case classdef: /* A class definition */ |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2088 | /* classdef: 'class' NAME |
| 2089 | ['(' testlist ')' |'(' ')' ['=' baselist]] |
| 2090 | ':' suite */ |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2091 | com_node(c, CHILD(n, NCH(n)-1)); /* The suite */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2092 | com_addbyte(c, LOAD_LOCALS); |
| 2093 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2096 | 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 Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2103 | /* Optimization for local and global variables. |
| 2104 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 2105 | XXX Need to update this text for LOAD_FAST stuff... |
| 2106 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2107 | 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 | |
| 2136 | static void |
| 2137 | optimizer(co) |
| 2138 | codeobject *co; |
| 2139 | { |
Guido van Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 2140 | unsigned char *next_instr, *cur_instr; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2141 | object *locals; |
| 2142 | int opcode; |
| 2143 | int oparg; |
| 2144 | object *name; |
| 2145 | int star_used; |
Guido van Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 2146 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2147 | #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 Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 2158 | next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2159 | 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 Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 2175 | next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2176 | 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 Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2185 | if (dict2lookup(locals, name) != NULL) |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2186 | *cur_instr = LOAD_LOCAL; |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2187 | else { |
| 2188 | err_clear(); |
| 2189 | if (!star_used) |
| 2190 | *cur_instr = LOAD_GLOBAL; |
| 2191 | } |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | DECREF(locals); |
| 2196 | } |
| 2197 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2198 | codeobject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2199 | compile(n, filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2200 | node *n; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2201 | char *filename; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2202 | { |
| 2203 | struct compiling sc; |
| 2204 | codeobject *co; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2205 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2206 | return NULL; |
| 2207 | compile_node(&sc, n); |
| 2208 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 2209 | 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 Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 2219 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2220 | com_free(&sc); |
Guido van Rossum | fb8edfc | 1991-05-14 11:56:03 +0000 | [diff] [blame] | 2221 | if (co != NULL && filename[0] != '<') |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2222 | optimizer(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2223 | return co; |
| 2224 | } |