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 | 088bc2a | 1992-01-14 18:32:11 +0000 | [diff] [blame] | 41 | {"f_lasti", T_INT, OFF(f_lasti)}, |
| 42 | {"f_lineno", T_INT, OFF(f_lineno)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 43 | {NULL} /* Sentinel */ |
| 44 | }; |
| 45 | |
| 46 | static object * |
| 47 | frame_getattr(f, name) |
| 48 | frameobject *f; |
| 49 | char *name; |
| 50 | { |
| 51 | return getmember((char *)f, frame_memberlist, name); |
| 52 | } |
| 53 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 54 | /* Stack frames are allocated and deallocated at a considerable rate. |
| 55 | In an attempt to improve the speed of function calls, we maintain a |
| 56 | separate free list of stack frames (just like integers are |
| 57 | allocated in a special way -- see intobject.c). When a stack frame |
| 58 | is on the free list, only the following members have a meaning: |
| 59 | ob_type == &Frametype |
| 60 | f_back next item on free list, or NULL |
| 61 | f_nvalues size of f_valuestack |
| 62 | f_valuestack array of (f_nvalues+1) object pointers, or NULL |
| 63 | f_nblocks size of f_blockstack |
| 64 | f_blockstack array of (f_nblocks+1) blocks, or NULL |
| 65 | Note that the value and block stacks are preserved -- this can save |
| 66 | another malloc() call or two (and two free() calls as well!). |
| 67 | Also note that, unlike for integers, each frame object is a |
| 68 | malloc'ed object in its own right -- it is only the actual calls to |
| 69 | malloc() that we are trying to save here, not the administration. |
| 70 | After all, while a typical program may make millions of calls, a |
| 71 | call depth of more than 20 or 30 is probably already exceptional |
| 72 | unless the program contains run-away recursion. I hope. |
| 73 | */ |
| 74 | |
| 75 | static frameobject *free_list = NULL; |
| 76 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 77 | static void |
| 78 | frame_dealloc(f) |
| 79 | frameobject *f; |
| 80 | { |
| 81 | XDECREF(f->f_back); |
| 82 | XDECREF(f->f_code); |
| 83 | XDECREF(f->f_globals); |
| 84 | XDECREF(f->f_locals); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 85 | f->f_back = free_list; |
| 86 | free_list = f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | typeobject Frametype = { |
| 90 | OB_HEAD_INIT(&Typetype) |
| 91 | 0, |
| 92 | "frame", |
| 93 | sizeof(frameobject), |
| 94 | 0, |
| 95 | frame_dealloc, /*tp_dealloc*/ |
| 96 | 0, /*tp_print*/ |
| 97 | frame_getattr, /*tp_getattr*/ |
| 98 | 0, /*tp_setattr*/ |
| 99 | 0, /*tp_compare*/ |
| 100 | 0, /*tp_repr*/ |
| 101 | 0, /*tp_as_number*/ |
| 102 | 0, /*tp_as_sequence*/ |
| 103 | 0, /*tp_as_mapping*/ |
| 104 | }; |
| 105 | |
| 106 | frameobject * |
| 107 | newframeobject(back, code, globals, locals, nvalues, nblocks) |
| 108 | frameobject *back; |
| 109 | codeobject *code; |
| 110 | object *globals; |
| 111 | object *locals; |
| 112 | int nvalues; |
| 113 | int nblocks; |
| 114 | { |
| 115 | frameobject *f; |
| 116 | if ((back != NULL && !is_frameobject(back)) || |
| 117 | code == NULL || !is_codeobject(code) || |
| 118 | globals == NULL || !is_dictobject(globals) || |
| 119 | locals == NULL || !is_dictobject(locals) || |
| 120 | nvalues < 0 || nblocks < 0) { |
| 121 | err_badcall(); |
| 122 | return NULL; |
| 123 | } |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 124 | if (free_list == NULL) { |
| 125 | f = NEWOBJ(frameobject, &Frametype); |
| 126 | f->f_nvalues = f->f_nblocks = 0; |
| 127 | f->f_valuestack = NULL; |
| 128 | f->f_blockstack = NULL; |
| 129 | } |
| 130 | else { |
| 131 | f = free_list; |
| 132 | free_list = free_list->f_back; |
| 133 | NEWREF(f); |
| 134 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 135 | if (f != NULL) { |
| 136 | if (back) |
| 137 | INCREF(back); |
| 138 | f->f_back = back; |
| 139 | INCREF(code); |
| 140 | f->f_code = code; |
| 141 | INCREF(globals); |
| 142 | f->f_globals = globals; |
| 143 | INCREF(locals); |
| 144 | f->f_locals = locals; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 145 | if (nvalues > f->f_nvalues || f->f_valuestack == NULL) { |
| 146 | XDEL(f->f_valuestack); |
| 147 | f->f_valuestack = NEW(object *, nvalues+1); |
| 148 | f->f_nvalues = nvalues; |
| 149 | } |
| 150 | if (nblocks > f->f_nblocks || f->f_blockstack == NULL) { |
| 151 | XDEL(f->f_blockstack); |
| 152 | f->f_blockstack = NEW(block, nblocks+1); |
| 153 | f->f_nblocks = nblocks; |
| 154 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 155 | f->f_iblock = 0; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 156 | f->f_lasti = 0; |
| 157 | f->f_lineno = -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 158 | if (f->f_valuestack == NULL || f->f_blockstack == NULL) { |
| 159 | err_nomem(); |
| 160 | DECREF(f); |
| 161 | f = NULL; |
| 162 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 163 | } |
| 164 | return f; |
| 165 | } |
| 166 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 167 | object ** |
| 168 | extend_stack(f, level, incr) |
| 169 | frameobject *f; |
| 170 | int level; |
| 171 | int incr; |
| 172 | { |
| 173 | f->f_nvalues = level + incr + 10; |
| 174 | f->f_valuestack = |
| 175 | (object **) realloc((ANY *)f->f_valuestack, |
| 176 | sizeof(object *) * (f->f_nvalues + 1)); |
| 177 | if (f->f_valuestack == NULL) { |
| 178 | err_nomem(); |
| 179 | return NULL; |
| 180 | } |
| 181 | return f->f_valuestack + level; |
| 182 | } |
| 183 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 184 | /* Block management */ |
| 185 | |
| 186 | void |
| 187 | setup_block(f, type, handler, level) |
| 188 | frameobject *f; |
| 189 | int type; |
| 190 | int handler; |
| 191 | int level; |
| 192 | { |
| 193 | block *b; |
| 194 | if (f->f_iblock >= f->f_nblocks) { |
| 195 | fprintf(stderr, "XXX block stack overflow\n"); |
| 196 | abort(); |
| 197 | } |
| 198 | b = &f->f_blockstack[f->f_iblock++]; |
| 199 | b->b_type = type; |
| 200 | b->b_level = level; |
| 201 | b->b_handler = handler; |
| 202 | } |
| 203 | |
| 204 | block * |
| 205 | pop_block(f) |
| 206 | frameobject *f; |
| 207 | { |
| 208 | block *b; |
| 209 | if (f->f_iblock <= 0) { |
| 210 | fprintf(stderr, "XXX block stack underflow\n"); |
| 211 | abort(); |
| 212 | } |
| 213 | b = &f->f_blockstack[--f->f_iblock]; |
| 214 | return b; |
| 215 | } |