Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame^] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | /* Frame object implementation */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | |
| 29 | #include "compile.h" |
| 30 | #include "frameobject.h" |
| 31 | #include "opcode.h" |
| 32 | #include "structmember.h" |
| 33 | |
| 34 | #define OFF(x) offsetof(frameobject, x) |
| 35 | |
| 36 | static struct memberlist frame_memberlist[] = { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 37 | {"f_back", T_OBJECT, OFF(f_back), RO}, |
| 38 | {"f_code", T_OBJECT, OFF(f_code), RO}, |
| 39 | {"f_globals", T_OBJECT, OFF(f_globals), RO}, |
| 40 | {"f_locals", T_OBJECT, OFF(f_locals), RO}, |
| 41 | {"f_owner", T_OBJECT, OFF(f_owner), RO}, |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 42 | #if 0 |
| 43 | {"f_fastlocals",T_OBJECT, OFF(f_fastlocals),RO}, /* XXX Unsafe */ |
| 44 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 45 | {"f_localmap", T_OBJECT, OFF(f_localmap),RO}, |
| 46 | {"f_lasti", T_INT, OFF(f_lasti), RO}, |
| 47 | {"f_lineno", T_INT, OFF(f_lineno), RO}, |
| 48 | {"f_trace", T_OBJECT, OFF(f_trace)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | {NULL} /* Sentinel */ |
| 50 | }; |
| 51 | |
| 52 | static object * |
| 53 | frame_getattr(f, name) |
| 54 | frameobject *f; |
| 55 | char *name; |
| 56 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 57 | if (strcmp(name, "f_locals") == 0) |
| 58 | fast_2_locals(f); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | return getmember((char *)f, frame_memberlist, name); |
| 60 | } |
| 61 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 62 | static int |
| 63 | frame_setattr(f, name, value) |
| 64 | frameobject *f; |
| 65 | char *name; |
| 66 | object *value; |
| 67 | { |
| 68 | return setmember((char *)f, frame_memberlist, name, value); |
| 69 | } |
| 70 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 71 | /* Stack frames are allocated and deallocated at a considerable rate. |
| 72 | In an attempt to improve the speed of function calls, we maintain a |
| 73 | separate free list of stack frames (just like integers are |
| 74 | allocated in a special way -- see intobject.c). When a stack frame |
| 75 | is on the free list, only the following members have a meaning: |
| 76 | ob_type == &Frametype |
| 77 | f_back next item on free list, or NULL |
| 78 | f_nvalues size of f_valuestack |
| 79 | f_valuestack array of (f_nvalues+1) object pointers, or NULL |
| 80 | f_nblocks size of f_blockstack |
| 81 | f_blockstack array of (f_nblocks+1) blocks, or NULL |
| 82 | Note that the value and block stacks are preserved -- this can save |
| 83 | another malloc() call or two (and two free() calls as well!). |
| 84 | Also note that, unlike for integers, each frame object is a |
| 85 | malloc'ed object in its own right -- it is only the actual calls to |
| 86 | malloc() that we are trying to save here, not the administration. |
| 87 | After all, while a typical program may make millions of calls, a |
| 88 | call depth of more than 20 or 30 is probably already exceptional |
| 89 | unless the program contains run-away recursion. I hope. |
| 90 | */ |
| 91 | |
| 92 | static frameobject *free_list = NULL; |
| 93 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | static void |
| 95 | frame_dealloc(f) |
| 96 | frameobject *f; |
| 97 | { |
| 98 | XDECREF(f->f_back); |
| 99 | XDECREF(f->f_code); |
| 100 | XDECREF(f->f_globals); |
| 101 | XDECREF(f->f_locals); |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 102 | XDECREF(f->f_owner); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 103 | XDECREF(f->f_fastlocals); |
| 104 | XDECREF(f->f_localmap); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 105 | XDECREF(f->f_trace); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 106 | f->f_back = free_list; |
| 107 | free_list = f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | typeobject Frametype = { |
| 111 | OB_HEAD_INIT(&Typetype) |
| 112 | 0, |
| 113 | "frame", |
| 114 | sizeof(frameobject), |
| 115 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 116 | (destructor)frame_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 117 | 0, /*tp_print*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 118 | (getattrfunc)frame_getattr, /*tp_getattr*/ |
| 119 | (setattrfunc)frame_setattr, /*tp_setattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 120 | 0, /*tp_compare*/ |
| 121 | 0, /*tp_repr*/ |
| 122 | 0, /*tp_as_number*/ |
| 123 | 0, /*tp_as_sequence*/ |
| 124 | 0, /*tp_as_mapping*/ |
| 125 | }; |
| 126 | |
| 127 | frameobject * |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 128 | newframeobject(back, code, globals, locals, owner, nvalues, nblocks) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 129 | frameobject *back; |
| 130 | codeobject *code; |
| 131 | object *globals; |
| 132 | object *locals; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 133 | object *owner; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | int nvalues; |
| 135 | int nblocks; |
| 136 | { |
| 137 | frameobject *f; |
| 138 | if ((back != NULL && !is_frameobject(back)) || |
| 139 | code == NULL || !is_codeobject(code) || |
| 140 | globals == NULL || !is_dictobject(globals) || |
| 141 | locals == NULL || !is_dictobject(locals) || |
| 142 | nvalues < 0 || nblocks < 0) { |
| 143 | err_badcall(); |
| 144 | return NULL; |
| 145 | } |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 146 | if (free_list == NULL) { |
| 147 | f = NEWOBJ(frameobject, &Frametype); |
| 148 | f->f_nvalues = f->f_nblocks = 0; |
| 149 | f->f_valuestack = NULL; |
| 150 | f->f_blockstack = NULL; |
| 151 | } |
| 152 | else { |
| 153 | f = free_list; |
| 154 | free_list = free_list->f_back; |
Sjoerd Mullender | f64992e | 1993-08-03 15:11:36 +0000 | [diff] [blame] | 155 | f->ob_type = &Frametype; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 156 | NEWREF(f); |
| 157 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 158 | if (f != NULL) { |
| 159 | if (back) |
| 160 | INCREF(back); |
| 161 | f->f_back = back; |
| 162 | INCREF(code); |
| 163 | f->f_code = code; |
| 164 | INCREF(globals); |
| 165 | f->f_globals = globals; |
| 166 | INCREF(locals); |
| 167 | f->f_locals = locals; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 168 | XINCREF(owner); |
| 169 | f->f_owner = owner; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 170 | f->f_fastlocals = NULL; |
| 171 | f->f_localmap = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 172 | if (nvalues > f->f_nvalues || f->f_valuestack == NULL) { |
| 173 | XDEL(f->f_valuestack); |
| 174 | f->f_valuestack = NEW(object *, nvalues+1); |
| 175 | f->f_nvalues = nvalues; |
| 176 | } |
| 177 | if (nblocks > f->f_nblocks || f->f_blockstack == NULL) { |
| 178 | XDEL(f->f_blockstack); |
| 179 | f->f_blockstack = NEW(block, nblocks+1); |
| 180 | f->f_nblocks = nblocks; |
| 181 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 182 | f->f_iblock = 0; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 183 | f->f_lasti = 0; |
| 184 | f->f_lineno = -1; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 185 | f->f_trace = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 186 | if (f->f_valuestack == NULL || f->f_blockstack == NULL) { |
| 187 | err_nomem(); |
| 188 | DECREF(f); |
| 189 | f = NULL; |
| 190 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 191 | } |
| 192 | return f; |
| 193 | } |
| 194 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 195 | object ** |
| 196 | extend_stack(f, level, incr) |
| 197 | frameobject *f; |
| 198 | int level; |
| 199 | int incr; |
| 200 | { |
| 201 | f->f_nvalues = level + incr + 10; |
| 202 | f->f_valuestack = |
| 203 | (object **) realloc((ANY *)f->f_valuestack, |
| 204 | sizeof(object *) * (f->f_nvalues + 1)); |
| 205 | if (f->f_valuestack == NULL) { |
| 206 | err_nomem(); |
| 207 | return NULL; |
| 208 | } |
| 209 | return f->f_valuestack + level; |
| 210 | } |
| 211 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 212 | /* Block management */ |
| 213 | |
| 214 | void |
| 215 | setup_block(f, type, handler, level) |
| 216 | frameobject *f; |
| 217 | int type; |
| 218 | int handler; |
| 219 | int level; |
| 220 | { |
| 221 | block *b; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 222 | if (f->f_iblock >= f->f_nblocks) |
| 223 | fatal("XXX block stack overflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 224 | b = &f->f_blockstack[f->f_iblock++]; |
| 225 | b->b_type = type; |
| 226 | b->b_level = level; |
| 227 | b->b_handler = handler; |
| 228 | } |
| 229 | |
| 230 | block * |
| 231 | pop_block(f) |
| 232 | frameobject *f; |
| 233 | { |
| 234 | block *b; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 235 | if (f->f_iblock <= 0) |
| 236 | fatal("XXX block stack underflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 237 | b = &f->f_blockstack[--f->f_iblock]; |
| 238 | return b; |
| 239 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 240 | |
| 241 | /* Convert between "fast" version of locals and dictionary version */ |
| 242 | |
| 243 | void |
| 244 | fast_2_locals(f) |
| 245 | frameobject *f; |
| 246 | { |
| 247 | /* Merge f->f_fastlocals into f->f_locals */ |
| 248 | object *locals, *fast, *map; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 249 | object *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 250 | int j; |
| 251 | if (f == NULL) |
| 252 | return; |
| 253 | locals = f->f_locals; |
| 254 | fast = f->f_fastlocals; |
| 255 | map = f->f_localmap; |
| 256 | if (locals == NULL || fast == NULL || map == NULL) |
| 257 | return; |
| 258 | if (!is_dictobject(locals) || !is_listobject(fast) || |
| 259 | !is_tupleobject(map)) |
| 260 | return; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 261 | err_fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 262 | for (j = gettuplesize(map); --j >= 0; ) { |
| 263 | object *key = gettupleitem(map, j); |
| 264 | object *value = getlistitem(fast, j); |
| 265 | if (value == NULL) { |
| 266 | err_clear(); |
| 267 | if (dict2remove(locals, key) != 0) |
| 268 | err_clear(); |
| 269 | } |
| 270 | else { |
| 271 | if (dict2insert(locals, key, value) != 0) |
| 272 | err_clear(); |
| 273 | } |
| 274 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 275 | err_restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void |
| 279 | locals_2_fast(f, clear) |
| 280 | frameobject *f; |
| 281 | int clear; |
| 282 | { |
| 283 | /* Merge f->f_locals into f->f_fastlocals */ |
| 284 | object *locals, *fast, *map; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 285 | object *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 286 | int j; |
| 287 | if (f == NULL) |
| 288 | return; |
| 289 | locals = f->f_locals; |
| 290 | fast = f->f_fastlocals; |
| 291 | map = f->f_localmap; |
| 292 | if (locals == NULL || fast == NULL || map == NULL) |
| 293 | return; |
| 294 | if (!is_dictobject(locals) || !is_listobject(fast) || |
| 295 | !is_tupleobject(map)) |
| 296 | return; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 297 | err_fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 298 | for (j = gettuplesize(map); --j >= 0; ) { |
| 299 | object *key = gettupleitem(map, j); |
| 300 | object *value = dict2lookup(locals, key); |
| 301 | if (value == NULL) |
| 302 | err_clear(); |
| 303 | else |
| 304 | INCREF(value); |
| 305 | if (value != NULL || clear) |
| 306 | if (setlistitem(fast, j, value) != 0) |
| 307 | err_clear(); |
| 308 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 309 | err_restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 310 | } |