Debian Codegraph @

terps/tads/tads3/vmstack.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/VMSTACK.H,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmstack.h - VM stack manager
12 Function
13
14 Notes
15
16 Modified
17 10/28/98 MJRoberts - Creation
18 */
19
20 #ifndef VMSTACK_H
21 #define VMSTACK_H
22
23 #include "vmtype.h"
24 #include "vmerr.h"
25 #include "vmerrnum.h"
26
27
28 /* ------------------------------------------------------------------------ */
29 /*
30 * The stack pointer and a few other VM registers are critical to
31 * performance, since they're accessed so frequently. If we're compiling
32 * in VMGLOB_VARS or VMGLOB_STRUCT mode, pull out the key registers as
33 * global static variables; this results in smaller and faster code on most
34 * machines than when it's part of the CVmStack structure. Don't do this
35 * when compiling in other global modes, since the local implementation
36 * must have a reason to keep everything in parameters (probably a shared
37 * memory situation, such as threads or a shared library).
38 */
39 #if defined(VMGLOB_VARS) || defined(VMGLOB_STRUCT)
40 /* global variable mode - define registers as separate globals */
41 # define VM_IF_REGS_IN_STRUCT(decl)
42 # define VM_IF_REGS_IN_GLOBALS(decl) decl
43 # define VM_REG_ACCESS static
44 # define VM_REG_CONST
45 #else
46 /* structure mode - define registers as part of CVmStack/CVmRun */
47 # define VM_IF_REGS_IN_STRUCT(decl) decl
48 # define VM_IF_REGS_IN_GLOBALS(decl)
49 # define VM_REG_ACCESS
50 # define VM_REG_CONST const
51 #endif
52
53 /* declare the stack pointer register as an extern global, if appropriate */
54 VM_IF_REGS_IN_GLOBALS(extern vm_val_t *sp_;)
55
56
57 /* ------------------------------------------------------------------------ */
58 /*
59 * VM stack interface
60 */
61
62 class CVmStack
63 {
64 public:
65 /*
66 * Allocate the stack. The maximum depth that the stack can achieve
67 * is fixed when the stack is created.
68 */
69 CVmStack(size_t max_depth, size_t reserve_depth);
70
71 /* initialize */
72 void init()
73 {
74 /* start the stack pointer at the first element */
75 sp_ = arr_;
76 }
77
78 /* delete the stack */
79 ~CVmStack();
80
81 /*
82 * get the current stack depth - this gives the number of active
83 * elements in the stack
84 */
85 size_t get_depth() const { return sp_ - arr_; }
86
87 /*
88 * Translate between pointer and index values. An index value is
89 * simply an integer giving the index in the stack of the given
90 * pointer; this value can be used, for example, for saving a stack
91 * location persistently.
92 *
93 * We return zero for a null stack pointer; we always return
94 * non-zero for a non-null stack pointer.
95 */
96 ulong ptr_to_index(vm_val_t *p) const
97 {
98 /* if it's null, return index 0; otherwise, return a non-zero index */
99 return (p == 0 ? 0 : p - arr_ + 1);
100 }
101 vm_val_t *index_to_ptr(ulong idx) const
102 {
103 /* if the index is zero, it's null; otherwise, get the pointer */
104 return (idx == 0 ? 0 : arr_ + idx - 1);
105 }
106
107 /*
108 * Get the depth relative to a given frame pointer. This returns
109 * the number of items on the stack beyond the given pointer. If
110 * the given frame pointer is beyond the current stack pointer
111 * (i.e., values have been popped since the frame pointer equalled
112 * the stack pointer), the return value will be negative.
113 */
114 VM_REG_ACCESS int get_depth_rel(vm_val_t *fp) VM_REG_CONST
115 { return sp_ - fp; }
116
117 /* get the current stack pointer */
118 VM_REG_ACCESS vm_val_t *get_sp() VM_REG_CONST
119 { return sp_; }
120
121 /*
122 * Set the current stack pointer. The pointer must always be a
123 * value previously returned by get_sp().
124 */
125 VM_REG_ACCESS void set_sp(vm_val_t *p) { sp_ = p; }
126
127 /*
128 * Get an element relative to a frame pointer (a frame pointer is a
129 * stack position that was previously obtained via get_sp() and
130 * stored by the caller). The offset is negative for a value pushed
131 * prior to the frame pointer, zero for the value at the frame
132 * pointer, or positive for values pushed after the frame pointer.
133 */
134 static vm_val_t *get_from_frame(vm_val_t *fp, int i)
135 { return (fp + i - 1); }
136
137 /* push a value */
138 VM_REG_ACCESS void push(const vm_val_t *val) { *sp_++ = *val; }
139
140 /*
141 * Push an element, returning a pointer to the element; this can be
142 * used to fill in a new stack element directly, without copying a
143 * value. The new element is not filled in yet on return, so the
144 * caller should immediately fill in the element with a valid value.
145 */
146 VM_REG_ACCESS vm_val_t *push() { return sp_++; }
147
148 /*
149 * Push a number of elements: this allocates a block of contiguous
150 * stack elements that the caller can fill in individually. The stack
151 * elements are uninitialized, so the caller must set the values
152 * immediately on return. A pointer to the first pushed element is
153 * returned; subsequent elements are addressed at the return value
154 * plus 1, plus 2, and so on.
155 */
156 VM_REG_ACCESS vm_val_t *push(unsigned int n)
157 {
158 /* remember the current stack pointer, which is what we return */
159 vm_val_t *ret = sp_;
160
161 /* allocate the elements */
162 sp_ += n;
163
164 /* return the base of the allocated block */
165 return ret;
166 }
167
168 /* push, checking space */
169 void push_check(const vm_val_t *val) { *push_check() = *val; }
170 vm_val_t *push_check() { check_throw(1); return push(); }
171 vm_val_t *push_check(unsigned int n) { check_throw(n); return push(n); }
172
173 /*
174 * Insert space for 'num' slots at index 'idx'. If 'idx' is zero, this
175 * is the same as pushing 'num' slots. Returns a pointer to the first
176 * slot allocated.
177 */
178 vm_val_t *insert(size_t idx, size_t num)
179 {
180 /* make sure there's room */
181 check_space(num);
182
183 /* add the space */
184 sp_ += num;
185
186 /* if idx is non-zero, move the idx slots by num to make room */
187 if (idx != 0)
188 memmove(sp_ - idx, sp_ - idx - num, idx*sizeof(*sp_));
189
190 /* return the start of the inserted block */
191 return sp_ - idx - num;
192 }
193
194 /*
195 * Get an element. Elements are numbered from zero to (depth - 1).
196 * Element number zero is the item most recently pushed onto the
197 * stack; element (depth-1) is the oldest element on the stack.
198 */
199 VM_REG_ACCESS vm_val_t *get(size_t i) VM_REG_CONST
200 { return (sp_ - i - 1); }
201
202 /* pop the top element off the stack */
203 VM_REG_ACCESS void pop(vm_val_t *val) { *val = *--sp_; }
204
205 /* discard the top element */
206 VM_REG_ACCESS void discard() { --sp_; }
207
208 /* discard a given number of elements from the top of the stack */
209 VM_REG_ACCESS void discard(int n) { sp_ -= n; }
210
211 /*
212 * Probe the stack for a given allocation. Returns true if the given
213 * number of slots are available, false if not. Does NOT actually
214 * allocate the space; merely checks for availability.
215 *
216 * Compilers are expected to produce function headers that check for
217 * the maximum amount of stack space needed locally in the function on
218 * entry, which allows us to check once at the start of the function
219 * for available stack space, relieving us of the need to check for
220 * available space in every push operation.
221 *
222 * Returns true if the required amount of space is available, false if
223 * not.
224 *
225 * (NB: 'slots' really should be a size_t, as it's not meaningful to
226 * reserve negative space. However, a compiler bug in 3.0.17,
227 * 3.0.17.1, and 3.0.18 caused the compiler to generate the wrong
228 * reservation sizes in headers; affected code occasionally makes
229 * OPC_MAKELSTPAR compute a negative value for its size request here.
230 * Making 'slots' signed makes the result of (get_depth() + <negative>)
231 * less than the current depth, as it should be, whereas a size_t type
232 * makes it yield a huge positive value on some systems, which looks
233 * like a stack overflow. These faulty .t3 files are generally benign
234 * despite the stack size miscalculation, because they're usually not
235 * so far off that we'd actually blow past the real memory limits of
236 * the stack, thanks to the exception-handling reserve. In other
237 * words, we'd usually catch an actual stack overflow in a faulty .t3
238 * before it crashed the interpreter. So it's desirable to be
239 * compatible with such files.)
240 */
241 int check_space(int nslots) const
242 { return (get_depth() + nslots <= max_depth_); }
243
244 /* check space for 'nslots' new slots, throwing an error on overflow */
245 void check_throw(int nslots) const
246 {
247 if (!check_space(nslots))
248 err_throw(VMERR_STACK_OVERFLOW);
249 }
250
251 /*
252 * Release the reserve. Debuggers can use this to allow manual
253 * recovery from stack overflows, by making some extra stack
254 * temporarily available for the debugger's use in handling the
255 * overflow. This releases the reserve, if available, that was
256 * specified when the stack was allocated. Returns true if reserve
257 * space is available for release, false if not.
258 */
259 int release_reserve()
260 {
261 /* if the reserve is already in use, we can't release it again */
262 if (reserve_in_use_)
263 return FALSE;
264
265 /* add the reserve space to the maximum stack depth */
266 max_depth_ += reserve_depth_;
267
268 /* note that the reserve has been released */
269 reserve_in_use_ = TRUE;
270
271 /* indicate that we successfully released the reserve */
272 return TRUE;
273 }
274
275 /*
276 * Recover the reserve. If the debugger releases the reserve to handle
277 * a stack overflow, it can call this once the situation has been dealt
278 * with to take the reserve back out of play, so that the debugger can
279 * deal with any future overflow in the same manner.
280 */
281 void recover_reserve()
282 {
283 /* if the reserve is in use, put it back in reserve */
284 if (reserve_in_use_)
285 {
286 /* remove the reserve from the stack */
287 max_depth_ -= reserve_depth_;
288
289 /* mark the reserve as available again */
290 reserve_in_use_ = FALSE;
291 }
292 }
293
294
295 private:
296 /* the array of value holders making up the stack */
297 vm_val_t *arr_;
298
299 /*
300 * Next available stack slot - the stack pointer starts out pointing at
301 * arr_[0], and is incremented after storing each element. This is
302 * defined as a member variable only if we're not defining it as a
303 * separate static global.
304 */
305 VM_IF_REGS_IN_STRUCT(vm_val_t *sp_;)
306
307 /* maximum depth that the stack is capable of holding */
308 size_t max_depth_;
309
310 /* extra reserve space */
311 size_t reserve_depth_;
312
313 /* flag: the reserve has been released for VM use */
314 int reserve_in_use_;
315 };
316
317 #endif /* VMSTACK_H */
318