Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 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[] = { |
| 37 | {"f_back", T_OBJECT, OFF(f_back)}, |
| 38 | {"f_code", T_OBJECT, OFF(f_code)}, |
| 39 | {"f_globals", T_OBJECT, OFF(f_globals)}, |
| 40 | {"f_locals", T_OBJECT, OFF(f_locals)}, |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 41 | {"f_owner", T_OBJECT, OFF(f_owner)}, |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 42 | /* {"f_fastlocals",T_OBJECT, OFF(f_fastlocals)}, /* XXX Unsafe */ |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 43 | {"f_localmap", T_OBJECT, OFF(f_localmap)}, |
Guido van Rossum | 088bc2a | 1992-01-14 18:32:11 +0000 | [diff] [blame] | 44 | {"f_lasti", T_INT, OFF(f_lasti)}, |
| 45 | {"f_lineno", T_INT, OFF(f_lineno)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | {NULL} /* Sentinel */ |
| 47 | }; |
| 48 | |
| 49 | static object * |
| 50 | frame_getattr(f, name) |
| 51 | frameobject *f; |
| 52 | char *name; |
| 53 | { |
| 54 | return getmember((char *)f, frame_memberlist, name); |
| 55 | } |
| 56 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 57 | /* Stack frames are allocated and deallocated at a considerable rate. |
| 58 | In an attempt to improve the speed of function calls, we maintain a |
| 59 | separate free list of stack frames (just like integers are |
| 60 | allocated in a special way -- see intobject.c). When a stack frame |
| 61 | is on the free list, only the following members have a meaning: |
| 62 | ob_type == &Frametype |
| 63 | f_back next item on free list, or NULL |
| 64 | f_nvalues size of f_valuestack |
| 65 | f_valuestack array of (f_nvalues+1) object pointers, or NULL |
| 66 | f_nblocks size of f_blockstack |
| 67 | f_blockstack array of (f_nblocks+1) blocks, or NULL |
| 68 | Note that the value and block stacks are preserved -- this can save |
| 69 | another malloc() call or two (and two free() calls as well!). |
| 70 | Also note that, unlike for integers, each frame object is a |
| 71 | malloc'ed object in its own right -- it is only the actual calls to |
| 72 | malloc() that we are trying to save here, not the administration. |
| 73 | After all, while a typical program may make millions of calls, a |
| 74 | call depth of more than 20 or 30 is probably already exceptional |
| 75 | unless the program contains run-away recursion. I hope. |
| 76 | */ |
| 77 | |
| 78 | static frameobject *free_list = NULL; |
| 79 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 80 | static void |
| 81 | frame_dealloc(f) |
| 82 | frameobject *f; |
| 83 | { |
| 84 | XDECREF(f->f_back); |
| 85 | XDECREF(f->f_code); |
| 86 | XDECREF(f->f_globals); |
| 87 | XDECREF(f->f_locals); |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 88 | XDECREF(f->f_owner); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 89 | XDECREF(f->f_fastlocals); |
| 90 | XDECREF(f->f_localmap); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 91 | f->f_back = free_list; |
| 92 | free_list = f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | typeobject Frametype = { |
| 96 | OB_HEAD_INIT(&Typetype) |
| 97 | 0, |
| 98 | "frame", |
| 99 | sizeof(frameobject), |
| 100 | 0, |
| 101 | frame_dealloc, /*tp_dealloc*/ |
| 102 | 0, /*tp_print*/ |
| 103 | frame_getattr, /*tp_getattr*/ |
| 104 | 0, /*tp_setattr*/ |
| 105 | 0, /*tp_compare*/ |
| 106 | 0, /*tp_repr*/ |
| 107 | 0, /*tp_as_number*/ |
| 108 | 0, /*tp_as_sequence*/ |
| 109 | 0, /*tp_as_mapping*/ |
| 110 | }; |
| 111 | |
| 112 | frameobject * |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 113 | newframeobject(back, code, globals, locals, owner, nvalues, nblocks) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 114 | frameobject *back; |
| 115 | codeobject *code; |
| 116 | object *globals; |
| 117 | object *locals; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 118 | object *owner; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 119 | int nvalues; |
| 120 | int nblocks; |
| 121 | { |
| 122 | frameobject *f; |
| 123 | if ((back != NULL && !is_frameobject(back)) || |
| 124 | code == NULL || !is_codeobject(code) || |
| 125 | globals == NULL || !is_dictobject(globals) || |
| 126 | locals == NULL || !is_dictobject(locals) || |
| 127 | nvalues < 0 || nblocks < 0) { |
| 128 | err_badcall(); |
| 129 | return NULL; |
| 130 | } |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 131 | if (free_list == NULL) { |
| 132 | f = NEWOBJ(frameobject, &Frametype); |
| 133 | f->f_nvalues = f->f_nblocks = 0; |
| 134 | f->f_valuestack = NULL; |
| 135 | f->f_blockstack = NULL; |
| 136 | } |
| 137 | else { |
| 138 | f = free_list; |
| 139 | free_list = free_list->f_back; |
| 140 | NEWREF(f); |
| 141 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 142 | if (f != NULL) { |
| 143 | if (back) |
| 144 | INCREF(back); |
| 145 | f->f_back = back; |
| 146 | INCREF(code); |
| 147 | f->f_code = code; |
| 148 | INCREF(globals); |
| 149 | f->f_globals = globals; |
| 150 | INCREF(locals); |
| 151 | f->f_locals = locals; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 152 | XINCREF(owner); |
| 153 | f->f_owner = owner; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 154 | f->f_fastlocals = NULL; |
| 155 | f->f_localmap = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 156 | if (nvalues > f->f_nvalues || f->f_valuestack == NULL) { |
| 157 | XDEL(f->f_valuestack); |
| 158 | f->f_valuestack = NEW(object *, nvalues+1); |
| 159 | f->f_nvalues = nvalues; |
| 160 | } |
| 161 | if (nblocks > f->f_nblocks || f->f_blockstack == NULL) { |
| 162 | XDEL(f->f_blockstack); |
| 163 | f->f_blockstack = NEW(block, nblocks+1); |
| 164 | f->f_nblocks = nblocks; |
| 165 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 166 | f->f_iblock = 0; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 167 | f->f_lasti = 0; |
| 168 | f->f_lineno = -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 169 | if (f->f_valuestack == NULL || f->f_blockstack == NULL) { |
| 170 | err_nomem(); |
| 171 | DECREF(f); |
| 172 | f = NULL; |
| 173 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 174 | } |
| 175 | return f; |
| 176 | } |
| 177 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 178 | object ** |
| 179 | extend_stack(f, level, incr) |
| 180 | frameobject *f; |
| 181 | int level; |
| 182 | int incr; |
| 183 | { |
| 184 | f->f_nvalues = level + incr + 10; |
| 185 | f->f_valuestack = |
| 186 | (object **) realloc((ANY *)f->f_valuestack, |
| 187 | sizeof(object *) * (f->f_nvalues + 1)); |
| 188 | if (f->f_valuestack == NULL) { |
| 189 | err_nomem(); |
| 190 | return NULL; |
| 191 | } |
| 192 | return f->f_valuestack + level; |
| 193 | } |
| 194 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 195 | /* Block management */ |
| 196 | |
| 197 | void |
| 198 | setup_block(f, type, handler, level) |
| 199 | frameobject *f; |
| 200 | int type; |
| 201 | int handler; |
| 202 | int level; |
| 203 | { |
| 204 | block *b; |
| 205 | if (f->f_iblock >= f->f_nblocks) { |
| 206 | fprintf(stderr, "XXX block stack overflow\n"); |
| 207 | abort(); |
| 208 | } |
| 209 | b = &f->f_blockstack[f->f_iblock++]; |
| 210 | b->b_type = type; |
| 211 | b->b_level = level; |
| 212 | b->b_handler = handler; |
| 213 | } |
| 214 | |
| 215 | block * |
| 216 | pop_block(f) |
| 217 | frameobject *f; |
| 218 | { |
| 219 | block *b; |
| 220 | if (f->f_iblock <= 0) { |
| 221 | fprintf(stderr, "XXX block stack underflow\n"); |
| 222 | abort(); |
| 223 | } |
| 224 | b = &f->f_blockstack[--f->f_iblock]; |
| 225 | return b; |
| 226 | } |