Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 25 | /* Compile an expression node to intermediate code */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | /* XXX TO DO: |
| 28 | XXX Compute maximum needed stack sizes while compiling |
| 29 | XXX Generate simple jump for break/return outside 'try...finally' |
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); |
| 339 | abort(); |
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 |
| 449 | com_addopname(c, op, n) |
| 450 | struct compiling *c; |
| 451 | int op; |
| 452 | node *n; |
| 453 | { |
| 454 | object *v; |
| 455 | int i; |
| 456 | char *name; |
| 457 | if (TYPE(n) == STAR) |
| 458 | name = "*"; |
| 459 | else { |
| 460 | REQ(n, NAME); |
| 461 | name = STR(n); |
| 462 | } |
| 463 | if ((v = newstringobject(name)) == NULL) { |
| 464 | c->c_errors++; |
| 465 | i = 255; |
| 466 | } |
| 467 | else { |
| 468 | i = com_addname(c, v); |
| 469 | DECREF(v); |
| 470 | } |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 471 | /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */ |
| 472 | switch (op) { |
| 473 | case LOAD_NAME: |
| 474 | case STORE_NAME: |
| 475 | case DELETE_NAME: |
| 476 | if (dictlookup(c->c_globals, name) != NULL) { |
| 477 | switch (op) { |
| 478 | case LOAD_NAME: op = LOAD_GLOBAL; break; |
| 479 | case STORE_NAME: op = STORE_GLOBAL; break; |
| 480 | case DELETE_NAME: op = DELETE_GLOBAL; break; |
| 481 | } |
| 482 | } |
| 483 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 484 | com_addoparg(c, op, i); |
| 485 | } |
| 486 | |
| 487 | static object * |
| 488 | parsenumber(s) |
| 489 | char *s; |
| 490 | { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 491 | extern long mystrtol PROTO((const char *, char **, int)); |
| 492 | extern unsigned long mystrtoul PROTO((const char *, char **, int)); |
| 493 | extern double atof PROTO((const char *)); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 494 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 495 | long x; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 496 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 497 | end = s + strlen(s) - 1; |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 498 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 499 | return long_scan(s, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 500 | if (s[0] == '0') |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 501 | x = (long) mystrtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 502 | else |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 503 | x = mystrtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 504 | if (*end == '\0') { |
| 505 | if (errno != 0) { |
Guido van Rossum | efb087b | 1991-12-16 15:41:41 +0000 | [diff] [blame] | 506 | err_setstr(OverflowError, |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 507 | "integer literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 508 | return NULL; |
| 509 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 510 | return newintobject(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 511 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 512 | /* XXX Huge floats may silently fail */ |
| 513 | return newfloatobject(atof(s)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static object * |
| 517 | parsestr(s) |
| 518 | char *s; |
| 519 | { |
| 520 | object *v; |
| 521 | int len; |
| 522 | char *buf; |
| 523 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 524 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 525 | int c; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 526 | int quote = *s; |
| 527 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 528 | err_badcall(); |
| 529 | return NULL; |
| 530 | } |
| 531 | s++; |
| 532 | len = strlen(s); |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 533 | if (s[--len] != quote) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 534 | err_badcall(); |
| 535 | return NULL; |
| 536 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 537 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 538 | s += 2; |
| 539 | len -= 2; |
| 540 | if (s[--len] != quote || s[--len] != quote) { |
| 541 | err_badcall(); |
| 542 | return NULL; |
| 543 | } |
| 544 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 545 | if (strchr(s, '\\') == NULL) |
| 546 | return newsizedstringobject(s, len); |
| 547 | v = newsizedstringobject((char *)NULL, len); |
| 548 | p = buf = getstringvalue(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 549 | end = s + len; |
| 550 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 551 | if (*s != '\\') { |
| 552 | *p++ = *s++; |
| 553 | continue; |
| 554 | } |
| 555 | s++; |
| 556 | switch (*s++) { |
| 557 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 558 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 559 | case '\\': *p++ = '\\'; break; |
| 560 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 561 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 562 | case 'b': *p++ = '\b'; break; |
| 563 | case 'f': *p++ = '\014'; break; /* FF */ |
| 564 | case 't': *p++ = '\t'; break; |
| 565 | case 'n': *p++ = '\n'; break; |
| 566 | case 'r': *p++ = '\r'; break; |
| 567 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 568 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 569 | case '0': case '1': case '2': case '3': |
| 570 | case '4': case '5': case '6': case '7': |
| 571 | c = s[-1] - '0'; |
| 572 | if ('0' <= *s && *s <= '7') { |
| 573 | c = (c<<3) + *s++ - '0'; |
| 574 | if ('0' <= *s && *s <= '7') |
| 575 | c = (c<<3) + *s++ - '0'; |
| 576 | } |
| 577 | *p++ = c; |
| 578 | break; |
| 579 | case 'x': |
| 580 | if (isxdigit(*s)) { |
| 581 | sscanf(s, "%x", &c); |
| 582 | *p++ = c; |
| 583 | do { |
| 584 | s++; |
| 585 | } while (isxdigit(*s)); |
| 586 | break; |
| 587 | } |
| 588 | /* FALLTHROUGH */ |
| 589 | default: *p++ = '\\'; *p++ = s[-1]; break; |
| 590 | } |
| 591 | } |
| 592 | resizestring(&v, (int)(p - buf)); |
| 593 | return v; |
| 594 | } |
| 595 | |
| 596 | static void |
| 597 | com_list_constructor(c, n) |
| 598 | struct compiling *c; |
| 599 | node *n; |
| 600 | { |
| 601 | int len; |
| 602 | int i; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 603 | if (TYPE(n) != testlist) |
| 604 | REQ(n, exprlist); |
| 605 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
| 606 | len = (NCH(n) + 1) / 2; |
| 607 | for (i = 0; i < NCH(n); i += 2) |
| 608 | com_node(c, CHILD(n, i)); |
| 609 | com_addoparg(c, BUILD_LIST, len); |
| 610 | } |
| 611 | |
| 612 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 613 | com_dictmaker(c, n) |
| 614 | struct compiling *c; |
| 615 | node *n; |
| 616 | { |
| 617 | int i; |
| 618 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 619 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 620 | /* We must arrange things just right for STORE_SUBSCR. |
| 621 | It wants the stack to look like (value) (dict) (key) */ |
| 622 | com_addbyte(c, DUP_TOP); |
| 623 | com_node(c, CHILD(n, i+2)); /* value */ |
| 624 | com_addbyte(c, ROT_TWO); |
| 625 | com_node(c, CHILD(n, i)); /* key */ |
| 626 | com_addbyte(c, STORE_SUBSCR); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 631 | com_atom(c, n) |
| 632 | struct compiling *c; |
| 633 | node *n; |
| 634 | { |
| 635 | node *ch; |
| 636 | object *v; |
| 637 | int i; |
| 638 | REQ(n, atom); |
| 639 | ch = CHILD(n, 0); |
| 640 | switch (TYPE(ch)) { |
| 641 | case LPAR: |
| 642 | if (TYPE(CHILD(n, 1)) == RPAR) |
| 643 | com_addoparg(c, BUILD_TUPLE, 0); |
| 644 | else |
| 645 | com_node(c, CHILD(n, 1)); |
| 646 | break; |
| 647 | case LSQB: |
| 648 | if (TYPE(CHILD(n, 1)) == RSQB) |
| 649 | com_addoparg(c, BUILD_LIST, 0); |
| 650 | else |
| 651 | com_list_constructor(c, CHILD(n, 1)); |
| 652 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 653 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 654 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 655 | if (TYPE(CHILD(n, 1)) != RBRACE) |
| 656 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 657 | break; |
| 658 | case BACKQUOTE: |
| 659 | com_node(c, CHILD(n, 1)); |
| 660 | com_addbyte(c, UNARY_CONVERT); |
| 661 | break; |
| 662 | case NUMBER: |
| 663 | if ((v = parsenumber(STR(ch))) == NULL) { |
| 664 | c->c_errors++; |
| 665 | i = 255; |
| 666 | } |
| 667 | else { |
| 668 | i = com_addconst(c, v); |
| 669 | DECREF(v); |
| 670 | } |
| 671 | com_addoparg(c, LOAD_CONST, i); |
| 672 | break; |
| 673 | case STRING: |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 674 | if ((v = parsestr(STR(ch))) != NULL) { |
| 675 | /* String literal concatenation */ |
| 676 | for (i = 1; i < NCH(n) && v != NULL; i++) { |
| 677 | joinstring_decref(&v, |
| 678 | parsestr(STR(CHILD(n, i)))); |
| 679 | } |
| 680 | } |
| 681 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 682 | c->c_errors++; |
| 683 | i = 255; |
| 684 | } |
| 685 | else { |
| 686 | i = com_addconst(c, v); |
| 687 | DECREF(v); |
| 688 | } |
| 689 | com_addoparg(c, LOAD_CONST, i); |
| 690 | break; |
| 691 | case NAME: |
| 692 | com_addopname(c, LOAD_NAME, ch); |
| 693 | break; |
| 694 | default: |
| 695 | fprintf(stderr, "node type %d\n", TYPE(ch)); |
| 696 | err_setstr(SystemError, "com_atom: unexpected node type"); |
| 697 | c->c_errors++; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | static void |
| 702 | com_slice(c, n, op) |
| 703 | struct compiling *c; |
| 704 | node *n; |
| 705 | int op; |
| 706 | { |
| 707 | if (NCH(n) == 1) { |
| 708 | com_addbyte(c, op); |
| 709 | } |
| 710 | else if (NCH(n) == 2) { |
| 711 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 712 | com_node(c, CHILD(n, 0)); |
| 713 | com_addbyte(c, op+1); |
| 714 | } |
| 715 | else { |
| 716 | com_node(c, CHILD(n, 1)); |
| 717 | com_addbyte(c, op+2); |
| 718 | } |
| 719 | } |
| 720 | else { |
| 721 | com_node(c, CHILD(n, 0)); |
| 722 | com_node(c, CHILD(n, 2)); |
| 723 | com_addbyte(c, op+3); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static void |
| 728 | com_apply_subscript(c, n) |
| 729 | struct compiling *c; |
| 730 | node *n; |
| 731 | { |
| 732 | REQ(n, subscript); |
| 733 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) { |
| 734 | /* It's a single subscript */ |
| 735 | com_node(c, CHILD(n, 0)); |
| 736 | com_addbyte(c, BINARY_SUBSCR); |
| 737 | } |
| 738 | else { |
| 739 | /* It's a slice: [expr] ':' [expr] */ |
| 740 | com_slice(c, n, SLICE); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | static void |
| 745 | com_call_function(c, n) |
| 746 | struct compiling *c; |
| 747 | node *n; /* EITHER testlist OR ')' */ |
| 748 | { |
| 749 | if (TYPE(n) == RPAR) { |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 750 | com_addoparg(c, BUILD_TUPLE, 0); |
| 751 | com_addbyte(c, BINARY_CALL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 752 | } |
| 753 | else { |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 754 | REQ(n, testlist); |
| 755 | com_list(c, n, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 756 | com_addbyte(c, BINARY_CALL); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | static void |
| 761 | com_select_member(c, n) |
| 762 | struct compiling *c; |
| 763 | node *n; |
| 764 | { |
| 765 | com_addopname(c, LOAD_ATTR, n); |
| 766 | } |
| 767 | |
| 768 | static void |
| 769 | com_apply_trailer(c, n) |
| 770 | struct compiling *c; |
| 771 | node *n; |
| 772 | { |
| 773 | REQ(n, trailer); |
| 774 | switch (TYPE(CHILD(n, 0))) { |
| 775 | case LPAR: |
| 776 | com_call_function(c, CHILD(n, 1)); |
| 777 | break; |
| 778 | case DOT: |
| 779 | com_select_member(c, CHILD(n, 1)); |
| 780 | break; |
| 781 | case LSQB: |
| 782 | com_apply_subscript(c, CHILD(n, 1)); |
| 783 | break; |
| 784 | default: |
| 785 | err_setstr(SystemError, |
| 786 | "com_apply_trailer: unknown trailer type"); |
| 787 | c->c_errors++; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | static void |
| 792 | com_factor(c, n) |
| 793 | struct compiling *c; |
| 794 | node *n; |
| 795 | { |
| 796 | int i; |
| 797 | REQ(n, factor); |
| 798 | if (TYPE(CHILD(n, 0)) == PLUS) { |
| 799 | com_factor(c, CHILD(n, 1)); |
| 800 | com_addbyte(c, UNARY_POSITIVE); |
| 801 | } |
| 802 | else if (TYPE(CHILD(n, 0)) == MINUS) { |
| 803 | com_factor(c, CHILD(n, 1)); |
| 804 | com_addbyte(c, UNARY_NEGATIVE); |
| 805 | } |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 806 | else if (TYPE(CHILD(n, 0)) == TILDE) { |
| 807 | com_factor(c, CHILD(n, 1)); |
| 808 | com_addbyte(c, UNARY_INVERT); |
| 809 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 810 | else { |
| 811 | com_atom(c, CHILD(n, 0)); |
| 812 | for (i = 1; i < NCH(n); i++) |
| 813 | com_apply_trailer(c, CHILD(n, i)); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | static void |
| 818 | com_term(c, n) |
| 819 | struct compiling *c; |
| 820 | node *n; |
| 821 | { |
| 822 | int i; |
| 823 | int op; |
| 824 | REQ(n, term); |
| 825 | com_factor(c, CHILD(n, 0)); |
| 826 | for (i = 2; i < NCH(n); i += 2) { |
| 827 | com_factor(c, CHILD(n, i)); |
| 828 | switch (TYPE(CHILD(n, i-1))) { |
| 829 | case STAR: |
| 830 | op = BINARY_MULTIPLY; |
| 831 | break; |
| 832 | case SLASH: |
| 833 | op = BINARY_DIVIDE; |
| 834 | break; |
| 835 | case PERCENT: |
| 836 | op = BINARY_MODULO; |
| 837 | break; |
| 838 | default: |
| 839 | err_setstr(SystemError, |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 840 | "com_term: operator not *, / or %"); |
| 841 | c->c_errors++; |
| 842 | op = 255; |
| 843 | } |
| 844 | com_addbyte(c, op); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | static void |
| 849 | com_arith_expr(c, n) |
| 850 | struct compiling *c; |
| 851 | node *n; |
| 852 | { |
| 853 | int i; |
| 854 | int op; |
| 855 | REQ(n, arith_expr); |
| 856 | com_term(c, CHILD(n, 0)); |
| 857 | for (i = 2; i < NCH(n); i += 2) { |
| 858 | com_term(c, CHILD(n, i)); |
| 859 | switch (TYPE(CHILD(n, i-1))) { |
| 860 | case PLUS: |
| 861 | op = BINARY_ADD; |
| 862 | break; |
| 863 | case MINUS: |
| 864 | op = BINARY_SUBTRACT; |
| 865 | break; |
| 866 | default: |
| 867 | err_setstr(SystemError, |
| 868 | "com_arith_expr: operator not + or -"); |
| 869 | c->c_errors++; |
| 870 | op = 255; |
| 871 | } |
| 872 | com_addbyte(c, op); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | static void |
| 877 | com_shift_expr(c, n) |
| 878 | struct compiling *c; |
| 879 | node *n; |
| 880 | { |
| 881 | int i; |
| 882 | int op; |
| 883 | REQ(n, shift_expr); |
| 884 | com_arith_expr(c, CHILD(n, 0)); |
| 885 | for (i = 2; i < NCH(n); i += 2) { |
| 886 | com_arith_expr(c, CHILD(n, i)); |
| 887 | switch (TYPE(CHILD(n, i-1))) { |
| 888 | case LEFTSHIFT: |
| 889 | op = BINARY_LSHIFT; |
| 890 | break; |
| 891 | case RIGHTSHIFT: |
| 892 | op = BINARY_RSHIFT; |
| 893 | break; |
| 894 | default: |
| 895 | err_setstr(SystemError, |
| 896 | "com_shift_expr: operator not << or >>"); |
| 897 | c->c_errors++; |
| 898 | op = 255; |
| 899 | } |
| 900 | com_addbyte(c, op); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | static void |
| 905 | com_and_expr(c, n) |
| 906 | struct compiling *c; |
| 907 | node *n; |
| 908 | { |
| 909 | int i; |
| 910 | int op; |
| 911 | REQ(n, and_expr); |
| 912 | com_shift_expr(c, CHILD(n, 0)); |
| 913 | for (i = 2; i < NCH(n); i += 2) { |
| 914 | com_shift_expr(c, CHILD(n, i)); |
| 915 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 916 | op = BINARY_AND; |
| 917 | } |
| 918 | else { |
| 919 | err_setstr(SystemError, |
| 920 | "com_and_expr: operator not &"); |
| 921 | c->c_errors++; |
| 922 | op = 255; |
| 923 | } |
| 924 | com_addbyte(c, op); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | static void |
| 929 | com_xor_expr(c, n) |
| 930 | struct compiling *c; |
| 931 | node *n; |
| 932 | { |
| 933 | int i; |
| 934 | int op; |
| 935 | REQ(n, xor_expr); |
| 936 | com_and_expr(c, CHILD(n, 0)); |
| 937 | for (i = 2; i < NCH(n); i += 2) { |
| 938 | com_and_expr(c, CHILD(n, i)); |
| 939 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 940 | op = BINARY_XOR; |
| 941 | } |
| 942 | else { |
| 943 | err_setstr(SystemError, |
| 944 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 945 | c->c_errors++; |
| 946 | op = 255; |
| 947 | } |
| 948 | com_addbyte(c, op); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | static void |
| 953 | com_expr(c, n) |
| 954 | struct compiling *c; |
| 955 | node *n; |
| 956 | { |
| 957 | int i; |
| 958 | int op; |
| 959 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 960 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 961 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 962 | com_xor_expr(c, CHILD(n, i)); |
| 963 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 964 | op = BINARY_OR; |
| 965 | } |
| 966 | else { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 967 | err_setstr(SystemError, |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 968 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 969 | c->c_errors++; |
| 970 | op = 255; |
| 971 | } |
| 972 | com_addbyte(c, op); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | static enum cmp_op |
| 977 | cmp_type(n) |
| 978 | node *n; |
| 979 | { |
| 980 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 981 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 982 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 983 | if (NCH(n) == 1) { |
| 984 | n = CHILD(n, 0); |
| 985 | switch (TYPE(n)) { |
| 986 | case LESS: return LT; |
| 987 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 988 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 989 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 990 | case LESSEQUAL: return LE; |
| 991 | case GREATEREQUAL: return GE; |
| 992 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 993 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 994 | if (strcmp(STR(n), "is") == 0) return IS; |
| 995 | } |
| 996 | } |
| 997 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 998 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 999 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1000 | return NOT_IN; |
| 1001 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1002 | return IS_NOT; |
| 1003 | } |
| 1004 | } |
| 1005 | return BAD; |
| 1006 | } |
| 1007 | |
| 1008 | static void |
| 1009 | com_comparison(c, n) |
| 1010 | struct compiling *c; |
| 1011 | node *n; |
| 1012 | { |
| 1013 | int i; |
| 1014 | enum cmp_op op; |
| 1015 | int anchor; |
| 1016 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 1017 | com_expr(c, CHILD(n, 0)); |
| 1018 | if (NCH(n) == 1) |
| 1019 | return; |
| 1020 | |
| 1021 | /**************************************************************** |
| 1022 | The following code is generated for all but the last |
| 1023 | comparison in a chain: |
| 1024 | |
| 1025 | label: on stack: opcode: jump to: |
| 1026 | |
| 1027 | a <code to load b> |
| 1028 | a, b DUP_TOP |
| 1029 | a, b, b ROT_THREE |
| 1030 | b, a, b COMPARE_OP |
| 1031 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 1032 | b, 1 POP_TOP |
| 1033 | b |
| 1034 | |
| 1035 | We are now ready to repeat this sequence for the next |
| 1036 | comparison in the chain. |
| 1037 | |
| 1038 | For the last we generate: |
| 1039 | |
| 1040 | b <code to load c> |
| 1041 | b, c COMPARE_OP |
| 1042 | 0-or-1 |
| 1043 | |
| 1044 | If there were any jumps to L1 (i.e., there was more than one |
| 1045 | comparison), we generate: |
| 1046 | |
| 1047 | 0-or-1 JUMP_FORWARD L2 |
| 1048 | L1: b, 0 ROT_TWO |
| 1049 | 0, b POP_TOP |
| 1050 | 0 |
| 1051 | L2: |
| 1052 | ****************************************************************/ |
| 1053 | |
| 1054 | anchor = 0; |
| 1055 | |
| 1056 | for (i = 2; i < NCH(n); i += 2) { |
| 1057 | com_expr(c, CHILD(n, i)); |
| 1058 | if (i+2 < NCH(n)) { |
| 1059 | com_addbyte(c, DUP_TOP); |
| 1060 | com_addbyte(c, ROT_THREE); |
| 1061 | } |
| 1062 | op = cmp_type(CHILD(n, i-1)); |
| 1063 | if (op == BAD) { |
| 1064 | err_setstr(SystemError, |
| 1065 | "com_comparison: unknown comparison op"); |
| 1066 | c->c_errors++; |
| 1067 | } |
| 1068 | com_addoparg(c, COMPARE_OP, op); |
| 1069 | if (i+2 < NCH(n)) { |
| 1070 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1071 | com_addbyte(c, POP_TOP); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | if (anchor) { |
| 1076 | int anchor2 = 0; |
| 1077 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 1078 | com_backpatch(c, anchor); |
| 1079 | com_addbyte(c, ROT_TWO); |
| 1080 | com_addbyte(c, POP_TOP); |
| 1081 | com_backpatch(c, anchor2); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | static void |
| 1086 | com_not_test(c, n) |
| 1087 | struct compiling *c; |
| 1088 | node *n; |
| 1089 | { |
| 1090 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 1091 | if (NCH(n) == 1) { |
| 1092 | com_comparison(c, CHILD(n, 0)); |
| 1093 | } |
| 1094 | else { |
| 1095 | com_not_test(c, CHILD(n, 1)); |
| 1096 | com_addbyte(c, UNARY_NOT); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | static void |
| 1101 | com_and_test(c, n) |
| 1102 | struct compiling *c; |
| 1103 | node *n; |
| 1104 | { |
| 1105 | int i; |
| 1106 | int anchor; |
| 1107 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 1108 | anchor = 0; |
| 1109 | i = 0; |
| 1110 | for (;;) { |
| 1111 | com_not_test(c, CHILD(n, i)); |
| 1112 | if ((i += 2) >= NCH(n)) |
| 1113 | break; |
| 1114 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1115 | com_addbyte(c, POP_TOP); |
| 1116 | } |
| 1117 | if (anchor) |
| 1118 | com_backpatch(c, anchor); |
| 1119 | } |
| 1120 | |
| 1121 | static void |
| 1122 | com_test(c, n) |
| 1123 | struct compiling *c; |
| 1124 | node *n; |
| 1125 | { |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1126 | REQ(n, test); /* and_test ('and' and_test)* | lambdef */ |
| 1127 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
| 1128 | object *v; |
| 1129 | int i; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1130 | int argcount; |
| 1131 | int ndefs = com_argdefs(c, CHILD(n, 0), &argcount); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1132 | v = (object *) compile(CHILD(n, 0), c->c_filename); |
| 1133 | if (v == NULL) { |
| 1134 | c->c_errors++; |
| 1135 | i = 255; |
| 1136 | } |
| 1137 | else { |
| 1138 | i = com_addconst(c, v); |
| 1139 | DECREF(v); |
| 1140 | } |
| 1141 | com_addoparg(c, LOAD_CONST, i); |
| 1142 | com_addbyte(c, BUILD_FUNCTION); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1143 | if (ndefs > 0) |
| 1144 | com_addoparg(c, SET_FUNC_ARGS, argcount); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1145 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1146 | else { |
| 1147 | int anchor = 0; |
| 1148 | int i = 0; |
| 1149 | for (;;) { |
| 1150 | com_and_test(c, CHILD(n, i)); |
| 1151 | if ((i += 2) >= NCH(n)) |
| 1152 | break; |
| 1153 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 1154 | com_addbyte(c, POP_TOP); |
| 1155 | } |
| 1156 | if (anchor) |
| 1157 | com_backpatch(c, anchor); |
| 1158 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | static void |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1162 | com_list(c, n, toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1163 | struct compiling *c; |
| 1164 | node *n; |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1165 | int toplevel; /* If nonzero, *always* build a tuple */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1166 | { |
| 1167 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1168 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1169 | com_node(c, CHILD(n, 0)); |
| 1170 | } |
| 1171 | else { |
| 1172 | int i; |
| 1173 | int len; |
| 1174 | len = (NCH(n) + 1) / 2; |
| 1175 | for (i = 0; i < NCH(n); i += 2) |
| 1176 | com_node(c, CHILD(n, i)); |
| 1177 | com_addoparg(c, BUILD_TUPLE, len); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | /* Begin of assignment compilation */ |
| 1183 | |
| 1184 | static void com_assign_name PROTO((struct compiling *, node *, int)); |
| 1185 | static void com_assign PROTO((struct compiling *, node *, int)); |
| 1186 | |
| 1187 | static void |
| 1188 | com_assign_attr(c, n, assigning) |
| 1189 | struct compiling *c; |
| 1190 | node *n; |
| 1191 | int assigning; |
| 1192 | { |
| 1193 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
| 1194 | } |
| 1195 | |
| 1196 | static void |
| 1197 | com_assign_slice(c, n, assigning) |
| 1198 | struct compiling *c; |
| 1199 | node *n; |
| 1200 | int assigning; |
| 1201 | { |
| 1202 | com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE); |
| 1203 | } |
| 1204 | |
| 1205 | static void |
| 1206 | com_assign_subscript(c, n, assigning) |
| 1207 | struct compiling *c; |
| 1208 | node *n; |
| 1209 | int assigning; |
| 1210 | { |
| 1211 | com_node(c, n); |
| 1212 | com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR); |
| 1213 | } |
| 1214 | |
| 1215 | static void |
| 1216 | com_assign_trailer(c, n, assigning) |
| 1217 | struct compiling *c; |
| 1218 | node *n; |
| 1219 | int assigning; |
| 1220 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1221 | REQ(n, trailer); |
| 1222 | switch (TYPE(CHILD(n, 0))) { |
| 1223 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1224 | err_setstr(SyntaxError, "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1225 | c->c_errors++; |
| 1226 | break; |
| 1227 | case DOT: /* '.' NAME */ |
| 1228 | com_assign_attr(c, CHILD(n, 1), assigning); |
| 1229 | break; |
| 1230 | case LSQB: /* '[' subscript ']' */ |
| 1231 | n = CHILD(n, 1); |
| 1232 | REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */ |
| 1233 | if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON) |
| 1234 | com_assign_slice(c, n, assigning); |
| 1235 | else |
| 1236 | com_assign_subscript(c, CHILD(n, 0), assigning); |
| 1237 | break; |
| 1238 | default: |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1239 | err_setstr(SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1240 | c->c_errors++; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | static void |
| 1245 | com_assign_tuple(c, n, assigning) |
| 1246 | struct compiling *c; |
| 1247 | node *n; |
| 1248 | int assigning; |
| 1249 | { |
| 1250 | int i; |
| 1251 | if (TYPE(n) != testlist) |
| 1252 | REQ(n, exprlist); |
| 1253 | if (assigning) |
| 1254 | com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); |
| 1255 | for (i = 0; i < NCH(n); i += 2) |
| 1256 | com_assign(c, CHILD(n, i), assigning); |
| 1257 | } |
| 1258 | |
| 1259 | static void |
| 1260 | com_assign_list(c, n, assigning) |
| 1261 | struct compiling *c; |
| 1262 | node *n; |
| 1263 | int assigning; |
| 1264 | { |
| 1265 | int i; |
| 1266 | if (assigning) |
| 1267 | com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2); |
| 1268 | for (i = 0; i < NCH(n); i += 2) |
| 1269 | com_assign(c, CHILD(n, i), assigning); |
| 1270 | } |
| 1271 | |
| 1272 | static void |
| 1273 | com_assign_name(c, n, assigning) |
| 1274 | struct compiling *c; |
| 1275 | node *n; |
| 1276 | int assigning; |
| 1277 | { |
| 1278 | REQ(n, NAME); |
| 1279 | com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n); |
| 1280 | } |
| 1281 | |
| 1282 | static void |
| 1283 | com_assign(c, n, assigning) |
| 1284 | struct compiling *c; |
| 1285 | node *n; |
| 1286 | int assigning; |
| 1287 | { |
| 1288 | /* Loop to avoid trivial recursion */ |
| 1289 | for (;;) { |
| 1290 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1291 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1292 | case exprlist: |
| 1293 | case testlist: |
| 1294 | if (NCH(n) > 1) { |
| 1295 | com_assign_tuple(c, n, assigning); |
| 1296 | return; |
| 1297 | } |
| 1298 | n = CHILD(n, 0); |
| 1299 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1300 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1301 | case test: |
| 1302 | case and_test: |
| 1303 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1304 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1305 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1306 | case xor_expr: |
| 1307 | case and_expr: |
| 1308 | case shift_expr: |
| 1309 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1310 | case term: |
| 1311 | if (NCH(n) > 1) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1312 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1313 | "can't assign to operator"); |
| 1314 | c->c_errors++; |
| 1315 | return; |
| 1316 | } |
| 1317 | n = CHILD(n, 0); |
| 1318 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1319 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1320 | case factor: /* ('+'|'-'|'~') factor | atom trailer* */ |
| 1321 | if (TYPE(CHILD(n, 0)) != atom) { /* '+'|'-'|'~' */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1322 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1323 | "can't assign to operator"); |
| 1324 | c->c_errors++; |
| 1325 | return; |
| 1326 | } |
| 1327 | if (NCH(n) > 1) { /* trailer present */ |
| 1328 | int i; |
| 1329 | com_node(c, CHILD(n, 0)); |
| 1330 | for (i = 1; i+1 < NCH(n); i++) { |
| 1331 | com_apply_trailer(c, CHILD(n, i)); |
| 1332 | } /* NB i is still alive */ |
| 1333 | com_assign_trailer(c, |
| 1334 | CHILD(n, i), assigning); |
| 1335 | return; |
| 1336 | } |
| 1337 | n = CHILD(n, 0); |
| 1338 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1339 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1340 | case atom: |
| 1341 | switch (TYPE(CHILD(n, 0))) { |
| 1342 | case LPAR: |
| 1343 | n = CHILD(n, 1); |
| 1344 | if (TYPE(n) == RPAR) { |
| 1345 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1346 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1347 | "can't assign to ()"); |
| 1348 | c->c_errors++; |
| 1349 | return; |
| 1350 | } |
| 1351 | break; |
| 1352 | case LSQB: |
| 1353 | n = CHILD(n, 1); |
| 1354 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1355 | err_setstr(SyntaxError, |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1356 | "can't assign to []"); |
| 1357 | c->c_errors++; |
| 1358 | return; |
| 1359 | } |
| 1360 | com_assign_list(c, n, assigning); |
| 1361 | return; |
| 1362 | case NAME: |
| 1363 | com_assign_name(c, CHILD(n, 0), assigning); |
| 1364 | return; |
| 1365 | default: |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1366 | err_setstr(SyntaxError, |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 1367 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1368 | c->c_errors++; |
| 1369 | return; |
| 1370 | } |
| 1371 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1372 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1373 | default: |
| 1374 | fprintf(stderr, "node type %d\n", TYPE(n)); |
| 1375 | err_setstr(SystemError, "com_assign: bad node"); |
| 1376 | c->c_errors++; |
| 1377 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1378 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | static void |
| 1384 | com_expr_stmt(c, n) |
| 1385 | struct compiling *c; |
| 1386 | node *n; |
| 1387 | { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1388 | REQ(n, expr_stmt); /* testlist ('=' testlist)* */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1389 | com_node(c, CHILD(n, NCH(n)-1)); |
| 1390 | if (NCH(n) == 1) { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1391 | if (c->c_interactive) |
| 1392 | com_addbyte(c, PRINT_EXPR); |
| 1393 | else |
| 1394 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1395 | } |
| 1396 | else { |
| 1397 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1398 | for (i = 0; i < NCH(n)-2; i+=2) { |
| 1399 | if (i+2 < NCH(n)-2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1400 | com_addbyte(c, DUP_TOP); |
| 1401 | com_assign(c, CHILD(n, i), 1/*assign*/); |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | static void |
| 1407 | com_print_stmt(c, n) |
| 1408 | struct compiling *c; |
| 1409 | node *n; |
| 1410 | { |
| 1411 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1412 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
| 1413 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1414 | com_node(c, CHILD(n, i)); |
| 1415 | com_addbyte(c, PRINT_ITEM); |
| 1416 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1417 | if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1418 | com_addbyte(c, PRINT_NEWLINE); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1419 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | static void |
| 1423 | com_return_stmt(c, n) |
| 1424 | struct compiling *c; |
| 1425 | node *n; |
| 1426 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1427 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1428 | if (!c->c_infunction) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1429 | err_setstr(SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1430 | c->c_errors++; |
| 1431 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1432 | if (NCH(n) < 2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1433 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1434 | else |
| 1435 | com_node(c, CHILD(n, 1)); |
| 1436 | com_addbyte(c, RETURN_VALUE); |
| 1437 | } |
| 1438 | |
| 1439 | static void |
| 1440 | com_raise_stmt(c, n) |
| 1441 | struct compiling *c; |
| 1442 | node *n; |
| 1443 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1444 | REQ(n, raise_stmt); /* 'raise' test [',' test] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1445 | com_node(c, CHILD(n, 1)); |
| 1446 | if (NCH(n) > 3) |
| 1447 | com_node(c, CHILD(n, 3)); |
| 1448 | else |
| 1449 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1450 | com_addbyte(c, RAISE_EXCEPTION); |
| 1451 | } |
| 1452 | |
| 1453 | static void |
| 1454 | com_import_stmt(c, n) |
| 1455 | struct compiling *c; |
| 1456 | node *n; |
| 1457 | { |
| 1458 | int i; |
| 1459 | REQ(n, import_stmt); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1460 | /* 'import' NAME (',' NAME)* | |
| 1461 | 'from' NAME 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1462 | if (STR(CHILD(n, 0))[0] == 'f') { |
| 1463 | /* 'from' NAME 'import' ... */ |
| 1464 | REQ(CHILD(n, 1), NAME); |
| 1465 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
| 1466 | for (i = 3; i < NCH(n); i += 2) |
| 1467 | com_addopname(c, IMPORT_FROM, CHILD(n, i)); |
| 1468 | com_addbyte(c, POP_TOP); |
| 1469 | } |
| 1470 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1471 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1472 | for (i = 1; i < NCH(n); i += 2) { |
| 1473 | com_addopname(c, IMPORT_NAME, CHILD(n, i)); |
| 1474 | com_addopname(c, STORE_NAME, CHILD(n, i)); |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | static void |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1480 | com_global_stmt(c, n) |
| 1481 | struct compiling *c; |
| 1482 | node *n; |
| 1483 | { |
| 1484 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1485 | REQ(n, global_stmt); |
| 1486 | /* 'global' NAME (',' NAME)* */ |
| 1487 | for (i = 1; i < NCH(n); i += 2) { |
| 1488 | if (dictinsert(c->c_globals, STR(CHILD(n, i)), None) != 0) |
| 1489 | c->c_errors++; |
| 1490 | } |
| 1491 | } |
| 1492 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1493 | #define strequ(a, b) (strcmp((a), (b)) == 0) |
| 1494 | |
| 1495 | static void |
| 1496 | com_access_stmt(c, n) |
| 1497 | struct compiling *c; |
| 1498 | node *n; |
| 1499 | { |
| 1500 | int i, j, k, mode, imode; |
| 1501 | object *vmode; |
| 1502 | REQ(n, access_stmt); |
| 1503 | /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)* |
| 1504 | accesstype: NAME+ */ |
| 1505 | |
| 1506 | /* Find where the colon is */ |
| 1507 | i = 1; |
| 1508 | while (TYPE(CHILD(n,i-1)) != COLON) |
| 1509 | i += 1; |
| 1510 | |
| 1511 | /* Calculate the mode mask */ |
| 1512 | mode = 0; |
| 1513 | for (j = i; j < NCH(n); j += 2) { |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1514 | int r = 0, w = 0, p = 0; |
| 1515 | for (k = 0; k < NCH(CHILD(n,j)); k++) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1516 | if (strequ(STR(CHILD(CHILD(n,j),k)), "public")) |
| 1517 | p = 0; |
| 1518 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected")) |
| 1519 | p = 1; |
| 1520 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "private")) |
| 1521 | p = 2; |
| 1522 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "read")) |
| 1523 | r = 1; |
| 1524 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "write")) |
| 1525 | w = 1; |
| 1526 | else /* XXX should make this an exception */ |
| 1527 | fprintf(stderr, "bad access type %s\n", |
| 1528 | STR(CHILD(CHILD(n,j),k))); |
| 1529 | } |
| 1530 | if (r == 0 && w == 0) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1531 | r = w = 1; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1532 | if (p == 0) { |
| 1533 | if (r == 1) mode |= AC_R_PUBLIC; |
| 1534 | if (w == 1) mode |= AC_W_PUBLIC; |
| 1535 | } else if (p == 1) { |
| 1536 | if (r == 1) mode |= AC_R_PROTECTED; |
| 1537 | if (w == 1) mode |= AC_W_PROTECTED; |
| 1538 | } else { |
| 1539 | if (r == 1) mode |= AC_R_PRIVATE; |
| 1540 | if (w == 1) mode |= AC_W_PRIVATE; |
| 1541 | } |
| 1542 | } |
| 1543 | vmode = newintobject((long)mode); |
| 1544 | imode = com_addconst(c, vmode); |
| 1545 | XDECREF(vmode); |
| 1546 | for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) { |
| 1547 | com_addoparg(c, LOAD_CONST, imode); |
| 1548 | com_addopname(c, ACCESS_MODE, CHILD(n, i)); |
| 1549 | } |
| 1550 | } |
| 1551 | |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1552 | static void |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1553 | com_exec_stmt(c, n) |
| 1554 | struct compiling *c; |
| 1555 | node *n; |
| 1556 | { |
| 1557 | REQ(n, exec_stmt); |
| 1558 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 1559 | com_node(c, CHILD(n, 1)); |
| 1560 | if (NCH(n) >= 4) |
| 1561 | com_node(c, CHILD(n, 3)); |
| 1562 | else |
| 1563 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1564 | if (NCH(n) >= 6) |
| 1565 | com_node(c, CHILD(n, 5)); |
| 1566 | else |
| 1567 | com_addbyte(c, DUP_TOP); |
| 1568 | com_addbyte(c, EXEC_STMT); |
| 1569 | } |
| 1570 | |
| 1571 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1572 | com_if_stmt(c, n) |
| 1573 | struct compiling *c; |
| 1574 | node *n; |
| 1575 | { |
| 1576 | int i; |
| 1577 | int anchor = 0; |
| 1578 | REQ(n, if_stmt); |
| 1579 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 1580 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 1581 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1582 | node *ch = CHILD(n, i+1); |
| 1583 | if (i > 0) |
| 1584 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1585 | com_node(c, CHILD(n, i+1)); |
| 1586 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1587 | com_addbyte(c, POP_TOP); |
| 1588 | com_node(c, CHILD(n, i+3)); |
| 1589 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1590 | com_backpatch(c, a); |
| 1591 | com_addbyte(c, POP_TOP); |
| 1592 | } |
| 1593 | if (i+2 < NCH(n)) |
| 1594 | com_node(c, CHILD(n, i+2)); |
| 1595 | com_backpatch(c, anchor); |
| 1596 | } |
| 1597 | |
| 1598 | static void |
| 1599 | com_while_stmt(c, n) |
| 1600 | struct compiling *c; |
| 1601 | node *n; |
| 1602 | { |
| 1603 | int break_anchor = 0; |
| 1604 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1605 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1606 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 1607 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1608 | block_push(c, SETUP_LOOP); |
| 1609 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1610 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1611 | com_node(c, CHILD(n, 1)); |
| 1612 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1613 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1614 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1615 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1616 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1617 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1618 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1619 | com_backpatch(c, anchor); |
| 1620 | com_addbyte(c, POP_TOP); |
| 1621 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1622 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1623 | if (NCH(n) > 4) |
| 1624 | com_node(c, CHILD(n, 6)); |
| 1625 | com_backpatch(c, break_anchor); |
| 1626 | } |
| 1627 | |
| 1628 | static void |
| 1629 | com_for_stmt(c, n) |
| 1630 | struct compiling *c; |
| 1631 | node *n; |
| 1632 | { |
| 1633 | object *v; |
| 1634 | int break_anchor = 0; |
| 1635 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1636 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1637 | REQ(n, for_stmt); |
| 1638 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 1639 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1640 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1641 | com_node(c, CHILD(n, 3)); |
| 1642 | v = newintobject(0L); |
| 1643 | if (v == NULL) |
| 1644 | c->c_errors++; |
| 1645 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
| 1646 | XDECREF(v); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1647 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1648 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1649 | com_addfwref(c, FOR_LOOP, &anchor); |
| 1650 | com_assign(c, CHILD(n, 1), 1/*assigning*/); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1651 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1652 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1653 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1654 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1655 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1656 | com_backpatch(c, anchor); |
| 1657 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1658 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1659 | if (NCH(n) > 8) |
| 1660 | com_node(c, CHILD(n, 8)); |
| 1661 | com_backpatch(c, break_anchor); |
| 1662 | } |
| 1663 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1664 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1665 | |
| 1666 | SETUP_FINALLY L |
| 1667 | <code for S> |
| 1668 | POP_BLOCK |
| 1669 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1670 | L: <code for Sf> |
| 1671 | END_FINALLY |
| 1672 | |
| 1673 | The special instructions use the block stack. Each block |
| 1674 | stack entry contains the instruction that created it (here |
| 1675 | SETUP_FINALLY), the level of the value stack at the time the |
| 1676 | block stack entry was created, and a label (here L). |
| 1677 | |
| 1678 | SETUP_FINALLY: |
| 1679 | Pushes the current value stack level and the label |
| 1680 | onto the block stack. |
| 1681 | POP_BLOCK: |
| 1682 | Pops en entry from the block stack, and pops the value |
| 1683 | stack until its level is the same as indicated on the |
| 1684 | block stack. (The label is ignored.) |
| 1685 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1686 | Pops a variable number of entries from the *value* stack |
| 1687 | and re-raises the exception they specify. The number of |
| 1688 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1689 | |
| 1690 | The block stack is unwound when an exception is raised: |
| 1691 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 1692 | onto the value stack (and the exception condition is cleared), |
| 1693 | and the interpreter jumps to the label gotten from the block |
| 1694 | stack. |
| 1695 | |
| 1696 | Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1697 | (The contents of the value stack is shown in [], with the top |
| 1698 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 1699 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1700 | |
| 1701 | Value stack Label Instruction Argument |
| 1702 | [] SETUP_EXCEPT L1 |
| 1703 | [] <code for S> |
| 1704 | [] POP_BLOCK |
| 1705 | [] JUMP_FORWARD L0 |
| 1706 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1707 | [tb, val, exc] L1: DUP ) |
| 1708 | [tb, val, exc, exc] <evaluate E1> ) |
| 1709 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 1710 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 1711 | [tb, val, exc, 1] POP ) |
| 1712 | [tb, val, exc] POP |
| 1713 | [tb, val] <assign to V1> (or POP if no V1) |
| 1714 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1715 | [] <code for S1> |
| 1716 | JUMP_FORWARD L0 |
| 1717 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1718 | [tb, val, exc, 0] L2: POP |
| 1719 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1720 | .............................etc....................... |
| 1721 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1722 | [tb, val, exc, 0] Ln+1: POP |
| 1723 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1724 | |
| 1725 | [] L0: <next statement> |
| 1726 | |
| 1727 | Of course, parts are not generated if Vi or Ei is not present. |
| 1728 | */ |
| 1729 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1730 | static void |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1731 | com_try_except(c, n) |
| 1732 | struct compiling *c; |
| 1733 | node *n; |
| 1734 | { |
| 1735 | int except_anchor = 0; |
| 1736 | int end_anchor = 0; |
| 1737 | int else_anchor = 0; |
| 1738 | int i; |
| 1739 | node *ch; |
| 1740 | |
| 1741 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 1742 | block_push(c, SETUP_EXCEPT); |
| 1743 | com_node(c, CHILD(n, 2)); |
| 1744 | com_addbyte(c, POP_BLOCK); |
| 1745 | block_pop(c, SETUP_EXCEPT); |
| 1746 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 1747 | com_backpatch(c, except_anchor); |
| 1748 | for (i = 3; |
| 1749 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 1750 | i += 3) { |
| 1751 | /* except_clause: 'except' [expr [',' expr]] */ |
| 1752 | if (except_anchor == 0) { |
| 1753 | err_setstr(SyntaxError, |
| 1754 | "default 'except:' must be last"); |
| 1755 | c->c_errors++; |
| 1756 | break; |
| 1757 | } |
| 1758 | except_anchor = 0; |
| 1759 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 1760 | if (NCH(ch) > 1) { |
| 1761 | com_addbyte(c, DUP_TOP); |
| 1762 | com_node(c, CHILD(ch, 1)); |
| 1763 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
| 1764 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 1765 | com_addbyte(c, POP_TOP); |
| 1766 | } |
| 1767 | com_addbyte(c, POP_TOP); |
| 1768 | if (NCH(ch) > 3) |
| 1769 | com_assign(c, CHILD(ch, 3), 1/*assigning*/); |
| 1770 | else |
| 1771 | com_addbyte(c, POP_TOP); |
| 1772 | com_addbyte(c, POP_TOP); |
| 1773 | com_node(c, CHILD(n, i+2)); |
| 1774 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 1775 | if (except_anchor) { |
| 1776 | com_backpatch(c, except_anchor); |
| 1777 | com_addbyte(c, POP_TOP); |
| 1778 | } |
| 1779 | } |
| 1780 | com_addbyte(c, END_FINALLY); |
| 1781 | com_backpatch(c, else_anchor); |
| 1782 | if (i < NCH(n)) |
| 1783 | com_node(c, CHILD(n, i+2)); |
| 1784 | com_backpatch(c, end_anchor); |
| 1785 | } |
| 1786 | |
| 1787 | static void |
| 1788 | com_try_finally(c, n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1789 | struct compiling *c; |
| 1790 | node *n; |
| 1791 | { |
| 1792 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1793 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 1794 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1795 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 1796 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1797 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1798 | com_addbyte(c, POP_BLOCK); |
| 1799 | block_pop(c, SETUP_FINALLY); |
| 1800 | block_push(c, END_FINALLY); |
| 1801 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1802 | com_backpatch(c, finally_anchor); |
| 1803 | ch = CHILD(n, NCH(n)-1); |
| 1804 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 1805 | com_node(c, ch); |
| 1806 | com_addbyte(c, END_FINALLY); |
| 1807 | block_pop(c, END_FINALLY); |
| 1808 | } |
| 1809 | |
| 1810 | static void |
| 1811 | com_try_stmt(c, n) |
| 1812 | struct compiling *c; |
| 1813 | node *n; |
| 1814 | { |
| 1815 | REQ(n, try_stmt); |
| 1816 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 1817 | | 'try' ':' suite 'finally' ':' suite */ |
| 1818 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 1819 | com_try_finally(c, n); |
| 1820 | else |
| 1821 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | static void |
| 1825 | com_suite(c, n) |
| 1826 | struct compiling *c; |
| 1827 | node *n; |
| 1828 | { |
| 1829 | REQ(n, suite); |
| 1830 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 1831 | if (NCH(n) == 1) { |
| 1832 | com_node(c, CHILD(n, 0)); |
| 1833 | } |
| 1834 | else { |
| 1835 | int i; |
| 1836 | for (i = 0; i < NCH(n); i++) { |
| 1837 | node *ch = CHILD(n, i); |
| 1838 | if (TYPE(ch) == stmt) |
| 1839 | com_node(c, ch); |
| 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1844 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1845 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1846 | com_continue_stmt(c, n) |
| 1847 | struct compiling *c; |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1848 | node *n; /* Not used, but passed for consistency */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1849 | { |
| 1850 | int i = c->c_nblocks; |
| 1851 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 1852 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1853 | } |
| 1854 | else { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 1855 | err_setstr(SyntaxError, "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1856 | c->c_errors++; |
| 1857 | } |
| 1858 | /* XXX Could allow it inside a 'finally' clause |
| 1859 | XXX if we could pop the exception still on the stack */ |
| 1860 | } |
| 1861 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1862 | static int |
| 1863 | com_argdefs(c, n, argcount_return) |
| 1864 | struct compiling *c; |
| 1865 | node *n; |
| 1866 | int *argcount_return; |
| 1867 | { |
| 1868 | int i, nch, nargs, ndefs, star; |
| 1869 | if (TYPE(n) == lambdef) { |
| 1870 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 1871 | n = CHILD(n, 1); |
| 1872 | } |
| 1873 | else { |
| 1874 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 1875 | n = CHILD(n, 2); |
| 1876 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 1877 | n = CHILD(n, 1); |
| 1878 | } |
| 1879 | if (TYPE(n) != varargslist) |
| 1880 | return -1; |
| 1881 | /* varargslist: |
| 1882 | (fpdef ['=' test] ',')* '*' NAME | |
| 1883 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 1884 | nch = NCH(n); |
| 1885 | if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) { |
| 1886 | star = 1; |
| 1887 | nch -= 2; |
| 1888 | } |
| 1889 | else |
| 1890 | star = 0; |
| 1891 | nargs = 0; |
| 1892 | ndefs = 0; |
| 1893 | for (i = 0; i < nch; i++) { |
| 1894 | int t; |
| 1895 | nargs++; |
| 1896 | i++; |
| 1897 | if (i >= nch) |
| 1898 | break; |
| 1899 | t = TYPE(CHILD(n, i)); |
| 1900 | if (t == EQUAL) { |
| 1901 | i++; |
| 1902 | ndefs++; |
| 1903 | com_node(c, CHILD(n, i)); |
| 1904 | i++; |
| 1905 | if (i >= nch) |
| 1906 | break; |
| 1907 | t = TYPE(CHILD(n, i)); |
| 1908 | } |
| 1909 | else { |
| 1910 | /* Treat "(a=1, b)" as "(a=1, b=None)" */ |
| 1911 | if (ndefs) { |
| 1912 | com_addoparg(c, LOAD_CONST, |
| 1913 | com_addconst(c, None)); |
| 1914 | ndefs++; |
| 1915 | } |
| 1916 | } |
| 1917 | if (t != COMMA) |
| 1918 | break; |
| 1919 | } |
| 1920 | if (star) |
| 1921 | nargs ^= 0x4000; |
| 1922 | *argcount_return = nargs; |
| 1923 | if (ndefs > 0) |
| 1924 | com_addoparg(c, BUILD_TUPLE, ndefs); |
| 1925 | return ndefs; |
| 1926 | } |
| 1927 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1928 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1929 | com_funcdef(c, n) |
| 1930 | struct compiling *c; |
| 1931 | node *n; |
| 1932 | { |
| 1933 | object *v; |
| 1934 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 1935 | v = (object *)compile(n, c->c_filename); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1936 | if (v == NULL) |
| 1937 | c->c_errors++; |
| 1938 | else { |
| 1939 | int i = com_addconst(c, v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1940 | int argcount; |
| 1941 | int ndefs = com_argdefs(c, n, &argcount); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1942 | com_addoparg(c, LOAD_CONST, i); |
| 1943 | com_addbyte(c, BUILD_FUNCTION); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 1944 | if (ndefs > 0) |
| 1945 | com_addoparg(c, SET_FUNC_ARGS, argcount); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1946 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 1947 | DECREF(v); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | static void |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1952 | com_bases(c, n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1953 | struct compiling *c; |
| 1954 | node *n; |
| 1955 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1956 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1957 | REQ(n, testlist); |
| 1958 | /* testlist: test (',' test)* [','] */ |
| 1959 | for (i = 0; i < NCH(n); i += 2) |
| 1960 | com_node(c, CHILD(n, i)); |
| 1961 | com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2); |
| 1962 | } |
| 1963 | |
| 1964 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1965 | com_classdef(c, n) |
| 1966 | struct compiling *c; |
| 1967 | node *n; |
| 1968 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1969 | int i; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1970 | object *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1971 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1972 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
| 1973 | if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) { |
| 1974 | c->c_errors++; |
| 1975 | return; |
| 1976 | } |
| 1977 | /* Push the class name on the stack */ |
| 1978 | i = com_addconst(c, v); |
| 1979 | com_addoparg(c, LOAD_CONST, i); |
| 1980 | DECREF(v); |
| 1981 | /* Push the tuple of base classes on the stack */ |
| 1982 | if (TYPE(CHILD(n, 2)) != LPAR) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1983 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1984 | else |
| 1985 | com_bases(c, CHILD(n, 3)); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 1986 | v = (object *)compile(n, c->c_filename); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1987 | if (v == NULL) |
| 1988 | c->c_errors++; |
| 1989 | else { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1990 | i = com_addconst(c, v); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1991 | com_addoparg(c, LOAD_CONST, i); |
| 1992 | com_addbyte(c, BUILD_FUNCTION); |
| 1993 | com_addbyte(c, UNARY_CALL); |
| 1994 | com_addbyte(c, BUILD_CLASS); |
| 1995 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 1996 | DECREF(v); |
| 1997 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | static void |
| 2001 | com_node(c, n) |
| 2002 | struct compiling *c; |
| 2003 | node *n; |
| 2004 | { |
| 2005 | switch (TYPE(n)) { |
| 2006 | |
| 2007 | /* Definition nodes */ |
| 2008 | |
| 2009 | case funcdef: |
| 2010 | com_funcdef(c, n); |
| 2011 | break; |
| 2012 | case classdef: |
| 2013 | com_classdef(c, n); |
| 2014 | break; |
| 2015 | |
| 2016 | /* Trivial parse tree nodes */ |
| 2017 | |
| 2018 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2019 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2020 | case flow_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2021 | com_node(c, CHILD(n, 0)); |
| 2022 | break; |
| 2023 | |
| 2024 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2025 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 2026 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2027 | { |
| 2028 | int i; |
| 2029 | for (i = 0; i < NCH(n)-1; i += 2) |
| 2030 | com_node(c, CHILD(n, i)); |
| 2031 | } |
| 2032 | break; |
| 2033 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2034 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2035 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2036 | com_node(c, CHILD(n, 0)); |
| 2037 | break; |
| 2038 | |
| 2039 | /* Statement nodes */ |
| 2040 | |
| 2041 | case expr_stmt: |
| 2042 | com_expr_stmt(c, n); |
| 2043 | break; |
| 2044 | case print_stmt: |
| 2045 | com_print_stmt(c, n); |
| 2046 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2047 | case del_stmt: /* 'del' exprlist */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2048 | com_assign(c, CHILD(n, 1), 0/*delete*/); |
| 2049 | break; |
| 2050 | case pass_stmt: |
| 2051 | break; |
| 2052 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2053 | if (c->c_loops == 0) { |
Guido van Rossum | 598fd8c | 1992-01-26 18:12:03 +0000 | [diff] [blame] | 2054 | err_setstr(SyntaxError, "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2055 | c->c_errors++; |
| 2056 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2057 | com_addbyte(c, BREAK_LOOP); |
| 2058 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2059 | case continue_stmt: |
| 2060 | com_continue_stmt(c, n); |
| 2061 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2062 | case return_stmt: |
| 2063 | com_return_stmt(c, n); |
| 2064 | break; |
| 2065 | case raise_stmt: |
| 2066 | com_raise_stmt(c, n); |
| 2067 | break; |
| 2068 | case import_stmt: |
| 2069 | com_import_stmt(c, n); |
| 2070 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2071 | case global_stmt: |
| 2072 | com_global_stmt(c, n); |
| 2073 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2074 | case access_stmt: |
| 2075 | com_access_stmt(c, n); |
| 2076 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2077 | case exec_stmt: |
| 2078 | com_exec_stmt(c, n); |
| 2079 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2080 | case if_stmt: |
| 2081 | com_if_stmt(c, n); |
| 2082 | break; |
| 2083 | case while_stmt: |
| 2084 | com_while_stmt(c, n); |
| 2085 | break; |
| 2086 | case for_stmt: |
| 2087 | com_for_stmt(c, n); |
| 2088 | break; |
| 2089 | case try_stmt: |
| 2090 | com_try_stmt(c, n); |
| 2091 | break; |
| 2092 | case suite: |
| 2093 | com_suite(c, n); |
| 2094 | break; |
| 2095 | |
| 2096 | /* Expression nodes */ |
| 2097 | |
| 2098 | case testlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2099 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | case test: |
| 2102 | com_test(c, n); |
| 2103 | break; |
| 2104 | case and_test: |
| 2105 | com_and_test(c, n); |
| 2106 | break; |
| 2107 | case not_test: |
| 2108 | com_not_test(c, n); |
| 2109 | break; |
| 2110 | case comparison: |
| 2111 | com_comparison(c, n); |
| 2112 | break; |
| 2113 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2114 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | case expr: |
| 2117 | com_expr(c, n); |
| 2118 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2119 | case xor_expr: |
| 2120 | com_xor_expr(c, n); |
| 2121 | break; |
| 2122 | case and_expr: |
| 2123 | com_and_expr(c, n); |
| 2124 | break; |
| 2125 | case shift_expr: |
| 2126 | com_shift_expr(c, n); |
| 2127 | break; |
| 2128 | case arith_expr: |
| 2129 | com_arith_expr(c, n); |
| 2130 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2131 | case term: |
| 2132 | com_term(c, n); |
| 2133 | break; |
| 2134 | case factor: |
| 2135 | com_factor(c, n); |
| 2136 | break; |
| 2137 | case atom: |
| 2138 | com_atom(c, n); |
| 2139 | break; |
| 2140 | |
| 2141 | default: |
| 2142 | fprintf(stderr, "node type %d\n", TYPE(n)); |
| 2143 | err_setstr(SystemError, "com_node: unexpected node type"); |
| 2144 | c->c_errors++; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | static void com_fplist PROTO((struct compiling *, node *)); |
| 2149 | |
| 2150 | static void |
| 2151 | com_fpdef(c, n) |
| 2152 | struct compiling *c; |
| 2153 | node *n; |
| 2154 | { |
| 2155 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 2156 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 2157 | com_fplist(c, CHILD(n, 1)); |
| 2158 | else |
| 2159 | com_addopname(c, STORE_NAME, CHILD(n, 0)); |
| 2160 | } |
| 2161 | |
| 2162 | static void |
| 2163 | com_fplist(c, n) |
| 2164 | struct compiling *c; |
| 2165 | node *n; |
| 2166 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 2167 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2168 | if (NCH(n) == 1) { |
| 2169 | com_fpdef(c, CHILD(n, 0)); |
| 2170 | } |
| 2171 | else { |
| 2172 | int i; |
| 2173 | com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); |
| 2174 | for (i = 0; i < NCH(n); i += 2) |
| 2175 | com_fpdef(c, CHILD(n, i)); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | static void |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2180 | com_arglist(c, n) |
| 2181 | struct compiling *c; |
| 2182 | node *n; |
| 2183 | { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2184 | int nch, op, nargs, i, t; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2185 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 2186 | /* varargslist: |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2187 | (fpdef ['=' test] ',')* '*' NAME | |
| 2188 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 2189 | nch = NCH(n); |
| 2190 | if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) { |
| 2191 | op = UNPACK_VARARG; |
| 2192 | nch -= 2; |
| 2193 | } |
| 2194 | else |
| 2195 | op = UNPACK_ARG; |
| 2196 | nargs = 0; |
| 2197 | for (i = 0; i < nch; i++) { |
| 2198 | nargs++; |
| 2199 | i++; |
| 2200 | if (i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2201 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2202 | t = TYPE(CHILD(n, i)); |
| 2203 | if (t == EQUAL) { |
| 2204 | i += 2; |
| 2205 | if (i >= nch) |
| 2206 | break; |
| 2207 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2208 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2209 | if (t != COMMA) |
| 2210 | break; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2211 | } |
| 2212 | com_addoparg(c, op, nargs); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2213 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2214 | com_fpdef(c, CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2215 | i++; |
| 2216 | if (i >= nch) |
| 2217 | break; |
| 2218 | t = TYPE(CHILD(n, i)); |
| 2219 | if (t == EQUAL) { |
| 2220 | i += 2; |
| 2221 | if (i >= nch) |
| 2222 | break; |
| 2223 | t = TYPE(CHILD(n, i)); |
| 2224 | } |
| 2225 | if (t != COMMA) |
| 2226 | break; |
| 2227 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2228 | if (op == UNPACK_VARARG) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2229 | com_addopname(c, STORE_NAME, CHILD(n, nch+1)); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2233 | com_file_input(c, n) |
| 2234 | struct compiling *c; |
| 2235 | node *n; |
| 2236 | { |
| 2237 | int i; |
| 2238 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
| 2239 | for (i = 0; i < NCH(n); i++) { |
| 2240 | node *ch = CHILD(n, i); |
| 2241 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 2242 | com_node(c, ch); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | /* Top-level compile-node interface */ |
| 2247 | |
| 2248 | static void |
| 2249 | compile_funcdef(c, n) |
| 2250 | struct compiling *c; |
| 2251 | node *n; |
| 2252 | { |
| 2253 | node *ch; |
| 2254 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2255 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2256 | com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); /* Patched! */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2257 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 2258 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2259 | if (TYPE(ch) == RPAR) |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2260 | com_addoparg(c, UNPACK_ARG, 0); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2261 | else |
| 2262 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2263 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2264 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2265 | c->c_infunction = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2266 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2267 | com_addbyte(c, RETURN_VALUE); |
| 2268 | } |
| 2269 | |
| 2270 | static void |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2271 | compile_lambdef(c, n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2272 | struct compiling *c; |
| 2273 | node *n; |
| 2274 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2275 | node *ch; |
| 2276 | REQ(n, lambdef); /* lambdef: 'lambda' [parameters] ':' test */ |
| 2277 | c->c_name = NULL; |
| 2278 | |
| 2279 | ch = CHILD(n, 1); |
| 2280 | if (TYPE(ch) == COLON) { |
| 2281 | com_addoparg(c, UNPACK_ARG, 0); |
| 2282 | com_node(c, CHILD(n, 2)); |
| 2283 | } |
| 2284 | else { |
| 2285 | com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); |
| 2286 | com_arglist(c, ch); |
| 2287 | com_node(c, CHILD(n, 3)); |
| 2288 | } |
| 2289 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2290 | com_addbyte(c, RETURN_VALUE); |
| 2291 | } |
| 2292 | |
| 2293 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2294 | compile_node(c, n) |
| 2295 | struct compiling *c; |
| 2296 | node *n; |
| 2297 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2298 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2299 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2300 | switch (TYPE(n)) { |
| 2301 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2302 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2303 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2304 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2305 | n = CHILD(n, 0); |
| 2306 | if (TYPE(n) != NEWLINE) |
| 2307 | com_node(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2308 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2309 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2310 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2311 | break; |
| 2312 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2313 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2314 | com_file_input(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2315 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2316 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2317 | break; |
| 2318 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2319 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2320 | com_node(c, CHILD(n, 0)); |
| 2321 | com_addbyte(c, RETURN_VALUE); |
| 2322 | break; |
| 2323 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2324 | case lambdef: /* anonymous function definition */ |
| 2325 | compile_lambdef(c, n); |
| 2326 | break; |
| 2327 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2328 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2329 | compile_funcdef(c, n); |
| 2330 | break; |
| 2331 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2332 | case classdef: /* A class definition */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2333 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 2334 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2335 | com_node(c, CHILD(n, NCH(n)-1)); /* The suite */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2336 | com_addbyte(c, LOAD_LOCALS); |
| 2337 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2338 | break; |
| 2339 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2340 | default: |
| 2341 | fprintf(stderr, "node type %d\n", TYPE(n)); |
| 2342 | err_setstr(SystemError, "compile_node: unexpected node type"); |
| 2343 | c->c_errors++; |
| 2344 | } |
| 2345 | } |
| 2346 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2347 | /* Optimization for local variables in functions (and *only* functions). |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2348 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2349 | This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME |
| 2350 | instructions that refer to local variables with LOAD_FAST etc. |
| 2351 | The latter instructions are much faster because they don't need to |
| 2352 | look up the variable name in a dictionary. |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2353 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2354 | To find all local variables, we check all STORE_NAME, IMPORT_FROM and |
| 2355 | DELETE_NAME instructions. This yields all local variables, including |
| 2356 | arguments, function definitions, class definitions and import |
| 2357 | statements. |
| 2358 | |
| 2359 | All remaining LOAD_NAME instructions must refer to non-local (global |
| 2360 | or builtin) variables, so are replaced by LOAD_GLOBAL. |
| 2361 | |
| 2362 | There are two problems: 'from foo import *' and 'exec' may introduce |
| 2363 | local variables that we can't know while compiling. If this is the |
| 2364 | case, we don't optimize at all (this rarely happens, since exec is |
| 2365 | rare, & this form of import statement is mostly used at the module |
| 2366 | level). |
| 2367 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2368 | NB: this modifies the string object co->co_code! |
| 2369 | */ |
| 2370 | |
| 2371 | static void |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2372 | optimize(c) |
| 2373 | struct compiling *c; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2374 | { |
Guido van Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 2375 | unsigned char *next_instr, *cur_instr; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2376 | object *locals; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2377 | int nlocals; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2378 | int opcode; |
| 2379 | int oparg; |
| 2380 | object *name; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2381 | int fast_reserved; |
| 2382 | object *error_type, *error_value; |
| 2383 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2384 | #define NEXTOP() (*next_instr++) |
| 2385 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
| 2386 | #define GETITEM(v, i) (getlistitem((v), (i))) |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2387 | #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i))) |
| 2388 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2389 | locals = newdictobject(); |
| 2390 | if (locals == NULL) { |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2391 | c->c_errors++; |
| 2392 | return; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2393 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2394 | nlocals = 0; |
| 2395 | |
| 2396 | err_get(&error_type, &error_value); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2397 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2398 | next_instr = (unsigned char *) getstringvalue(c->c_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2399 | for (;;) { |
| 2400 | opcode = NEXTOP(); |
| 2401 | if (opcode == STOP_CODE) |
| 2402 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2403 | if (opcode == EXEC_STMT) |
| 2404 | goto end; /* Don't optimize if exec present */ |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2405 | if (HAS_ARG(opcode)) |
| 2406 | oparg = NEXTARG(); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2407 | if (opcode == STORE_NAME || opcode == DELETE_NAME || |
| 2408 | opcode == IMPORT_FROM) { |
| 2409 | object *v; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2410 | name = GETNAMEOBJ(oparg); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2411 | if (dict2lookup(locals, name) != NULL) |
| 2412 | continue; |
| 2413 | err_clear(); |
| 2414 | v = newintobject(nlocals); |
| 2415 | if (v == NULL) { |
| 2416 | c->c_errors++; |
| 2417 | goto err; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2418 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2419 | nlocals++; |
| 2420 | if (dict2insert(locals, name, v) != 0) { |
| 2421 | DECREF(v); |
| 2422 | c->c_errors++; |
| 2423 | goto err; |
| 2424 | } |
| 2425 | DECREF(v); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2426 | } |
| 2427 | } |
| 2428 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2429 | if (dictlookup(locals, "*") != NULL) { |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2430 | /* Don't optimize anything */ |
| 2431 | goto end; |
| 2432 | } |
| 2433 | |
| 2434 | next_instr = (unsigned char *) getstringvalue(c->c_code); |
| 2435 | fast_reserved = 0; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2436 | for (;;) { |
| 2437 | cur_instr = next_instr; |
| 2438 | opcode = NEXTOP(); |
| 2439 | if (opcode == STOP_CODE) |
| 2440 | break; |
| 2441 | if (HAS_ARG(opcode)) |
| 2442 | oparg = NEXTARG(); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2443 | if (opcode == RESERVE_FAST) { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2444 | int i; |
| 2445 | object *localmap = newtupleobject(nlocals); |
| 2446 | int pos; |
| 2447 | object *key, *value; |
| 2448 | if (localmap == NULL) { /* XXX mask error */ |
| 2449 | err_clear(); |
| 2450 | continue; |
| 2451 | } |
| 2452 | pos = 0; |
| 2453 | while (mappinggetnext(locals, &pos, &key, &value)) { |
| 2454 | int j; |
| 2455 | if (!is_intobject(value)) |
| 2456 | continue; |
| 2457 | j = getintvalue(value); |
| 2458 | if (0 <= j && j < nlocals) { |
| 2459 | INCREF(key); |
| 2460 | settupleitem(localmap, j, key); |
| 2461 | } |
| 2462 | } |
| 2463 | i = com_addconst(c, localmap); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2464 | cur_instr[1] = i & 0xff; |
| 2465 | cur_instr[2] = (i>>8) & 0xff; |
| 2466 | fast_reserved = 1; |
| 2467 | continue; |
| 2468 | } |
| 2469 | if (!fast_reserved) |
| 2470 | continue; |
| 2471 | if (opcode == LOAD_NAME || |
| 2472 | opcode == STORE_NAME || |
| 2473 | opcode == DELETE_NAME) { |
| 2474 | object *v; |
| 2475 | int i; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2476 | name = GETNAMEOBJ(oparg); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2477 | v = dict2lookup(locals, name); |
| 2478 | if (v == NULL) { |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2479 | err_clear(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame^] | 2480 | if (opcode == LOAD_NAME) |
| 2481 | cur_instr[0] = LOAD_GLOBAL; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2482 | continue; |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2483 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2484 | i = getintvalue(v); |
| 2485 | switch (opcode) { |
| 2486 | case LOAD_NAME: cur_instr[0] = LOAD_FAST; break; |
| 2487 | case STORE_NAME: cur_instr[0] = STORE_FAST; break; |
| 2488 | case DELETE_NAME: cur_instr[0] = DELETE_FAST; break; |
| 2489 | } |
| 2490 | cur_instr[1] = i & 0xff; |
| 2491 | cur_instr[2] = (i>>8) & 0xff; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2492 | } |
| 2493 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2494 | |
| 2495 | end: |
| 2496 | err_setval(error_type, error_value); |
| 2497 | err: |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2498 | DECREF(locals); |
| 2499 | } |
| 2500 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2501 | codeobject * |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2502 | compile(n, filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2503 | node *n; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2504 | char *filename; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2505 | { |
| 2506 | struct compiling sc; |
| 2507 | codeobject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2508 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2509 | return NULL; |
| 2510 | compile_node(&sc, n); |
| 2511 | com_done(&sc); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2512 | if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2513 | optimize(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2514 | co = NULL; |
| 2515 | if (sc.c_errors == 0) { |
| 2516 | object *v, *w; |
| 2517 | v = newstringobject(sc.c_filename); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2518 | if (sc.c_name) |
| 2519 | w = newstringobject(sc.c_name); |
| 2520 | else { |
| 2521 | INCREF(None); |
| 2522 | w = None; |
| 2523 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2524 | if (v != NULL && w != NULL) |
| 2525 | co = newcodeobject(sc.c_code, sc.c_consts, |
| 2526 | sc.c_names, v, w); |
| 2527 | XDECREF(v); |
| 2528 | XDECREF(w); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 2529 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2530 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2531 | return co; |
| 2532 | } |