Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | 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' |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | #include "allobjects.h" |
| 33 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 34 | #include "node.h" |
| 35 | #include "token.h" |
| 36 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 37 | #include "compile.h" |
| 38 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | #include "structmember.h" |
| 40 | |
| 41 | #include <ctype.h> |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 42 | #include <errno.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 44 | #define OFF(x) offsetof(codeobject, x) |
| 45 | |
| 46 | static struct memberlist code_memberlist[] = { |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 47 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 48 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 49 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
| 50 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 51 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 52 | {NULL} /* Sentinel */ |
| 53 | }; |
| 54 | |
| 55 | static object * |
| 56 | code_getattr(co, name) |
| 57 | codeobject *co; |
| 58 | char *name; |
| 59 | { |
| 60 | return getmember((char *)co, code_memberlist, name); |
| 61 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 62 | |
| 63 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | code_dealloc(co) |
| 65 | codeobject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 66 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 67 | XDECREF(co->co_code); |
| 68 | XDECREF(co->co_consts); |
| 69 | XDECREF(co->co_names); |
| 70 | XDECREF(co->co_filename); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 71 | XDECREF(co->co_name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 72 | DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 75 | static object * |
| 76 | code_repr(co) |
| 77 | codeobject *co; |
| 78 | { |
| 79 | char buf[500]; |
| 80 | int lineno = -1; |
| 81 | char *p = GETSTRINGVALUE(co->co_code); |
| 82 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 83 | char *name = "???"; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 84 | if (*p == SET_LINENO) |
| 85 | lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8); |
| 86 | if (co->co_filename && is_stringobject(co->co_filename)) |
| 87 | filename = getstringvalue(co->co_filename); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 88 | if (co->co_name && is_stringobject(co->co_name)) |
| 89 | name = getstringvalue(co->co_name); |
| 90 | sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>", |
| 91 | name, (long)co, filename, lineno); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 92 | return newstringobject(buf); |
| 93 | } |
| 94 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 95 | static int |
| 96 | code_compare(co, cp) |
| 97 | codeobject *co, *cp; |
| 98 | { |
| 99 | int cmp; |
| 100 | cmp = cmpobject((object *)co->co_code, (object *)cp->co_code); |
| 101 | if (cmp) return cmp; |
| 102 | cmp = cmpobject(co->co_consts, cp->co_consts); |
| 103 | if (cmp) return cmp; |
| 104 | cmp = cmpobject(co->co_names, cp->co_names); |
| 105 | return cmp; |
| 106 | } |
| 107 | |
| 108 | static long |
| 109 | code_hash(co) |
| 110 | codeobject *co; |
| 111 | { |
| 112 | long h, h1, h2, h3; |
| 113 | h1 = hashobject((object *)co->co_code); |
| 114 | if (h1 == -1) return -1; |
| 115 | h2 = hashobject(co->co_consts); |
| 116 | if (h2 == -1) return -1; |
| 117 | h3 = hashobject(co->co_names); |
| 118 | if (h3 == -1) return -1; |
| 119 | h = h1 ^ h2 ^ h3; |
| 120 | if (h == -1) h = -2; |
| 121 | return h; |
| 122 | } |
| 123 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 124 | typeobject Codetype = { |
| 125 | OB_HEAD_INIT(&Typetype) |
| 126 | 0, |
| 127 | "code", |
| 128 | sizeof(codeobject), |
| 129 | 0, |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 130 | (destructor)code_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 131 | 0, /*tp_print*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 132 | (getattrfunc)code_getattr, /*tp_getattr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 133 | 0, /*tp_setattr*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 134 | (cmpfunc)code_compare, /*tp_compare*/ |
| 135 | (reprfunc)code_repr, /*tp_repr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 136 | 0, /*tp_as_number*/ |
| 137 | 0, /*tp_as_sequence*/ |
| 138 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 139 | (hashfunc)code_hash, /*tp_hash*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 142 | codeobject * |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 143 | newcodeobject(code, consts, names, filename, name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 144 | object *code; |
| 145 | object *consts; |
| 146 | object *names; |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 147 | object *filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 148 | object *name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 149 | { |
| 150 | codeobject *co; |
| 151 | int i; |
| 152 | /* Check argument types */ |
| 153 | if (code == NULL || !is_stringobject(code) || |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 154 | consts == NULL || |
| 155 | names == NULL || |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 156 | name == NULL || !(is_stringobject(name) || name == None)) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 157 | err_badcall(); |
| 158 | return NULL; |
| 159 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 160 | /* Allow two lists instead of two tuples */ |
| 161 | if (is_listobject(consts) && is_listobject(names)) { |
| 162 | consts = listtuple(consts); |
| 163 | if (consts == NULL) |
| 164 | return NULL; |
| 165 | names = listtuple(names); |
| 166 | if (names == NULL) { |
| 167 | DECREF(consts); |
| 168 | return NULL; |
| 169 | } |
| 170 | } |
| 171 | else if (!is_tupleobject(consts) && !is_tupleobject(names)) { |
| 172 | err_badcall(); |
| 173 | return NULL; |
| 174 | } |
| 175 | else { |
| 176 | INCREF(consts); |
| 177 | INCREF(names); |
| 178 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 179 | /* Make sure the list of names contains only strings */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 180 | for (i = gettuplesize(names); --i >= 0; ) { |
| 181 | object *v = gettupleitem(names, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 182 | if (v == NULL || !is_stringobject(v)) { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 183 | DECREF(consts); |
| 184 | DECREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 185 | err_badcall(); |
| 186 | return NULL; |
| 187 | } |
| 188 | } |
| 189 | co = NEWOBJ(codeobject, &Codetype); |
| 190 | if (co != NULL) { |
| 191 | INCREF(code); |
| 192 | co->co_code = (stringobject *)code; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 193 | co->co_consts = consts; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 194 | co->co_names = names; |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 195 | INCREF(filename); |
| 196 | co->co_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 197 | INCREF(name); |
| 198 | co->co_name = name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 199 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 200 | else { |
| 201 | DECREF(consts); |
| 202 | DECREF(names); |
| 203 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 204 | return co; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 209 | |
| 210 | #define MAXBLOCKS 20 /* Max static block nesting within a function */ |
| 211 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 212 | struct compiling { |
| 213 | object *c_code; /* string */ |
| 214 | object *c_consts; /* list of objects */ |
| 215 | object *c_names; /* list of strings (names) */ |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 216 | object *c_globals; /* dictionary */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 217 | int c_nexti; /* index into c_code */ |
| 218 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 219 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 220 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 221 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 222 | int c_begin; /* begin of current loop, for 'continue' */ |
| 223 | int c_block[MAXBLOCKS]; /* stack of block types */ |
| 224 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 225 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 226 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 229 | |
| 230 | /* Interface to the block stack */ |
| 231 | |
| 232 | static void |
| 233 | block_push(c, type) |
| 234 | struct compiling *c; |
| 235 | int type; |
| 236 | { |
| 237 | if (c->c_nblocks >= MAXBLOCKS) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 238 | err_setstr(SystemError, "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 239 | c->c_errors++; |
| 240 | } |
| 241 | else { |
| 242 | c->c_block[c->c_nblocks++] = type; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | block_pop(c, type) |
| 248 | struct compiling *c; |
| 249 | int type; |
| 250 | { |
| 251 | if (c->c_nblocks > 0) |
| 252 | c->c_nblocks--; |
| 253 | if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) { |
| 254 | err_setstr(SystemError, "bad block pop"); |
| 255 | c->c_errors++; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 260 | /* Prototypes */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 262 | static int com_init PROTO((struct compiling *, char *)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 263 | static void com_free PROTO((struct compiling *)); |
| 264 | static void com_done PROTO((struct compiling *)); |
| 265 | static void com_node PROTO((struct compiling *, struct _node *)); |
| 266 | static void com_addbyte PROTO((struct compiling *, int)); |
| 267 | static void com_addint PROTO((struct compiling *, int)); |
| 268 | static void com_addoparg PROTO((struct compiling *, int, int)); |
| 269 | static void com_addfwref PROTO((struct compiling *, int, int *)); |
| 270 | static void com_backpatch PROTO((struct compiling *, int)); |
| 271 | static int com_add PROTO((struct compiling *, object *, object *)); |
| 272 | static int com_addconst PROTO((struct compiling *, object *)); |
| 273 | static int com_addname PROTO((struct compiling *, object *)); |
| 274 | static void com_addopname PROTO((struct compiling *, int, node *)); |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 275 | static void com_list PROTO((struct compiling *, node *, int)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 276 | static int com_argdefs PROTO((struct compiling *, node *, int *)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 277 | |
| 278 | static int |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 279 | com_init(c, filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 280 | struct compiling *c; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 281 | char *filename; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 282 | { |
Guido van Rossum | 62d4624 | 1991-04-03 19:00:23 +0000 | [diff] [blame] | 283 | if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 284 | goto fail_3; |
| 285 | if ((c->c_consts = newlistobject(0)) == NULL) |
| 286 | goto fail_2; |
| 287 | if ((c->c_names = newlistobject(0)) == NULL) |
| 288 | goto fail_1; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 289 | if ((c->c_globals = newdictobject()) == NULL) |
| 290 | goto fail_0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 291 | c->c_nexti = 0; |
| 292 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 293 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 294 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 295 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 296 | c->c_begin = 0; |
| 297 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 298 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 299 | c->c_name = "?"; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 300 | return 1; |
| 301 | |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 302 | fail_0: |
| 303 | DECREF(c->c_names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 304 | fail_1: |
| 305 | DECREF(c->c_consts); |
| 306 | fail_2: |
| 307 | DECREF(c->c_code); |
| 308 | fail_3: |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static void |
| 313 | com_free(c) |
| 314 | struct compiling *c; |
| 315 | { |
| 316 | XDECREF(c->c_code); |
| 317 | XDECREF(c->c_consts); |
| 318 | XDECREF(c->c_names); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 319 | XDECREF(c->c_globals); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void |
| 323 | com_done(c) |
| 324 | struct compiling *c; |
| 325 | { |
| 326 | if (c->c_code != NULL) |
| 327 | resizestring(&c->c_code, c->c_nexti); |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | com_addbyte(c, byte) |
| 332 | struct compiling *c; |
| 333 | int byte; |
| 334 | { |
| 335 | int len; |
| 336 | if (byte < 0 || byte > 255) { |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 337 | /* |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 338 | fprintf(stderr, "XXX compiling bad byte: %d\n", byte); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 339 | fatal("com_addbyte: byte out of range"); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 340 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 341 | err_setstr(SystemError, "com_addbyte: byte out of range"); |
| 342 | c->c_errors++; |
| 343 | } |
| 344 | if (c->c_code == NULL) |
| 345 | return; |
| 346 | len = getstringsize(c->c_code); |
| 347 | if (c->c_nexti >= len) { |
| 348 | if (resizestring(&c->c_code, len+1000) != 0) { |
| 349 | c->c_errors++; |
| 350 | return; |
| 351 | } |
| 352 | } |
| 353 | getstringvalue(c->c_code)[c->c_nexti++] = byte; |
| 354 | } |
| 355 | |
| 356 | static void |
| 357 | com_addint(c, x) |
| 358 | struct compiling *c; |
| 359 | int x; |
| 360 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 361 | com_addbyte(c, x & 0xff); |
| 362 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void |
| 366 | com_addoparg(c, op, arg) |
| 367 | struct compiling *c; |
| 368 | int op; |
| 369 | int arg; |
| 370 | { |
| 371 | com_addbyte(c, op); |
| 372 | com_addint(c, arg); |
| 373 | } |
| 374 | |
| 375 | static void |
| 376 | com_addfwref(c, op, p_anchor) |
| 377 | struct compiling *c; |
| 378 | int op; |
| 379 | int *p_anchor; |
| 380 | { |
| 381 | /* Compile a forward reference for backpatching */ |
| 382 | int here; |
| 383 | int anchor; |
| 384 | com_addbyte(c, op); |
| 385 | here = c->c_nexti; |
| 386 | anchor = *p_anchor; |
| 387 | *p_anchor = here; |
| 388 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 389 | } |
| 390 | |
| 391 | static void |
| 392 | com_backpatch(c, anchor) |
| 393 | struct compiling *c; |
| 394 | int anchor; /* Must be nonzero */ |
| 395 | { |
| 396 | unsigned char *code = (unsigned char *) getstringvalue(c->c_code); |
| 397 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 398 | int dist; |
| 399 | int prev; |
| 400 | for (;;) { |
| 401 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 402 | prev = code[anchor] + (code[anchor+1] << 8); |
| 403 | dist = target - (anchor+2); |
| 404 | code[anchor] = dist & 0xff; |
| 405 | code[anchor+1] = dist >> 8; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 406 | if (!prev) |
| 407 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 408 | anchor -= prev; |
| 409 | } |
| 410 | } |
| 411 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 412 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 413 | |
| 414 | static int |
| 415 | com_add(c, list, v) |
| 416 | struct compiling *c; |
| 417 | object *list; |
| 418 | object *v; |
| 419 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 420 | int n = getlistsize(list); |
| 421 | int i; |
| 422 | for (i = n; --i >= 0; ) { |
| 423 | object *w = getlistitem(list, i); |
Guido van Rossum | efc0bd0 | 1991-07-01 18:44:20 +0000 | [diff] [blame] | 424 | if (v->ob_type == w->ob_type && cmpobject(v, w) == 0) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 425 | return i; |
| 426 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 427 | if (addlistitem(list, v) != 0) |
| 428 | c->c_errors++; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 429 | return n; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int |
| 433 | com_addconst(c, v) |
| 434 | struct compiling *c; |
| 435 | object *v; |
| 436 | { |
| 437 | return com_add(c, c->c_consts, v); |
| 438 | } |
| 439 | |
| 440 | static int |
| 441 | com_addname(c, v) |
| 442 | struct compiling *c; |
| 443 | object *v; |
| 444 | { |
| 445 | return com_add(c, c->c_names, v); |
| 446 | } |
| 447 | |
| 448 | static void |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 449 | com_addopnamestr(c, op, name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 450 | struct compiling *c; |
| 451 | int op; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 452 | char *name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 453 | { |
| 454 | object *v; |
| 455 | int i; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 456 | if (name == NULL || (v = newstringobject(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 457 | c->c_errors++; |
| 458 | i = 255; |
| 459 | } |
| 460 | else { |
| 461 | i = com_addname(c, v); |
| 462 | DECREF(v); |
| 463 | } |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 464 | /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */ |
| 465 | switch (op) { |
| 466 | case LOAD_NAME: |
| 467 | case STORE_NAME: |
| 468 | case DELETE_NAME: |
| 469 | if (dictlookup(c->c_globals, name) != NULL) { |
| 470 | switch (op) { |
| 471 | case LOAD_NAME: op = LOAD_GLOBAL; break; |
| 472 | case STORE_NAME: op = STORE_GLOBAL; break; |
| 473 | case DELETE_NAME: op = DELETE_GLOBAL; break; |
| 474 | } |
| 475 | } |
| 476 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 477 | com_addoparg(c, op, i); |
| 478 | } |
| 479 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 480 | static void |
| 481 | com_addopname(c, op, n) |
| 482 | struct compiling *c; |
| 483 | int op; |
| 484 | node *n; |
| 485 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 486 | char *name; |
| 487 | char buffer[1000]; |
| 488 | /* XXX it is possible to write this code without the 1000 |
| 489 | chars on the total length of dotted names, I just can't be |
| 490 | bothered right now */ |
| 491 | if (TYPE(n) == STAR) |
| 492 | name = "*"; |
| 493 | else if (TYPE(n) == dotted_name) { |
| 494 | char *p = buffer; |
| 495 | int i; |
| 496 | name = buffer; |
| 497 | for (i = 0; i < NCH(n); i += 2) { |
| 498 | char *s = STR(CHILD(n, i)); |
| 499 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
| 500 | err_setstr(MemoryError, |
| 501 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 502 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | if (p != buffer) |
| 506 | *p++ = '.'; |
| 507 | strcpy(p, s); |
| 508 | p = strchr(p, '\0'); |
| 509 | } |
| 510 | } |
| 511 | else { |
| 512 | REQ(n, NAME); |
| 513 | name = STR(n); |
| 514 | } |
| 515 | com_addopnamestr(c, op, name); |
| 516 | } |
| 517 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 518 | static object * |
| 519 | parsenumber(s) |
| 520 | char *s; |
| 521 | { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 522 | extern long mystrtol PROTO((const char *, char **, int)); |
| 523 | extern unsigned long mystrtoul PROTO((const char *, char **, int)); |
| 524 | extern double atof PROTO((const char *)); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 525 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 526 | long x; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 527 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 528 | end = s + strlen(s) - 1; |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 529 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 530 | return long_scan(s, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 531 | if (s[0] == '0') |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 532 | x = (long) mystrtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 533 | else |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 534 | x = mystrtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 535 | if (*end == '\0') { |
| 536 | if (errno != 0) { |
Guido van Rossum | efb087b | 1991-12-16 15:41:41 +0000 | [diff] [blame] | 537 | err_setstr(OverflowError, |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 538 | "integer literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 539 | return NULL; |
| 540 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 541 | return newintobject(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 542 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 543 | /* XXX Huge floats may silently fail */ |
| 544 | return newfloatobject(atof(s)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static object * |
| 548 | parsestr(s) |
| 549 | char *s; |
| 550 | { |
| 551 | object *v; |
| 552 | int len; |
| 553 | char *buf; |
| 554 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 555 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 556 | int c; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 557 | int quote = *s; |
| 558 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 559 | err_badcall(); |
| 560 | return NULL; |
| 561 | } |
| 562 | s++; |
| 563 | len = strlen(s); |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 564 | if (s[--len] != quote) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 565 | err_badcall(); |
| 566 | return NULL; |
| 567 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 568 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 569 | s += 2; |
| 570 | len -= 2; |
| 571 | if (s[--len] != quote || s[--len] != quote) { |
| 572 | err_badcall(); |
| 573 | return NULL; |
| 574 | } |
| 575 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 576 | if (strchr(s, '\\') == NULL) |
| 577 | return newsizedstringobject(s, len); |
| 578 | v = newsizedstringobject((char *)NULL, len); |
| 579 | p = buf = getstringvalue(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 580 | end = s + len; |
| 581 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 582 | if (*s != '\\') { |
| 583 | *p++ = *s++; |
| 584 | continue; |
| 585 | } |
| 586 | s++; |
| 587 | switch (*s++) { |
| 588 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 589 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 590 | case '\\': *p++ = '\\'; break; |
| 591 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 592 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 593 | case 'b': *p++ = '\b'; break; |
| 594 | case 'f': *p++ = '\014'; break; /* FF */ |
| 595 | case 't': *p++ = '\t'; break; |
| 596 | case 'n': *p++ = '\n'; break; |
| 597 | case 'r': *p++ = '\r'; break; |
| 598 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 599 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 600 | case '0': case '1': case '2': case '3': |
| 601 | case '4': case '5': case '6': case '7': |
| 602 | c = s[-1] - '0'; |
| 603 | if ('0' <= *s && *s <= '7') { |
| 604 | c = (c<<3) + *s++ - '0'; |
| 605 | if ('0' <= *s && *s <= '7') |
| 606 | c = (c<<3) + *s++ - '0'; |
| 607 | } |
| 608 | *p++ = c; |
| 609 | break; |
| 610 | case 'x': |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 611 | if (isxdigit(Py_CHARMASK(*s))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 612 | sscanf(s, "%x", &c); |
| 613 | *p++ = c; |
| 614 | do { |
| 615 | s++; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 616 | } while (isxdigit(Py_CHARMASK(*s))); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 617 | break; |
| 618 | } |
| 619 | /* FALLTHROUGH */ |
| 620 | default: *p++ = '\\'; *p++ = s[-1]; break; |
| 621 | } |
| 622 | } |
| 623 | resizestring(&v, (int)(p - buf)); |
| 624 | return v; |
| 625 | } |
| 626 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 627 | static object * |
| 628 | parsestrplus(n) |
| 629 | node *n; |
| 630 | { |
| 631 | object *v; |
| 632 | int i; |
| 633 | REQ(CHILD(n, 0), STRING); |
| 634 | if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) { |
| 635 | /* String literal concatenation */ |
| 636 | for (i = 1; i < NCH(n) && v != NULL; i++) { |
| 637 | joinstring_decref(&v, parsestr(STR(CHILD(n, i)))); |
| 638 | } |
| 639 | } |
| 640 | return v; |
| 641 | } |
| 642 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 643 | static void |
| 644 | com_list_constructor(c, n) |
| 645 | struct compiling *c; |
| 646 | node *n; |
| 647 | { |
| 648 | int len; |
| 649 | int i; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 650 | if (TYPE(n) != testlist) |
| 651 | REQ(n, exprlist); |
| 652 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
| 653 | len = (NCH(n) + 1) / 2; |
| 654 | for (i = 0; i < NCH(n); i += 2) |
| 655 | com_node(c, CHILD(n, i)); |
| 656 | com_addoparg(c, BUILD_LIST, len); |
| 657 | } |
| 658 | |
| 659 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 660 | com_dictmaker(c, n) |
| 661 | struct compiling *c; |
| 662 | node *n; |
| 663 | { |
| 664 | int i; |
| 665 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 666 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 667 | /* We must arrange things just right for STORE_SUBSCR. |
| 668 | It wants the stack to look like (value) (dict) (key) */ |
| 669 | com_addbyte(c, DUP_TOP); |
| 670 | com_node(c, CHILD(n, i+2)); /* value */ |
| 671 | com_addbyte(c, ROT_TWO); |
| 672 | com_node(c, CHILD(n, i)); /* key */ |
| 673 | com_addbyte(c, STORE_SUBSCR); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 678 | com_atom(c, n) |
| 679 | struct compiling *c; |
| 680 | node *n; |
| 681 | { |
| 682 | node *ch; |
| 683 | object *v; |
| 684 | int i; |
| 685 | REQ(n, atom); |
| 686 | ch = CHILD(n, 0); |
| 687 | switch (TYPE(ch)) { |
| 688 | case LPAR: |
| 689 | if (TYPE(CHILD(n, 1)) == RPAR) |
| 690 | com_addoparg(c, BUILD_TUPLE, 0); |
| 691 | else |
| 692 | com_node(c, CHILD(n, 1)); |
| 693 | break; |
| 694 | case LSQB: |
| 695 | if (TYPE(CHILD(n, 1)) == RSQB) |
| 696 | com_addoparg(c, BUILD_LIST, 0); |
| 697 | else |
| 698 | com_list_constructor(c, CHILD(n, 1)); |
| 699 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 700 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 701 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 702 | if (TYPE(CHILD(n, 1)) != RBRACE) |
| 703 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 704 | break; |
| 705 | case BACKQUOTE: |
| 706 | com_node(c, CHILD(n, 1)); |
| 707 | com_addbyte(c, UNARY_CONVERT); |
| 708 | break; |
| 709 | case NUMBER: |
| 710 | if ((v = parsenumber(STR(ch))) == NULL) { |
| 711 | c->c_errors++; |
| 712 | i = 255; |
| 713 | } |
| 714 | else { |
| 715 | i = com_addconst(c, v); |
| 716 | DECREF(v); |
| 717 | } |
| 718 | com_addoparg(c, LOAD_CONST, i); |
| 719 | break; |
| 720 | case STRING: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 721 | v = parsestrplus(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 722 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 723 | c->c_errors++; |
| 724 | i = 255; |
| 725 | } |
| 726 | else { |
| 727 | i = com_addconst(c, v); |
| 728 | DECREF(v); |
| 729 | } |
| 730 | com_addoparg(c, LOAD_CONST, i); |
| 731 | break; |
| 732 | case NAME: |
| 733 | com_addopname(c, LOAD_NAME, ch); |
| 734 | break; |
| 735 | default: |
| 736 | fprintf(stderr, "node type %d\n", TYPE(ch)); |
| 737 | err_setstr(SystemError, "com_atom: unexpected node type"); |
| 738 | c->c_errors++; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | static void |
| 743 | com_slice(c, n, op) |
| 744 | struct compiling *c; |
| 745 | node *n; |
| 746 | int op; |
| 747 | { |
| 748 | if (NCH(n) == 1) { |
| 749 | com_addbyte(c, op); |
| 750 | } |
| 751 | else if (NCH(n) == 2) { |
| 752 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 753 | com_node(c, CHILD(n, 0)); |
| 754 | com_addbyte(c, op+1); |
| 755 | } |
| 756 | else { |
| 757 | com_node(c, CHILD(n, 1)); |
| 758 | com_addbyte(c, op+2); |
| 759 | } |
| 760 | } |
| 761 | else { |
| 762 | com_node(c, CHILD(n, 0)); |
| 763 | com_node(c, CHILD(n, 2)); |
| 764 | com_addbyte(c, op+3); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | static void |
| 769 | com_apply_subscript(c, n) |
| 770 | struct compiling *c; |
| 771 | node *n; |
| 772 | { |
| 773 | REQ(n, subscript); |
| 774 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) { |
| 775 | /* It's a single subscript */ |
| 776 | com_node(c, CHILD(n, 0)); |
| 777 | com_addbyte(c, BINARY_SUBSCR); |
| 778 | } |
| 779 | else { |
| 780 | /* It's a slice: [expr] ':' [expr] */ |
| 781 | com_slice(c, n, SLICE); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | static void |
| 786 | com_call_function(c, n) |
| 787 | struct compiling *c; |
| 788 | node *n; /* EITHER testlist OR ')' */ |
| 789 | { |
| 790 | if (TYPE(n) == RPAR) { |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 791 | com_addoparg(c, BUILD_TUPLE, 0); |
| 792 | com_addbyte(c, BINARY_CALL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 793 | } |
| 794 | else { |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 795 | REQ(n, testlist); |
| 796 | com_list(c, n, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 797 | com_addbyte(c, BINARY_CALL); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | static void |
| 802 | com_select_member(c, n) |
| 803 | struct compiling *c; |
| 804 | node *n; |
| 805 | { |
| 806 | com_addopname(c, LOAD_ATTR, n); |
| 807 | } |
| 808 | |
| 809 | static void |
| 810 | com_apply_trailer(c, n) |
| 811 | struct compiling *c; |
| 812 | node *n; |
| 813 | { |
| 814 | REQ(n, trailer); |
| 815 | switch (TYPE(CHILD(n, 0))) { |
| 816 | case LPAR: |
| 817 | com_call_function(c, CHILD(n, 1)); |
| 818 | break; |
| 819 | case DOT: |
| 820 | com_select_member(c, CHILD(n, 1)); |
| 821 | break; |
| 822 | case LSQB: |
| 823 | com_apply_subscript(c, CHILD(n, 1)); |
| 824 | break; |
| 825 | default: |
| 826 | err_setstr(SystemError, |
| 827 | "com_apply_trailer: unknown trailer type"); |
| 828 | c->c_errors++; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | static void |
| 833 | com_factor(c, n) |
| 834 | struct compiling *c; |
| 835 | node *n; |
| 836 | { |
| 837 | int i; |
| 838 | REQ(n, factor); |
| 839 | if (TYPE(CHILD(n, 0)) == PLUS) { |
| 840 | com_factor(c, CHILD(n, 1)); |
| 841 | com_addbyte(c, UNARY_POSITIVE); |
| 842 | } |
| 843 | else if (TYPE(CHILD(n, 0)) == MINUS) { |
| 844 | com_factor(c, CHILD(n, 1)); |
| 845 | com_addbyte(c, UNARY_NEGATIVE); |
| 846 | } |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 847 | else if (TYPE(CHILD(n, 0)) == TILDE) { |
| 848 | com_factor(c, CHILD(n, 1)); |
| 849 | com_addbyte(c, UNARY_INVERT); |
| 850 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 851 | else { |
| 852 | com_atom(c, CHILD(n, 0)); |
| 853 | for (i = 1; i < NCH(n); i++) |
| 854 | com_apply_trailer(c, CHILD(n, i)); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | static void |
| 859 | com_term(c, n) |
| 860 | struct compiling *c; |
| 861 | node *n; |
| 862 | { |
| 863 | int i; |
| 864 | int op; |
| 865 | REQ(n, term); |
| 866 | com_factor(c, CHILD(n, 0)); |
| 867 | for (i = 2; i < NCH(n); i += 2) { |
| 868 | com_factor(c, CHILD(n, i)); |
| 869 | switch (TYPE(CHILD(n, i-1))) { |
| 870 | case STAR: |
| 871 | op = BINARY_MULTIPLY; |
| 872 | break; |
| 873 | case SLASH: |
| 874 | op = BINARY_DIVIDE; |
| 875 | break; |
| 876 | case PERCENT: |
| 877 | op = BINARY_MODULO; |
| 878 | break; |
| 879 | default: |
| 880 | err_setstr(SystemError, |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 881 | "com_term: operator not *, / or %"); |
| 882 | c->c_errors++; |
| 883 | op = 255; |
| 884 | } |
| 885 | com_addbyte(c, op); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | static void |
| 890 | com_arith_expr(c, n) |
| 891 | struct compiling *c; |
| 892 | node *n; |
| 893 | { |
| 894 | int i; |
| 895 | int op; |
| 896 | REQ(n, arith_expr); |
| 897 | com_term(c, CHILD(n, 0)); |
| 898 | for (i = 2; i < NCH(n); i += 2) { |
| 899 | com_term(c, CHILD(n, i)); |
| 900 | switch (TYPE(CHILD(n, i-1))) { |
| 901 | case PLUS: |
| 902 | op = BINARY_ADD; |
| 903 | break; |
| 904 | case MINUS: |
| 905 | op = BINARY_SUBTRACT; |
| 906 | break; |
| 907 | default: |
| 908 | err_setstr(SystemError, |
| 909 | "com_arith_expr: operator not + or -"); |
| 910 | c->c_errors++; |
| 911 | op = 255; |
| 912 | } |
| 913 | com_addbyte(c, op); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | static void |
| 918 | com_shift_expr(c, n) |
| 919 | struct compiling *c; |
| 920 | node *n; |
| 921 | { |
| 922 | int i; |
| 923 | int op; |
| 924 | REQ(n, shift_expr); |
| 925 | com_arith_expr(c, CHILD(n, 0)); |
| 926 | for (i = 2; i < NCH(n); i += 2) { |
| 927 | com_arith_expr(c, CHILD(n, i)); |
| 928 | switch (TYPE(CHILD(n, i-1))) { |
| 929 | case LEFTSHIFT: |
| 930 | op = BINARY_LSHIFT; |
| 931 | break; |
| 932 | case RIGHTSHIFT: |
| 933 | op = BINARY_RSHIFT; |
| 934 | break; |
| 935 | default: |
| 936 | err_setstr(SystemError, |
| 937 | "com_shift_expr: operator not << or >>"); |
| 938 | c->c_errors++; |
| 939 | op = 255; |
| 940 | } |
| 941 | com_addbyte(c, op); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | static void |
| 946 | com_and_expr(c, n) |
| 947 | struct compiling *c; |
| 948 | node *n; |
| 949 | { |
| 950 | int i; |
| 951 | int op; |
| 952 | REQ(n, and_expr); |
| 953 | com_shift_expr(c, CHILD(n, 0)); |
| 954 | for (i = 2; i < NCH(n); i += 2) { |
| 955 | com_shift_expr(c, CHILD(n, i)); |
| 956 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 957 | op = BINARY_AND; |
| 958 | } |
| 959 | else { |
| 960 | err_setstr(SystemError, |
| 961 | "com_and_expr: operator not &"); |
| 962 | c->c_errors++; |
| 963 | op = 255; |
| 964 | } |
| 965 | com_addbyte(c, op); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | static void |
| 970 | com_xor_expr(c, n) |
| 971 | struct compiling *c; |
| 972 | node *n; |
| 973 | { |
| 974 | int i; |
| 975 | int op; |
| 976 | REQ(n, xor_expr); |
| 977 | com_and_expr(c, CHILD(n, 0)); |
| 978 | for (i = 2; i < NCH(n); i += 2) { |
| 979 | com_and_expr(c, CHILD(n, i)); |
| 980 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 981 | op = BINARY_XOR; |
| 982 | } |
| 983 | else { |
| 984 | err_setstr(SystemError, |
| 985 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 986 | c->c_errors++; |
| 987 | op = 255; |
| 988 | } |
| 989 | com_addbyte(c, op); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | com_expr(c, n) |
| 995 | struct compiling *c; |
| 996 | node *n; |
| 997 | { |
| 998 | int i; |
| 999 | int op; |
| 1000 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1001 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1002 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1003 | com_xor_expr(c, CHILD(n, i)); |
| 1004 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 1005 | op = BINARY_OR; |
| 1006 | } |
| 1007 | else { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1008 | err_setstr(SystemError, |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1009 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1010 | c->c_errors++; |
| 1011 | op = 255; |
| 1012 | } |
| 1013 | com_addbyte(c, op); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | static enum cmp_op |
| 1018 | cmp_type(n) |
| 1019 | node *n; |
| 1020 | { |
| 1021 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1022 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1023 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 1024 | if (NCH(n) == 1) { |
| 1025 | n = CHILD(n, 0); |
| 1026 | switch (TYPE(n)) { |
| 1027 | case LESS: return LT; |
| 1028 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1029 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1030 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1031 | case LESSEQUAL: return LE; |
| 1032 | case GREATEREQUAL: return GE; |
| 1033 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1034 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 1035 | if (strcmp(STR(n), "is") == 0) return IS; |
| 1036 | } |
| 1037 | } |
| 1038 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1039 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1040 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1041 | return NOT_IN; |
| 1042 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1043 | return IS_NOT; |
| 1044 | } |
| 1045 | } |
| 1046 | return BAD; |
| 1047 | } |
| 1048 | |
| 1049 | static void |
| 1050 | com_comparison(c, n) |
| 1051 | struct compiling *c; |
| 1052 | node *n; |
| 1053 | { |
| 1054 | int i; |
| 1055 | enum cmp_op op; |
| 1056 | int anchor; |
| 1057 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 1058 | com_expr(c, CHILD(n, 0)); |
| 1059 | if (NCH(n) == 1) |
| 1060 | return; |
| 1061 | |
| 1062 | /**************************************************************** |
| 1063 | The following code is generated for all but the last |
| 1064 | comparison in a chain: |
| 1065 | |
| 1066 | label: on stack: opcode: jump to: |
| 1067 | |
| 1068 | a <code to load b> |
| 1069 | a, b DUP_TOP |
| 1070 | a, b, b ROT_THREE |
| 1071 | b, a, b COMPARE_OP |
| 1072 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 1073 | b, 1 POP_TOP |
| 1074 | b |
| 1075 | |
| 1076 | We are now ready to repeat this sequence for the next |
| 1077 | comparison in the chain. |
| 1078 | |
| 1079 | For the last we generate: |
| 1080 | |
| 1081 | b <code to load c> |
| 1082 | b, c COMPARE_OP |
| 1083 | 0-or-1 |
| 1084 | |
| 1085 | If there were any jumps to L1 (i.e., there was more than one |
| 1086 | comparison), we generate: |
| 1087 | |
| 1088 | 0-or-1 JUMP_FORWARD L2 |
| 1089 | L1: b, 0 ROT_TWO |
| 1090 | 0, b POP_TOP |
| 1091 | 0 |
| 1092 | L2: |
| 1093 | ****************************************************************/ |
| 1094 | |
| 1095 | anchor = 0; |
| 1096 | |
| 1097 | for (i = 2; i < NCH(n); i += 2) { |
| 1098 | com_expr(c, CHILD(n, i)); |
| 1099 | if (i+2 < NCH(n)) { |
| 1100 | com_addbyte(c, DUP_TOP); |
| 1101 | com_addbyte(c, ROT_THREE); |
| 1102 | } |
| 1103 | op = cmp_type(CHILD(n, i-1)); |
| 1104 | if (op == BAD) { |
| 1105 | err_setstr(SystemError, |
| 1106 | "com_comparison: unknown comparison op"); |
| 1107 | c->c_errors++; |
| 1108 | } |
| 1109 | com_addoparg(c, COMPARE_OP, op); |
| 1110 | if (i+2 < NCH(n)) { |
| 1111 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1112 | com_addbyte(c, POP_TOP); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | if (anchor) { |
| 1117 | int anchor2 = 0; |
| 1118 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 1119 | com_backpatch(c, anchor); |
| 1120 | com_addbyte(c, ROT_TWO); |
| 1121 | com_addbyte(c, POP_TOP); |
| 1122 | com_backpatch(c, anchor2); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | static void |
| 1127 | com_not_test(c, n) |
| 1128 | struct compiling *c; |
| 1129 | node *n; |
| 1130 | { |
| 1131 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 1132 | if (NCH(n) == 1) { |
| 1133 | com_comparison(c, CHILD(n, 0)); |
| 1134 | } |
| 1135 | else { |
| 1136 | com_not_test(c, CHILD(n, 1)); |
| 1137 | com_addbyte(c, UNARY_NOT); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | static void |
| 1142 | com_and_test(c, n) |
| 1143 | struct compiling *c; |
| 1144 | node *n; |
| 1145 | { |
| 1146 | int i; |
| 1147 | int anchor; |
| 1148 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 1149 | anchor = 0; |
| 1150 | i = 0; |
| 1151 | for (;;) { |
| 1152 | com_not_test(c, CHILD(n, i)); |
| 1153 | if ((i += 2) >= NCH(n)) |
| 1154 | break; |
| 1155 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1156 | com_addbyte(c, POP_TOP); |
| 1157 | } |
| 1158 | if (anchor) |
| 1159 | com_backpatch(c, anchor); |
| 1160 | } |
| 1161 | |
| 1162 | static void |
| 1163 | com_test(c, n) |
| 1164 | struct compiling *c; |
| 1165 | node *n; |
| 1166 | { |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1167 | REQ(n, test); /* and_test ('and' and_test)* | lambdef */ |
| 1168 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
| 1169 | object *v; |
| 1170 | int i; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1171 | int argcount; |
| 1172 | int ndefs = com_argdefs(c, CHILD(n, 0), &argcount); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1173 | v = (object *) compile(CHILD(n, 0), c->c_filename); |
| 1174 | if (v == NULL) { |
| 1175 | c->c_errors++; |
| 1176 | i = 255; |
| 1177 | } |
| 1178 | else { |
| 1179 | i = com_addconst(c, v); |
| 1180 | DECREF(v); |
| 1181 | } |
| 1182 | com_addoparg(c, LOAD_CONST, i); |
| 1183 | com_addbyte(c, BUILD_FUNCTION); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1184 | if (ndefs > 0) |
| 1185 | com_addoparg(c, SET_FUNC_ARGS, argcount); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1186 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1187 | else { |
| 1188 | int anchor = 0; |
| 1189 | int i = 0; |
| 1190 | for (;;) { |
| 1191 | com_and_test(c, CHILD(n, i)); |
| 1192 | if ((i += 2) >= NCH(n)) |
| 1193 | break; |
| 1194 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 1195 | com_addbyte(c, POP_TOP); |
| 1196 | } |
| 1197 | if (anchor) |
| 1198 | com_backpatch(c, anchor); |
| 1199 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static void |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1203 | com_list(c, n, toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1204 | struct compiling *c; |
| 1205 | node *n; |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1206 | int toplevel; /* If nonzero, *always* build a tuple */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1207 | { |
| 1208 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1209 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1210 | com_node(c, CHILD(n, 0)); |
| 1211 | } |
| 1212 | else { |
| 1213 | int i; |
| 1214 | int len; |
| 1215 | len = (NCH(n) + 1) / 2; |
| 1216 | for (i = 0; i < NCH(n); i += 2) |
| 1217 | com_node(c, CHILD(n, i)); |
| 1218 | com_addoparg(c, BUILD_TUPLE, len); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | /* Begin of assignment compilation */ |
| 1224 | |
| 1225 | static void com_assign_name PROTO((struct compiling *, node *, int)); |
| 1226 | static void com_assign PROTO((struct compiling *, node *, int)); |
| 1227 | |
| 1228 | static void |
| 1229 | com_assign_attr(c, n, assigning) |
| 1230 | struct compiling *c; |
| 1231 | node *n; |
| 1232 | int assigning; |
| 1233 | { |
| 1234 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
| 1235 | } |
| 1236 | |
| 1237 | static void |
| 1238 | com_assign_slice(c, n, assigning) |
| 1239 | struct compiling *c; |
| 1240 | node *n; |
| 1241 | int assigning; |
| 1242 | { |
| 1243 | com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE); |
| 1244 | } |
| 1245 | |
| 1246 | static void |
| 1247 | com_assign_subscript(c, n, assigning) |
| 1248 | struct compiling *c; |
| 1249 | node *n; |
| 1250 | int assigning; |
| 1251 | { |
| 1252 | com_node(c, n); |
| 1253 | com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR); |
| 1254 | } |
| 1255 | |
| 1256 | static void |
| 1257 | com_assign_trailer(c, n, assigning) |
| 1258 | struct compiling *c; |
| 1259 | node *n; |
| 1260 | int assigning; |
| 1261 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1262 | REQ(n, trailer); |
| 1263 | switch (TYPE(CHILD(n, 0))) { |
| 1264 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1265 | err_setstr(SyntaxError, "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1266 | c->c_errors++; |
| 1267 | break; |
| 1268 | case DOT: /* '.' NAME */ |
| 1269 | com_assign_attr(c, CHILD(n, 1), assigning); |
| 1270 | break; |
| 1271 | case LSQB: /* '[' subscript ']' */ |
| 1272 | n = CHILD(n, 1); |
| 1273 | REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */ |
| 1274 | if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON) |
| 1275 | com_assign_slice(c, n, assigning); |
| 1276 | else |
| 1277 | com_assign_subscript(c, CHILD(n, 0), assigning); |
| 1278 | break; |
| 1279 | default: |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1280 | err_setstr(SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1281 | c->c_errors++; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | static void |
| 1286 | com_assign_tuple(c, n, assigning) |
| 1287 | struct compiling *c; |
| 1288 | node *n; |
| 1289 | int assigning; |
| 1290 | { |
| 1291 | int i; |
| 1292 | if (TYPE(n) != testlist) |
| 1293 | REQ(n, exprlist); |
| 1294 | if (assigning) |
| 1295 | com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); |
| 1296 | for (i = 0; i < NCH(n); i += 2) |
| 1297 | com_assign(c, CHILD(n, i), assigning); |
| 1298 | } |
| 1299 | |
| 1300 | static void |
| 1301 | com_assign_list(c, n, assigning) |
| 1302 | struct compiling *c; |
| 1303 | node *n; |
| 1304 | int assigning; |
| 1305 | { |
| 1306 | int i; |
| 1307 | if (assigning) |
| 1308 | com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2); |
| 1309 | for (i = 0; i < NCH(n); i += 2) |
| 1310 | com_assign(c, CHILD(n, i), assigning); |
| 1311 | } |
| 1312 | |
| 1313 | static void |
| 1314 | com_assign_name(c, n, assigning) |
| 1315 | struct compiling *c; |
| 1316 | node *n; |
| 1317 | int assigning; |
| 1318 | { |
| 1319 | REQ(n, NAME); |
| 1320 | com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n); |
| 1321 | } |
| 1322 | |
| 1323 | static void |
| 1324 | com_assign(c, n, assigning) |
| 1325 | struct compiling *c; |
| 1326 | node *n; |
| 1327 | int assigning; |
| 1328 | { |
| 1329 | /* Loop to avoid trivial recursion */ |
| 1330 | for (;;) { |
| 1331 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1332 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1333 | case exprlist: |
| 1334 | case testlist: |
| 1335 | if (NCH(n) > 1) { |
| 1336 | com_assign_tuple(c, n, assigning); |
| 1337 | return; |
| 1338 | } |
| 1339 | n = CHILD(n, 0); |
| 1340 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1341 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1342 | case test: |
| 1343 | case and_test: |
| 1344 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1345 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1346 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1347 | case xor_expr: |
| 1348 | case and_expr: |
| 1349 | case shift_expr: |
| 1350 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1351 | case term: |
| 1352 | if (NCH(n) > 1) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1353 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1354 | "can't assign to operator"); |
| 1355 | c->c_errors++; |
| 1356 | return; |
| 1357 | } |
| 1358 | n = CHILD(n, 0); |
| 1359 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1360 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1361 | case factor: /* ('+'|'-'|'~') factor | atom trailer* */ |
| 1362 | if (TYPE(CHILD(n, 0)) != atom) { /* '+'|'-'|'~' */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1363 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1364 | "can't assign to operator"); |
| 1365 | c->c_errors++; |
| 1366 | return; |
| 1367 | } |
| 1368 | if (NCH(n) > 1) { /* trailer present */ |
| 1369 | int i; |
| 1370 | com_node(c, CHILD(n, 0)); |
| 1371 | for (i = 1; i+1 < NCH(n); i++) { |
| 1372 | com_apply_trailer(c, CHILD(n, i)); |
| 1373 | } /* NB i is still alive */ |
| 1374 | com_assign_trailer(c, |
| 1375 | CHILD(n, i), assigning); |
| 1376 | return; |
| 1377 | } |
| 1378 | n = CHILD(n, 0); |
| 1379 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1380 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1381 | case atom: |
| 1382 | switch (TYPE(CHILD(n, 0))) { |
| 1383 | case LPAR: |
| 1384 | n = CHILD(n, 1); |
| 1385 | if (TYPE(n) == RPAR) { |
| 1386 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1387 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1388 | "can't assign to ()"); |
| 1389 | c->c_errors++; |
| 1390 | return; |
| 1391 | } |
| 1392 | break; |
| 1393 | case LSQB: |
| 1394 | n = CHILD(n, 1); |
| 1395 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1396 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1397 | "can't assign to []"); |
| 1398 | c->c_errors++; |
| 1399 | return; |
| 1400 | } |
| 1401 | com_assign_list(c, n, assigning); |
| 1402 | return; |
| 1403 | case NAME: |
| 1404 | com_assign_name(c, CHILD(n, 0), assigning); |
| 1405 | return; |
| 1406 | default: |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1407 | err_setstr(SyntaxError, |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 1408 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1409 | c->c_errors++; |
| 1410 | return; |
| 1411 | } |
| 1412 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1413 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1414 | default: |
| 1415 | fprintf(stderr, "node type %d\n", TYPE(n)); |
| 1416 | err_setstr(SystemError, "com_assign: bad node"); |
| 1417 | c->c_errors++; |
| 1418 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1419 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | static void |
| 1425 | com_expr_stmt(c, n) |
| 1426 | struct compiling *c; |
| 1427 | node *n; |
| 1428 | { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1429 | REQ(n, expr_stmt); /* testlist ('=' testlist)* */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1430 | com_node(c, CHILD(n, NCH(n)-1)); |
| 1431 | if (NCH(n) == 1) { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1432 | if (c->c_interactive) |
| 1433 | com_addbyte(c, PRINT_EXPR); |
| 1434 | else |
| 1435 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1436 | } |
| 1437 | else { |
| 1438 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1439 | for (i = 0; i < NCH(n)-2; i+=2) { |
| 1440 | if (i+2 < NCH(n)-2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1441 | com_addbyte(c, DUP_TOP); |
| 1442 | com_assign(c, CHILD(n, i), 1/*assign*/); |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | static void |
| 1448 | com_print_stmt(c, n) |
| 1449 | struct compiling *c; |
| 1450 | node *n; |
| 1451 | { |
| 1452 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1453 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
| 1454 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1455 | com_node(c, CHILD(n, i)); |
| 1456 | com_addbyte(c, PRINT_ITEM); |
| 1457 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1458 | if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1459 | com_addbyte(c, PRINT_NEWLINE); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1460 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static void |
| 1464 | com_return_stmt(c, n) |
| 1465 | struct compiling *c; |
| 1466 | node *n; |
| 1467 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1468 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1469 | if (!c->c_infunction) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1470 | err_setstr(SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1471 | c->c_errors++; |
| 1472 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1473 | if (NCH(n) < 2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1474 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1475 | else |
| 1476 | com_node(c, CHILD(n, 1)); |
| 1477 | com_addbyte(c, RETURN_VALUE); |
| 1478 | } |
| 1479 | |
| 1480 | static void |
| 1481 | com_raise_stmt(c, n) |
| 1482 | struct compiling *c; |
| 1483 | node *n; |
| 1484 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1485 | REQ(n, raise_stmt); /* 'raise' test [',' test] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1486 | com_node(c, CHILD(n, 1)); |
| 1487 | if (NCH(n) > 3) |
| 1488 | com_node(c, CHILD(n, 3)); |
| 1489 | else |
| 1490 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1491 | com_addbyte(c, RAISE_EXCEPTION); |
| 1492 | } |
| 1493 | |
| 1494 | static void |
| 1495 | com_import_stmt(c, n) |
| 1496 | struct compiling *c; |
| 1497 | node *n; |
| 1498 | { |
| 1499 | int i; |
| 1500 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1501 | /* 'import' dotted_name (',' dotted_name)* | |
| 1502 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1503 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1504 | /* 'from' dotted_name 'import' ... */ |
| 1505 | REQ(CHILD(n, 1), dotted_name); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1506 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
| 1507 | for (i = 3; i < NCH(n); i += 2) |
| 1508 | com_addopname(c, IMPORT_FROM, CHILD(n, i)); |
| 1509 | com_addbyte(c, POP_TOP); |
| 1510 | } |
| 1511 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1512 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1513 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1514 | REQ(CHILD(n, i), dotted_name); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1515 | com_addopname(c, IMPORT_NAME, CHILD(n, i)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1516 | com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | static void |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1522 | com_global_stmt(c, n) |
| 1523 | struct compiling *c; |
| 1524 | node *n; |
| 1525 | { |
| 1526 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1527 | REQ(n, global_stmt); |
| 1528 | /* 'global' NAME (',' NAME)* */ |
| 1529 | for (i = 1; i < NCH(n); i += 2) { |
| 1530 | if (dictinsert(c->c_globals, STR(CHILD(n, i)), None) != 0) |
| 1531 | c->c_errors++; |
| 1532 | } |
| 1533 | } |
| 1534 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1535 | #define strequ(a, b) (strcmp((a), (b)) == 0) |
| 1536 | |
| 1537 | static void |
| 1538 | com_access_stmt(c, n) |
| 1539 | struct compiling *c; |
| 1540 | node *n; |
| 1541 | { |
| 1542 | int i, j, k, mode, imode; |
| 1543 | object *vmode; |
| 1544 | REQ(n, access_stmt); |
| 1545 | /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)* |
| 1546 | accesstype: NAME+ */ |
| 1547 | |
| 1548 | /* Find where the colon is */ |
| 1549 | i = 1; |
| 1550 | while (TYPE(CHILD(n,i-1)) != COLON) |
| 1551 | i += 1; |
| 1552 | |
| 1553 | /* Calculate the mode mask */ |
| 1554 | mode = 0; |
| 1555 | for (j = i; j < NCH(n); j += 2) { |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1556 | int r = 0, w = 0, p = 0; |
| 1557 | for (k = 0; k < NCH(CHILD(n,j)); k++) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1558 | if (strequ(STR(CHILD(CHILD(n,j),k)), "public")) |
| 1559 | p = 0; |
| 1560 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected")) |
| 1561 | p = 1; |
| 1562 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "private")) |
| 1563 | p = 2; |
| 1564 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "read")) |
| 1565 | r = 1; |
| 1566 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "write")) |
| 1567 | w = 1; |
| 1568 | else /* XXX should make this an exception */ |
| 1569 | fprintf(stderr, "bad access type %s\n", |
| 1570 | STR(CHILD(CHILD(n,j),k))); |
| 1571 | } |
| 1572 | if (r == 0 && w == 0) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1573 | r = w = 1; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1574 | if (p == 0) { |
| 1575 | if (r == 1) mode |= AC_R_PUBLIC; |
| 1576 | if (w == 1) mode |= AC_W_PUBLIC; |
| 1577 | } else if (p == 1) { |
| 1578 | if (r == 1) mode |= AC_R_PROTECTED; |
| 1579 | if (w == 1) mode |= AC_W_PROTECTED; |
| 1580 | } else { |
| 1581 | if (r == 1) mode |= AC_R_PRIVATE; |
| 1582 | if (w == 1) mode |= AC_W_PRIVATE; |
| 1583 | } |
| 1584 | } |
| 1585 | vmode = newintobject((long)mode); |
| 1586 | imode = com_addconst(c, vmode); |
| 1587 | XDECREF(vmode); |
| 1588 | for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) { |
| 1589 | com_addoparg(c, LOAD_CONST, imode); |
| 1590 | com_addopname(c, ACCESS_MODE, CHILD(n, i)); |
| 1591 | } |
| 1592 | } |
| 1593 | |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1594 | static void |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1595 | com_exec_stmt(c, n) |
| 1596 | struct compiling *c; |
| 1597 | node *n; |
| 1598 | { |
| 1599 | REQ(n, exec_stmt); |
| 1600 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 1601 | com_node(c, CHILD(n, 1)); |
| 1602 | if (NCH(n) >= 4) |
| 1603 | com_node(c, CHILD(n, 3)); |
| 1604 | else |
| 1605 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1606 | if (NCH(n) >= 6) |
| 1607 | com_node(c, CHILD(n, 5)); |
| 1608 | else |
| 1609 | com_addbyte(c, DUP_TOP); |
| 1610 | com_addbyte(c, EXEC_STMT); |
| 1611 | } |
| 1612 | |
| 1613 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1614 | com_if_stmt(c, n) |
| 1615 | struct compiling *c; |
| 1616 | node *n; |
| 1617 | { |
| 1618 | int i; |
| 1619 | int anchor = 0; |
| 1620 | REQ(n, if_stmt); |
| 1621 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 1622 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 1623 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1624 | node *ch = CHILD(n, i+1); |
| 1625 | if (i > 0) |
| 1626 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1627 | com_node(c, CHILD(n, i+1)); |
| 1628 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1629 | com_addbyte(c, POP_TOP); |
| 1630 | com_node(c, CHILD(n, i+3)); |
| 1631 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1632 | com_backpatch(c, a); |
| 1633 | com_addbyte(c, POP_TOP); |
| 1634 | } |
| 1635 | if (i+2 < NCH(n)) |
| 1636 | com_node(c, CHILD(n, i+2)); |
| 1637 | com_backpatch(c, anchor); |
| 1638 | } |
| 1639 | |
| 1640 | static void |
| 1641 | com_while_stmt(c, n) |
| 1642 | struct compiling *c; |
| 1643 | node *n; |
| 1644 | { |
| 1645 | int break_anchor = 0; |
| 1646 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1647 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1648 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 1649 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1650 | block_push(c, SETUP_LOOP); |
| 1651 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1652 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1653 | com_node(c, CHILD(n, 1)); |
| 1654 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1655 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1656 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1657 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1658 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1659 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1660 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1661 | com_backpatch(c, anchor); |
| 1662 | com_addbyte(c, POP_TOP); |
| 1663 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1664 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1665 | if (NCH(n) > 4) |
| 1666 | com_node(c, CHILD(n, 6)); |
| 1667 | com_backpatch(c, break_anchor); |
| 1668 | } |
| 1669 | |
| 1670 | static void |
| 1671 | com_for_stmt(c, n) |
| 1672 | struct compiling *c; |
| 1673 | node *n; |
| 1674 | { |
| 1675 | object *v; |
| 1676 | int break_anchor = 0; |
| 1677 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1678 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1679 | REQ(n, for_stmt); |
| 1680 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 1681 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1682 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1683 | com_node(c, CHILD(n, 3)); |
| 1684 | v = newintobject(0L); |
| 1685 | if (v == NULL) |
| 1686 | c->c_errors++; |
| 1687 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
| 1688 | XDECREF(v); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1689 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1690 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1691 | com_addfwref(c, FOR_LOOP, &anchor); |
| 1692 | com_assign(c, CHILD(n, 1), 1/*assigning*/); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1693 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1694 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1695 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1696 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1697 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1698 | com_backpatch(c, anchor); |
| 1699 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1700 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1701 | if (NCH(n) > 8) |
| 1702 | com_node(c, CHILD(n, 8)); |
| 1703 | com_backpatch(c, break_anchor); |
| 1704 | } |
| 1705 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1706 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1707 | |
| 1708 | SETUP_FINALLY L |
| 1709 | <code for S> |
| 1710 | POP_BLOCK |
| 1711 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1712 | L: <code for Sf> |
| 1713 | END_FINALLY |
| 1714 | |
| 1715 | The special instructions use the block stack. Each block |
| 1716 | stack entry contains the instruction that created it (here |
| 1717 | SETUP_FINALLY), the level of the value stack at the time the |
| 1718 | block stack entry was created, and a label (here L). |
| 1719 | |
| 1720 | SETUP_FINALLY: |
| 1721 | Pushes the current value stack level and the label |
| 1722 | onto the block stack. |
| 1723 | POP_BLOCK: |
| 1724 | Pops en entry from the block stack, and pops the value |
| 1725 | stack until its level is the same as indicated on the |
| 1726 | block stack. (The label is ignored.) |
| 1727 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1728 | Pops a variable number of entries from the *value* stack |
| 1729 | and re-raises the exception they specify. The number of |
| 1730 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1731 | |
| 1732 | The block stack is unwound when an exception is raised: |
| 1733 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 1734 | onto the value stack (and the exception condition is cleared), |
| 1735 | and the interpreter jumps to the label gotten from the block |
| 1736 | stack. |
| 1737 | |
| 1738 | 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] | 1739 | (The contents of the value stack is shown in [], with the top |
| 1740 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 1741 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1742 | |
| 1743 | Value stack Label Instruction Argument |
| 1744 | [] SETUP_EXCEPT L1 |
| 1745 | [] <code for S> |
| 1746 | [] POP_BLOCK |
| 1747 | [] JUMP_FORWARD L0 |
| 1748 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1749 | [tb, val, exc] L1: DUP ) |
| 1750 | [tb, val, exc, exc] <evaluate E1> ) |
| 1751 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 1752 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 1753 | [tb, val, exc, 1] POP ) |
| 1754 | [tb, val, exc] POP |
| 1755 | [tb, val] <assign to V1> (or POP if no V1) |
| 1756 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1757 | [] <code for S1> |
| 1758 | JUMP_FORWARD L0 |
| 1759 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1760 | [tb, val, exc, 0] L2: POP |
| 1761 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1762 | .............................etc....................... |
| 1763 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1764 | [tb, val, exc, 0] Ln+1: POP |
| 1765 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1766 | |
| 1767 | [] L0: <next statement> |
| 1768 | |
| 1769 | Of course, parts are not generated if Vi or Ei is not present. |
| 1770 | */ |
| 1771 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1772 | static void |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1773 | com_try_except(c, n) |
| 1774 | struct compiling *c; |
| 1775 | node *n; |
| 1776 | { |
| 1777 | int except_anchor = 0; |
| 1778 | int end_anchor = 0; |
| 1779 | int else_anchor = 0; |
| 1780 | int i; |
| 1781 | node *ch; |
| 1782 | |
| 1783 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 1784 | block_push(c, SETUP_EXCEPT); |
| 1785 | com_node(c, CHILD(n, 2)); |
| 1786 | com_addbyte(c, POP_BLOCK); |
| 1787 | block_pop(c, SETUP_EXCEPT); |
| 1788 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 1789 | com_backpatch(c, except_anchor); |
| 1790 | for (i = 3; |
| 1791 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 1792 | i += 3) { |
| 1793 | /* except_clause: 'except' [expr [',' expr]] */ |
| 1794 | if (except_anchor == 0) { |
| 1795 | err_setstr(SyntaxError, |
| 1796 | "default 'except:' must be last"); |
| 1797 | c->c_errors++; |
| 1798 | break; |
| 1799 | } |
| 1800 | except_anchor = 0; |
| 1801 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 1802 | if (NCH(ch) > 1) { |
| 1803 | com_addbyte(c, DUP_TOP); |
| 1804 | com_node(c, CHILD(ch, 1)); |
| 1805 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
| 1806 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 1807 | com_addbyte(c, POP_TOP); |
| 1808 | } |
| 1809 | com_addbyte(c, POP_TOP); |
| 1810 | if (NCH(ch) > 3) |
| 1811 | com_assign(c, CHILD(ch, 3), 1/*assigning*/); |
| 1812 | else |
| 1813 | com_addbyte(c, POP_TOP); |
| 1814 | com_addbyte(c, POP_TOP); |
| 1815 | com_node(c, CHILD(n, i+2)); |
| 1816 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 1817 | if (except_anchor) { |
| 1818 | com_backpatch(c, except_anchor); |
| 1819 | com_addbyte(c, POP_TOP); |
| 1820 | } |
| 1821 | } |
| 1822 | com_addbyte(c, END_FINALLY); |
| 1823 | com_backpatch(c, else_anchor); |
| 1824 | if (i < NCH(n)) |
| 1825 | com_node(c, CHILD(n, i+2)); |
| 1826 | com_backpatch(c, end_anchor); |
| 1827 | } |
| 1828 | |
| 1829 | static void |
| 1830 | com_try_finally(c, n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1831 | struct compiling *c; |
| 1832 | node *n; |
| 1833 | { |
| 1834 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1835 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 1836 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1837 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 1838 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1839 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1840 | com_addbyte(c, POP_BLOCK); |
| 1841 | block_pop(c, SETUP_FINALLY); |
| 1842 | block_push(c, END_FINALLY); |
| 1843 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1844 | com_backpatch(c, finally_anchor); |
| 1845 | ch = CHILD(n, NCH(n)-1); |
| 1846 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 1847 | com_node(c, ch); |
| 1848 | com_addbyte(c, END_FINALLY); |
| 1849 | block_pop(c, END_FINALLY); |
| 1850 | } |
| 1851 | |
| 1852 | static void |
| 1853 | com_try_stmt(c, n) |
| 1854 | struct compiling *c; |
| 1855 | node *n; |
| 1856 | { |
| 1857 | REQ(n, try_stmt); |
| 1858 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 1859 | | 'try' ':' suite 'finally' ':' suite */ |
| 1860 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 1861 | com_try_finally(c, n); |
| 1862 | else |
| 1863 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1866 | static object * |
| 1867 | get_docstring(n) |
| 1868 | node *n; |
| 1869 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 1870 | int i; |
| 1871 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1872 | switch (TYPE(n)) { |
| 1873 | |
| 1874 | case suite: |
| 1875 | if (NCH(n) == 1) |
| 1876 | return get_docstring(CHILD(n, 0)); |
| 1877 | else { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1878 | for (i = 0; i < NCH(n); i++) { |
| 1879 | node *ch = CHILD(n, i); |
| 1880 | if (TYPE(ch) == stmt) |
| 1881 | return get_docstring(ch); |
| 1882 | } |
| 1883 | } |
| 1884 | break; |
| 1885 | |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 1886 | case file_input: |
| 1887 | for (i = 0; i < NCH(n); i++) { |
| 1888 | node *ch = CHILD(n, i); |
| 1889 | if (TYPE(ch) == stmt) |
| 1890 | return get_docstring(ch); |
| 1891 | } |
| 1892 | break; |
| 1893 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1894 | case stmt: |
| 1895 | case simple_stmt: |
| 1896 | case small_stmt: |
| 1897 | return get_docstring(CHILD(n, 0)); |
| 1898 | |
| 1899 | case expr_stmt: |
| 1900 | case testlist: |
| 1901 | case test: |
| 1902 | case and_test: |
| 1903 | case not_test: |
| 1904 | case comparison: |
| 1905 | case expr: |
| 1906 | case xor_expr: |
| 1907 | case and_expr: |
| 1908 | case shift_expr: |
| 1909 | case arith_expr: |
| 1910 | case term: |
| 1911 | case factor: |
| 1912 | if (NCH(n) == 1) |
| 1913 | return get_docstring(CHILD(n, 0)); |
| 1914 | break; |
| 1915 | |
| 1916 | case atom: |
| 1917 | if (TYPE(CHILD(n, 0)) == STRING) |
| 1918 | return parsestrplus(n); |
| 1919 | break; |
| 1920 | |
| 1921 | } |
| 1922 | return NULL; |
| 1923 | } |
| 1924 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1925 | static void |
| 1926 | com_suite(c, n) |
| 1927 | struct compiling *c; |
| 1928 | node *n; |
| 1929 | { |
| 1930 | REQ(n, suite); |
| 1931 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 1932 | if (NCH(n) == 1) { |
| 1933 | com_node(c, CHILD(n, 0)); |
| 1934 | } |
| 1935 | else { |
| 1936 | int i; |
| 1937 | for (i = 0; i < NCH(n); i++) { |
| 1938 | node *ch = CHILD(n, i); |
| 1939 | if (TYPE(ch) == stmt) |
| 1940 | com_node(c, ch); |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1945 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1946 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1947 | com_continue_stmt(c, n) |
| 1948 | struct compiling *c; |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1949 | node *n; /* Not used, but passed for consistency */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1950 | { |
| 1951 | int i = c->c_nblocks; |
| 1952 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 1953 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1954 | } |
| 1955 | else { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1956 | err_setstr(SyntaxError, "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1957 | c->c_errors++; |
| 1958 | } |
| 1959 | /* XXX Could allow it inside a 'finally' clause |
| 1960 | XXX if we could pop the exception still on the stack */ |
| 1961 | } |
| 1962 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1963 | static int |
| 1964 | com_argdefs(c, n, argcount_return) |
| 1965 | struct compiling *c; |
| 1966 | node *n; |
| 1967 | int *argcount_return; |
| 1968 | { |
| 1969 | int i, nch, nargs, ndefs, star; |
| 1970 | if (TYPE(n) == lambdef) { |
| 1971 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 1972 | n = CHILD(n, 1); |
| 1973 | } |
| 1974 | else { |
| 1975 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 1976 | n = CHILD(n, 2); |
| 1977 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 1978 | n = CHILD(n, 1); |
| 1979 | } |
| 1980 | if (TYPE(n) != varargslist) |
| 1981 | return -1; |
| 1982 | /* varargslist: |
| 1983 | (fpdef ['=' test] ',')* '*' NAME | |
| 1984 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 1985 | nch = NCH(n); |
| 1986 | if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) { |
| 1987 | star = 1; |
| 1988 | nch -= 2; |
| 1989 | } |
| 1990 | else |
| 1991 | star = 0; |
| 1992 | nargs = 0; |
| 1993 | ndefs = 0; |
| 1994 | for (i = 0; i < nch; i++) { |
| 1995 | int t; |
| 1996 | nargs++; |
| 1997 | i++; |
| 1998 | if (i >= nch) |
| 1999 | break; |
| 2000 | t = TYPE(CHILD(n, i)); |
| 2001 | if (t == EQUAL) { |
| 2002 | i++; |
| 2003 | ndefs++; |
| 2004 | com_node(c, CHILD(n, i)); |
| 2005 | i++; |
| 2006 | if (i >= nch) |
| 2007 | break; |
| 2008 | t = TYPE(CHILD(n, i)); |
| 2009 | } |
| 2010 | else { |
| 2011 | /* Treat "(a=1, b)" as "(a=1, b=None)" */ |
| 2012 | if (ndefs) { |
| 2013 | com_addoparg(c, LOAD_CONST, |
| 2014 | com_addconst(c, None)); |
| 2015 | ndefs++; |
| 2016 | } |
| 2017 | } |
| 2018 | if (t != COMMA) |
| 2019 | break; |
| 2020 | } |
| 2021 | if (star) |
| 2022 | nargs ^= 0x4000; |
| 2023 | *argcount_return = nargs; |
| 2024 | if (ndefs > 0) |
| 2025 | com_addoparg(c, BUILD_TUPLE, ndefs); |
| 2026 | return ndefs; |
| 2027 | } |
| 2028 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2029 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2030 | com_funcdef(c, n) |
| 2031 | struct compiling *c; |
| 2032 | node *n; |
| 2033 | { |
| 2034 | object *v; |
| 2035 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2036 | v = (object *)compile(n, c->c_filename); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2037 | if (v == NULL) |
| 2038 | c->c_errors++; |
| 2039 | else { |
| 2040 | int i = com_addconst(c, v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2041 | int argcount; |
| 2042 | int ndefs = com_argdefs(c, n, &argcount); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2043 | com_addoparg(c, LOAD_CONST, i); |
| 2044 | com_addbyte(c, BUILD_FUNCTION); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2045 | if (ndefs > 0) |
| 2046 | com_addoparg(c, SET_FUNC_ARGS, argcount); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2047 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 2048 | DECREF(v); |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | static void |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2053 | com_bases(c, n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2054 | struct compiling *c; |
| 2055 | node *n; |
| 2056 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 2057 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2058 | REQ(n, testlist); |
| 2059 | /* testlist: test (',' test)* [','] */ |
| 2060 | for (i = 0; i < NCH(n); i += 2) |
| 2061 | com_node(c, CHILD(n, i)); |
| 2062 | com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2); |
| 2063 | } |
| 2064 | |
| 2065 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2066 | com_classdef(c, n) |
| 2067 | struct compiling *c; |
| 2068 | node *n; |
| 2069 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2070 | int i; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2071 | object *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2072 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2073 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
| 2074 | if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) { |
| 2075 | c->c_errors++; |
| 2076 | return; |
| 2077 | } |
| 2078 | /* Push the class name on the stack */ |
| 2079 | i = com_addconst(c, v); |
| 2080 | com_addoparg(c, LOAD_CONST, i); |
| 2081 | DECREF(v); |
| 2082 | /* Push the tuple of base classes on the stack */ |
| 2083 | if (TYPE(CHILD(n, 2)) != LPAR) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2084 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2085 | else |
| 2086 | com_bases(c, CHILD(n, 3)); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2087 | v = (object *)compile(n, c->c_filename); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2088 | if (v == NULL) |
| 2089 | c->c_errors++; |
| 2090 | else { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2091 | i = com_addconst(c, v); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2092 | com_addoparg(c, LOAD_CONST, i); |
| 2093 | com_addbyte(c, BUILD_FUNCTION); |
| 2094 | com_addbyte(c, UNARY_CALL); |
| 2095 | com_addbyte(c, BUILD_CLASS); |
| 2096 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 2097 | DECREF(v); |
| 2098 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | static void |
| 2102 | com_node(c, n) |
| 2103 | struct compiling *c; |
| 2104 | node *n; |
| 2105 | { |
| 2106 | switch (TYPE(n)) { |
| 2107 | |
| 2108 | /* Definition nodes */ |
| 2109 | |
| 2110 | case funcdef: |
| 2111 | com_funcdef(c, n); |
| 2112 | break; |
| 2113 | case classdef: |
| 2114 | com_classdef(c, n); |
| 2115 | break; |
| 2116 | |
| 2117 | /* Trivial parse tree nodes */ |
| 2118 | |
| 2119 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2120 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2121 | case flow_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2122 | com_node(c, CHILD(n, 0)); |
| 2123 | break; |
| 2124 | |
| 2125 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2126 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 2127 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2128 | { |
| 2129 | int i; |
| 2130 | for (i = 0; i < NCH(n)-1; i += 2) |
| 2131 | com_node(c, CHILD(n, i)); |
| 2132 | } |
| 2133 | break; |
| 2134 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2135 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2136 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2137 | com_node(c, CHILD(n, 0)); |
| 2138 | break; |
| 2139 | |
| 2140 | /* Statement nodes */ |
| 2141 | |
| 2142 | case expr_stmt: |
| 2143 | com_expr_stmt(c, n); |
| 2144 | break; |
| 2145 | case print_stmt: |
| 2146 | com_print_stmt(c, n); |
| 2147 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2148 | case del_stmt: /* 'del' exprlist */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2149 | com_assign(c, CHILD(n, 1), 0/*delete*/); |
| 2150 | break; |
| 2151 | case pass_stmt: |
| 2152 | break; |
| 2153 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2154 | if (c->c_loops == 0) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 2155 | err_setstr(SyntaxError, "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2156 | c->c_errors++; |
| 2157 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2158 | com_addbyte(c, BREAK_LOOP); |
| 2159 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2160 | case continue_stmt: |
| 2161 | com_continue_stmt(c, n); |
| 2162 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2163 | case return_stmt: |
| 2164 | com_return_stmt(c, n); |
| 2165 | break; |
| 2166 | case raise_stmt: |
| 2167 | com_raise_stmt(c, n); |
| 2168 | break; |
| 2169 | case import_stmt: |
| 2170 | com_import_stmt(c, n); |
| 2171 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2172 | case global_stmt: |
| 2173 | com_global_stmt(c, n); |
| 2174 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2175 | case access_stmt: |
| 2176 | com_access_stmt(c, n); |
| 2177 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2178 | case exec_stmt: |
| 2179 | com_exec_stmt(c, n); |
| 2180 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2181 | case if_stmt: |
| 2182 | com_if_stmt(c, n); |
| 2183 | break; |
| 2184 | case while_stmt: |
| 2185 | com_while_stmt(c, n); |
| 2186 | break; |
| 2187 | case for_stmt: |
| 2188 | com_for_stmt(c, n); |
| 2189 | break; |
| 2190 | case try_stmt: |
| 2191 | com_try_stmt(c, n); |
| 2192 | break; |
| 2193 | case suite: |
| 2194 | com_suite(c, n); |
| 2195 | break; |
| 2196 | |
| 2197 | /* Expression nodes */ |
| 2198 | |
| 2199 | case testlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2200 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2201 | break; |
| 2202 | case test: |
| 2203 | com_test(c, n); |
| 2204 | break; |
| 2205 | case and_test: |
| 2206 | com_and_test(c, n); |
| 2207 | break; |
| 2208 | case not_test: |
| 2209 | com_not_test(c, n); |
| 2210 | break; |
| 2211 | case comparison: |
| 2212 | com_comparison(c, n); |
| 2213 | break; |
| 2214 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2215 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2216 | break; |
| 2217 | case expr: |
| 2218 | com_expr(c, n); |
| 2219 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2220 | case xor_expr: |
| 2221 | com_xor_expr(c, n); |
| 2222 | break; |
| 2223 | case and_expr: |
| 2224 | com_and_expr(c, n); |
| 2225 | break; |
| 2226 | case shift_expr: |
| 2227 | com_shift_expr(c, n); |
| 2228 | break; |
| 2229 | case arith_expr: |
| 2230 | com_arith_expr(c, n); |
| 2231 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2232 | case term: |
| 2233 | com_term(c, n); |
| 2234 | break; |
| 2235 | case factor: |
| 2236 | com_factor(c, n); |
| 2237 | break; |
| 2238 | case atom: |
| 2239 | com_atom(c, n); |
| 2240 | break; |
| 2241 | |
| 2242 | default: |
| 2243 | fprintf(stderr, "node type %d\n", TYPE(n)); |
| 2244 | err_setstr(SystemError, "com_node: unexpected node type"); |
| 2245 | c->c_errors++; |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | static void com_fplist PROTO((struct compiling *, node *)); |
| 2250 | |
| 2251 | static void |
| 2252 | com_fpdef(c, n) |
| 2253 | struct compiling *c; |
| 2254 | node *n; |
| 2255 | { |
| 2256 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 2257 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 2258 | com_fplist(c, CHILD(n, 1)); |
| 2259 | else |
| 2260 | com_addopname(c, STORE_NAME, CHILD(n, 0)); |
| 2261 | } |
| 2262 | |
| 2263 | static void |
| 2264 | com_fplist(c, n) |
| 2265 | struct compiling *c; |
| 2266 | node *n; |
| 2267 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 2268 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2269 | if (NCH(n) == 1) { |
| 2270 | com_fpdef(c, CHILD(n, 0)); |
| 2271 | } |
| 2272 | else { |
| 2273 | int i; |
| 2274 | com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); |
| 2275 | for (i = 0; i < NCH(n); i += 2) |
| 2276 | com_fpdef(c, CHILD(n, i)); |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | static void |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2281 | com_arglist(c, n) |
| 2282 | struct compiling *c; |
| 2283 | node *n; |
| 2284 | { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2285 | int nch, op, nargs, i, t; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2286 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 2287 | /* varargslist: |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2288 | (fpdef ['=' test] ',')* '*' NAME | |
| 2289 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 2290 | nch = NCH(n); |
| 2291 | if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) { |
| 2292 | op = UNPACK_VARARG; |
| 2293 | nch -= 2; |
| 2294 | } |
| 2295 | else |
| 2296 | op = UNPACK_ARG; |
| 2297 | nargs = 0; |
| 2298 | for (i = 0; i < nch; i++) { |
| 2299 | nargs++; |
| 2300 | i++; |
| 2301 | if (i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2302 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2303 | t = TYPE(CHILD(n, i)); |
| 2304 | if (t == EQUAL) { |
| 2305 | i += 2; |
| 2306 | if (i >= nch) |
| 2307 | break; |
| 2308 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2309 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2310 | if (t != COMMA) |
| 2311 | break; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2312 | } |
| 2313 | com_addoparg(c, op, nargs); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2314 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2315 | com_fpdef(c, CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2316 | i++; |
| 2317 | if (i >= nch) |
| 2318 | break; |
| 2319 | t = TYPE(CHILD(n, i)); |
| 2320 | if (t == EQUAL) { |
| 2321 | i += 2; |
| 2322 | if (i >= nch) |
| 2323 | break; |
| 2324 | t = TYPE(CHILD(n, i)); |
| 2325 | } |
| 2326 | if (t != COMMA) |
| 2327 | break; |
| 2328 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2329 | if (op == UNPACK_VARARG) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2330 | com_addopname(c, STORE_NAME, CHILD(n, nch+1)); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2334 | com_file_input(c, n) |
| 2335 | struct compiling *c; |
| 2336 | node *n; |
| 2337 | { |
| 2338 | int i; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2339 | object *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2340 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2341 | doc = get_docstring(n); |
| 2342 | if (doc != NULL) { |
| 2343 | int i = com_addconst(c, doc); |
| 2344 | DECREF(doc); |
| 2345 | com_addoparg(c, LOAD_CONST, i); |
| 2346 | com_addopnamestr(c, STORE_NAME, "__doc__"); |
| 2347 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2348 | for (i = 0; i < NCH(n); i++) { |
| 2349 | node *ch = CHILD(n, i); |
| 2350 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 2351 | com_node(c, ch); |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | /* Top-level compile-node interface */ |
| 2356 | |
| 2357 | static void |
| 2358 | compile_funcdef(c, n) |
| 2359 | struct compiling *c; |
| 2360 | node *n; |
| 2361 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2362 | object *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2363 | node *ch; |
| 2364 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2365 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2366 | doc = get_docstring(CHILD(n, 4)); |
| 2367 | if (doc != NULL) { |
| 2368 | (void) com_addconst(c, doc); |
| 2369 | DECREF(doc); |
| 2370 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2371 | com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); /* Patched! */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2372 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 2373 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2374 | if (TYPE(ch) == RPAR) |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2375 | com_addoparg(c, UNPACK_ARG, 0); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2376 | else |
| 2377 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2378 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2379 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2380 | c->c_infunction = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2381 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2382 | com_addbyte(c, RETURN_VALUE); |
| 2383 | } |
| 2384 | |
| 2385 | static void |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2386 | compile_lambdef(c, n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2387 | struct compiling *c; |
| 2388 | node *n; |
| 2389 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2390 | node *ch; |
| 2391 | REQ(n, lambdef); /* lambdef: 'lambda' [parameters] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 2392 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2393 | |
| 2394 | ch = CHILD(n, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2395 | (void) com_addconst(c, None); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2396 | if (TYPE(ch) == COLON) { |
| 2397 | com_addoparg(c, UNPACK_ARG, 0); |
| 2398 | com_node(c, CHILD(n, 2)); |
| 2399 | } |
| 2400 | else { |
| 2401 | com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); |
| 2402 | com_arglist(c, ch); |
| 2403 | com_node(c, CHILD(n, 3)); |
| 2404 | } |
| 2405 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2406 | com_addbyte(c, RETURN_VALUE); |
| 2407 | } |
| 2408 | |
| 2409 | static void |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2410 | compile_classdef(c, n) |
| 2411 | struct compiling *c; |
| 2412 | node *n; |
| 2413 | { |
| 2414 | node *ch; |
| 2415 | object *doc; |
| 2416 | REQ(n, classdef); |
| 2417 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 2418 | c->c_name = STR(CHILD(n, 1)); |
| 2419 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
| 2420 | doc = get_docstring(ch); |
| 2421 | if (doc != NULL) { |
| 2422 | int i = com_addconst(c, doc); |
| 2423 | DECREF(doc); |
| 2424 | com_addoparg(c, LOAD_CONST, i); |
| 2425 | com_addopnamestr(c, STORE_NAME, "__doc__"); |
| 2426 | } |
| 2427 | else |
| 2428 | (void) com_addconst(c, None); |
| 2429 | com_node(c, ch); |
| 2430 | com_addbyte(c, LOAD_LOCALS); |
| 2431 | com_addbyte(c, RETURN_VALUE); |
| 2432 | } |
| 2433 | |
| 2434 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2435 | compile_node(c, n) |
| 2436 | struct compiling *c; |
| 2437 | node *n; |
| 2438 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2439 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2440 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2441 | switch (TYPE(n)) { |
| 2442 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2443 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2444 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2445 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2446 | n = CHILD(n, 0); |
| 2447 | if (TYPE(n) != NEWLINE) |
| 2448 | com_node(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2449 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2450 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2451 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2452 | break; |
| 2453 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2454 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2455 | com_file_input(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2456 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2457 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2458 | break; |
| 2459 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2460 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2461 | com_node(c, CHILD(n, 0)); |
| 2462 | com_addbyte(c, RETURN_VALUE); |
| 2463 | break; |
| 2464 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2465 | case lambdef: /* anonymous function definition */ |
| 2466 | compile_lambdef(c, n); |
| 2467 | break; |
| 2468 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2469 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2470 | compile_funcdef(c, n); |
| 2471 | break; |
| 2472 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2473 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2474 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2475 | break; |
| 2476 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2477 | default: |
| 2478 | fprintf(stderr, "node type %d\n", TYPE(n)); |
| 2479 | err_setstr(SystemError, "compile_node: unexpected node type"); |
| 2480 | c->c_errors++; |
| 2481 | } |
| 2482 | } |
| 2483 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2484 | /* Optimization for local variables in functions (and *only* functions). |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2485 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2486 | This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME |
| 2487 | instructions that refer to local variables with LOAD_FAST etc. |
| 2488 | The latter instructions are much faster because they don't need to |
| 2489 | look up the variable name in a dictionary. |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2490 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2491 | To find all local variables, we check all STORE_NAME, IMPORT_FROM and |
| 2492 | DELETE_NAME instructions. This yields all local variables, including |
| 2493 | arguments, function definitions, class definitions and import |
| 2494 | statements. |
| 2495 | |
| 2496 | All remaining LOAD_NAME instructions must refer to non-local (global |
| 2497 | or builtin) variables, so are replaced by LOAD_GLOBAL. |
| 2498 | |
| 2499 | There are two problems: 'from foo import *' and 'exec' may introduce |
| 2500 | local variables that we can't know while compiling. If this is the |
| 2501 | case, we don't optimize at all (this rarely happens, since exec is |
| 2502 | rare, & this form of import statement is mostly used at the module |
| 2503 | level). |
| 2504 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2505 | NB: this modifies the string object co->co_code! |
| 2506 | */ |
| 2507 | |
| 2508 | static void |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2509 | optimize(c) |
| 2510 | struct compiling *c; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2511 | { |
Guido van Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 2512 | unsigned char *next_instr, *cur_instr; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2513 | object *locals; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2514 | int nlocals; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2515 | int opcode; |
| 2516 | int oparg; |
| 2517 | object *name; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2518 | int fast_reserved; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2519 | object *error_type, *error_value, *error_traceback; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2520 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2521 | #define NEXTOP() (*next_instr++) |
| 2522 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
| 2523 | #define GETITEM(v, i) (getlistitem((v), (i))) |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2524 | #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i))) |
| 2525 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2526 | locals = newdictobject(); |
| 2527 | if (locals == NULL) { |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2528 | c->c_errors++; |
| 2529 | return; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2530 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2531 | nlocals = 0; |
| 2532 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2533 | err_fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2534 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2535 | next_instr = (unsigned char *) getstringvalue(c->c_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2536 | for (;;) { |
| 2537 | opcode = NEXTOP(); |
| 2538 | if (opcode == STOP_CODE) |
| 2539 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2540 | if (opcode == EXEC_STMT) |
| 2541 | goto end; /* Don't optimize if exec present */ |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2542 | if (HAS_ARG(opcode)) |
| 2543 | oparg = NEXTARG(); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2544 | if (opcode == STORE_NAME || opcode == DELETE_NAME || |
| 2545 | opcode == IMPORT_FROM) { |
| 2546 | object *v; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2547 | name = GETNAMEOBJ(oparg); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2548 | if (dict2lookup(locals, name) != NULL) |
| 2549 | continue; |
| 2550 | err_clear(); |
| 2551 | v = newintobject(nlocals); |
| 2552 | if (v == NULL) { |
| 2553 | c->c_errors++; |
| 2554 | goto err; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2555 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2556 | nlocals++; |
| 2557 | if (dict2insert(locals, name, v) != 0) { |
| 2558 | DECREF(v); |
| 2559 | c->c_errors++; |
| 2560 | goto err; |
| 2561 | } |
| 2562 | DECREF(v); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2563 | } |
| 2564 | } |
| 2565 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2566 | if (dictlookup(locals, "*") != NULL) { |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2567 | /* Don't optimize anything */ |
| 2568 | goto end; |
| 2569 | } |
| 2570 | |
| 2571 | next_instr = (unsigned char *) getstringvalue(c->c_code); |
| 2572 | fast_reserved = 0; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2573 | for (;;) { |
| 2574 | cur_instr = next_instr; |
| 2575 | opcode = NEXTOP(); |
| 2576 | if (opcode == STOP_CODE) |
| 2577 | break; |
| 2578 | if (HAS_ARG(opcode)) |
| 2579 | oparg = NEXTARG(); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2580 | if (opcode == RESERVE_FAST) { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2581 | int i; |
| 2582 | object *localmap = newtupleobject(nlocals); |
| 2583 | int pos; |
| 2584 | object *key, *value; |
| 2585 | if (localmap == NULL) { /* XXX mask error */ |
| 2586 | err_clear(); |
| 2587 | continue; |
| 2588 | } |
| 2589 | pos = 0; |
| 2590 | while (mappinggetnext(locals, &pos, &key, &value)) { |
| 2591 | int j; |
| 2592 | if (!is_intobject(value)) |
| 2593 | continue; |
| 2594 | j = getintvalue(value); |
| 2595 | if (0 <= j && j < nlocals) { |
| 2596 | INCREF(key); |
| 2597 | settupleitem(localmap, j, key); |
| 2598 | } |
| 2599 | } |
| 2600 | i = com_addconst(c, localmap); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2601 | cur_instr[1] = i & 0xff; |
| 2602 | cur_instr[2] = (i>>8) & 0xff; |
| 2603 | fast_reserved = 1; |
Guido van Rossum | 3952cb0 | 1995-01-20 16:56:41 +0000 | [diff] [blame] | 2604 | DECREF(localmap); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2605 | continue; |
| 2606 | } |
| 2607 | if (!fast_reserved) |
| 2608 | continue; |
| 2609 | if (opcode == LOAD_NAME || |
| 2610 | opcode == STORE_NAME || |
| 2611 | opcode == DELETE_NAME) { |
| 2612 | object *v; |
| 2613 | int i; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2614 | name = GETNAMEOBJ(oparg); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2615 | v = dict2lookup(locals, name); |
| 2616 | if (v == NULL) { |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2617 | err_clear(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2618 | if (opcode == LOAD_NAME) |
| 2619 | cur_instr[0] = LOAD_GLOBAL; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2620 | continue; |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2621 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2622 | i = getintvalue(v); |
| 2623 | switch (opcode) { |
| 2624 | case LOAD_NAME: cur_instr[0] = LOAD_FAST; break; |
| 2625 | case STORE_NAME: cur_instr[0] = STORE_FAST; break; |
| 2626 | case DELETE_NAME: cur_instr[0] = DELETE_FAST; break; |
| 2627 | } |
| 2628 | cur_instr[1] = i & 0xff; |
| 2629 | cur_instr[2] = (i>>8) & 0xff; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2630 | } |
| 2631 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2632 | |
| 2633 | end: |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2634 | err_restore(error_type, error_value, error_traceback); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2635 | err: |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2636 | DECREF(locals); |
| 2637 | } |
| 2638 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2639 | codeobject * |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2640 | compile(n, filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2641 | node *n; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2642 | char *filename; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2643 | { |
| 2644 | struct compiling sc; |
| 2645 | codeobject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2646 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2647 | return NULL; |
| 2648 | compile_node(&sc, n); |
| 2649 | com_done(&sc); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2650 | if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2651 | optimize(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2652 | co = NULL; |
| 2653 | if (sc.c_errors == 0) { |
| 2654 | object *v, *w; |
| 2655 | v = newstringobject(sc.c_filename); |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 2656 | w = newstringobject(sc.c_name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2657 | if (v != NULL && w != NULL) |
| 2658 | co = newcodeobject(sc.c_code, sc.c_consts, |
| 2659 | sc.c_names, v, w); |
| 2660 | XDECREF(v); |
| 2661 | XDECREF(w); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 2662 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2663 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2664 | return co; |
| 2665 | } |