blob: c7b5ddf58ef7e64c56c22412b2c6a4a3094d0699 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* 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
36static 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 Rossum088bc2a1992-01-14 18:32:11 +000041 {"f_lasti", T_INT, OFF(f_lasti)},
42 {"f_lineno", T_INT, OFF(f_lineno)},
Guido van Rossum3f5da241990-12-20 15:06:42 +000043 {NULL} /* Sentinel */
44};
45
46static object *
47frame_getattr(f, name)
48 frameobject *f;
49 char *name;
50{
51 return getmember((char *)f, frame_memberlist, name);
52}
53
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000054/* 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
75static frameobject *free_list = NULL;
76
Guido van Rossum3f5da241990-12-20 15:06:42 +000077static void
78frame_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 Rossuma9e7dc11992-10-18 18:53:57 +000085 f->f_back = free_list;
86 free_list = f;
Guido van Rossum3f5da241990-12-20 15:06:42 +000087}
88
89typeobject 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
106frameobject *
107newframeobject(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 Rossuma9e7dc11992-10-18 18:53:57 +0000124 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 Rossum3f5da241990-12-20 15:06:42 +0000135 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 Rossuma9e7dc11992-10-18 18:53:57 +0000145 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 Rossum3f5da241990-12-20 15:06:42 +0000155 f->f_iblock = 0;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000156 f->f_lasti = 0;
157 f->f_lineno = -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158 if (f->f_valuestack == NULL || f->f_blockstack == NULL) {
159 err_nomem();
160 DECREF(f);
161 f = NULL;
162 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000163 }
164 return f;
165}
166
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000167object **
168extend_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 Rossum3f5da241990-12-20 15:06:42 +0000184/* Block management */
185
186void
187setup_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
204block *
205pop_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}