Debian Codegraph @

terps/tads/tads3/vmfref.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2000, 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 vmfref.h - stack frame reference object
12 Function
13 There are two objects comprising a stack frame reference:
14
15 - A StackFrameDesc object. This object is visible to the bytecode
16 program and provides the API. This object records a particular return
17 location in a function; it stores a return address, scope index, and a
18 pointer to a StackFrameRef object.
19
20 - The StackFrameRef object. This object is a reference to an activation
21 frame. It records the live stack frame pointer and entry address, and
22 stores a snapshot of the local variables, parameters, and method context
23 variables after the frame terminates. This is an internal object with no
24 public API; its interface is used only by StackFrameDesc.
25
26 The reason we need two objects is that a given activation frame can have
27 many execution points over the course of its lifetime. Each execution
28 point has to be recorded separately, since a single function can have
29 multiple local variable scopes with different symbol bindings.
30 Notes
31
32 Modified
33 04/22/00 MJRoberts - Creation
34 */
35
36 #ifndef VMFREF_H
37 #define VMFREF_H
38
39 #include <stdlib.h>
40 #include "vmtype.h"
41 #include "vmobj.h"
42 #include "vmglob.h"
43
44 /* ------------------------------------------------------------------------ */
45 /*
46 * Extension for the frame descriptor object
47 */
48 struct vm_framedesc_ext
49 {
50 /* the underlying StackFrameRef object */
51 vm_obj_id_t fref;
52
53 /* frame index in the debug table for the method */
54 int frame_idx;
55
56 /* the return offset */
57 uint ret_ofs;
58 };
59
60 /*
61 * Extension for stack frame reference object
62 */
63 struct vm_frameref_ext
64 {
65 /*
66 * Stack frame pointer. The VM automatically sets this to null when
67 * the frame exits. This is also set to null when we restore a frame
68 * from a saved state file, since stack frames are inherently transient
69 * and thus show up as null references in a save file.
70 */
71 vm_val_t *fp;
72
73 /* method entry pointer value */
74 vm_val_t entry;
75
76 /* resolved pointer to the method entry byte code */
77 const uchar *entryp;
78
79 /* number of local variables and parameters */
80 int nlocals;
81 int nparams;
82
83 /*
84 * Snapshot copy of the method context variables. We copy these values
85 * at the same time we copy the local variables when detaching the
86 * frame (see below).
87 */
88 vm_val_t self;
89 vm_obj_id_t defobj;
90 vm_obj_id_t targobj;
91 vm_prop_id_t targprop;
92 vm_val_t invokee;
93
94 /*
95 * Snapshot copy of the locals and parameters. Just before the
96 * associated routine returns to its caller, rendering the true stack
97 * frame invalid, we make a private copy of the locals and parameters
98 * from the frame here. This allows callers to access the variable
99 * values after the frame becomes inactive, as long as 'self' continues
100 * to be reachable. This effectively detaches the frame from the
101 * stack, analogously to the way "context locals" for anonymous
102 * functions work. The difference from anonymous functions is that our
103 * mechanism here can detach any frame on the fly, without the compiler
104 * having to make special arrangements in advance.
105 *
106 * We overallocate the structure to make room for nparams + nlocals
107 * slots. The locals come first, followed by the parameters.
108 */
109 vm_val_t vars[1];
110 };
111
112
113 /* ------------------------------------------------------------------------ */
114 /*
115 * Stack frame descriptor object
116 */
117 class CVmObjFrameDesc: public CVmObject
118 {
119 friend class CVmMetaclassFrameDesc;
120
121 public:
122 /* metaclass registration object */
123 static class CVmMetaclass *metaclass_reg_;
124 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
125
126 /* am I of the given metaclass? */
127 virtual int is_of_metaclass(class CVmMetaclass *meta) const
128 {
129 /* try my own metaclass and my base class */
130 return (meta == metaclass_reg_
131 || CVmObject::is_of_metaclass(meta));
132 }
133
134 /* is 'obj' a frame descriptor object? */
135 static int is_framedesc_obj(VMG_ vm_obj_id_t obj)
136 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
137
138 /* get my StackFrameRef object */
139 vm_obj_id_t get_frame_ref_id() const { return get_ext()->fref; }
140 class CVmObjFrameRef *get_frame_ref(VMG0_) const
141 { return (class CVmObjFrameRef *)vm_objp(vmg_ get_frame_ref_id()); }
142
143 /* find a local in our frame */
144 int find_local(VMG_ const vm_val_t *nval, class CVmDbgFrameSymPtr *symp);
145 int find_local(VMG_ const textchar_t *sym, size_t len,
146 class CVmDbgFrameSymPtr *symp);
147
148 /* get the value of a local by name */
149 int get_local_val(VMG_ vm_val_t *result, const vm_val_t *name);
150
151 /* set the value of a local by name */
152 int set_local_val(VMG_ const vm_val_t *name, const vm_val_t *new_val);
153
154 /* create */
155 static vm_obj_id_t create(VMG_ vm_obj_id_t fref, int frame_idx,
156 uint ret_ofs);
157
158 /*
159 * call a static property - we don't have any of our own, so simply
160 * "inherit" the base class handling
161 */
162 static int call_stat_prop(VMG_ vm_val_t *result,
163 const uchar **pc_ptr, uint *argc,
164 vm_prop_id_t prop)
165 { return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); }
166
167 /* set a property */
168 void set_prop(VMG_ class CVmUndo *,
169 vm_obj_id_t, vm_prop_id_t, const vm_val_t *)
170 {
171 /* cannot set frame reference properties */
172 err_throw(VMERR_INVALID_SETPROP);
173 }
174
175 /* get a property */
176 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
177 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
178
179
180 /* index the frame: this looks up a variable's value by name */
181 virtual int index_val_q(VMG_ vm_val_t *result,
182 vm_obj_id_t self,
183 const vm_val_t *index_val);
184
185 /* assign an indexed value: this sets a variable's value by name */
186 virtual int set_index_val_q(VMG_ vm_val_t *new_container,
187 vm_obj_id_t self,
188 const vm_val_t *index_val,
189 const vm_val_t *new_val);
190
191 /* notify of deletion */
192 void notify_delete(VMG_ int in_root_set);
193
194 /* undo methods - we don't participate in undo, so these are no-ops */
195 void notify_new_savept() { }
196 void apply_undo(VMG_ struct CVmUndoRecord *) { }
197
198 /* mark references */
199 void mark_refs(VMG_ uint);
200
201 /* we're immutable, so we don't have to worry about undo */
202 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
203
204 /* we don't have any weak references */
205 void remove_stale_weak_refs(VMG0_) { }
206 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
207
208 /* image file operations */
209 void load_from_image(VMG_ vm_obj_id_t, const char *, size_t);
210 void reload_from_image(VMG_ vm_obj_id_t, const char *, size_t);
211 ulong rebuild_image(VMG_ char *, ulong);
212
213 /* we're not loadable and we don't change */
214 int is_changed_since_load() const { return FALSE; }
215
216 /* save/restore */
217 void save_to_file(VMG_ class CVmFile *fp);
218 void restore_from_file(VMG_ vm_obj_id_t self, class CVmFile *fp,
219 class CVmObjFixup *fixups);
220
221 protected:
222 /* construction */
223 CVmObjFrameDesc() { ext_ = 0; }
224 CVmObjFrameDesc(VMG_ vm_obj_id_t fref, int frame_idx, uint ret_ofs);
225
226 /* allocate our extension */
227 vm_framedesc_ext *alloc_ext(VMG_ vm_obj_id_t fref,
228 int frame_idx, uint ret_ofs);
229
230 /* get my extension, properly cast */
231 vm_framedesc_ext *get_ext() const { return (vm_framedesc_ext *)ext_; }
232
233 /* load the image data */
234 void load_image_data(VMG_ vm_obj_id_t self,
235 const char *ptr, size_t siz);
236
237 /* property evaluator - undefined property */
238 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
239
240 /* property evaluator - is this frame still activie? */
241 virtual int getp_is_active(VMG_ vm_obj_id_t self, vm_val_t *retval,
242 uint *argc);
243
244 /* property evaluator - get 'self' in the frame */
245 virtual int getp_get_self(VMG_ vm_obj_id_t self, vm_val_t *retval,
246 uint *argc);
247
248 /* property evaluator - get 'definingobj' in the frame */
249 virtual int getp_get_defobj(VMG_ vm_obj_id_t self, vm_val_t *retval,
250 uint *argc);
251
252 /* property evaluator - get 'targetobj' in the frame */
253 virtual int getp_get_targobj(VMG_ vm_obj_id_t self, vm_val_t *retval,
254 uint *argc);
255
256 /* property evaluator - get 'targetprop' in the frame */
257 virtual int getp_get_targprop(VMG_ vm_obj_id_t self, vm_val_t *retval,
258 uint *argc);
259
260 /* property evaluator - get a lookup table of the variables */
261 virtual int getp_get_vars(VMG_ vm_obj_id_t self, vm_val_t *retval,
262 uint *argc);
263
264 /* property evaluator - get 'invokee' in the frame */
265 virtual int getp_get_invokee(VMG_ vm_obj_id_t self, vm_val_t *retval,
266 uint *argc);
267
268 /* function table */
269 static int (CVmObjFrameDesc::*func_table_[])
270 (VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
271 };
272
273 /* ------------------------------------------------------------------------ */
274 /*
275 * Stack frame reference object
276 */
277 class CVmObjFrameRef: public CVmObject
278 {
279 friend class CVmMetaclassFrameRef;
280 friend class CVmObjFrameDesc;
281
282 public:
283 /* metaclass registration object */
284 static class CVmMetaclass *metaclass_reg_;
285 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
286
287 /* am I of the given metaclass? */
288 virtual int is_of_metaclass(class CVmMetaclass *meta) const
289 {
290 /* try my own metaclass and my base class */
291 return (meta == metaclass_reg_
292 || CVmObject::is_of_metaclass(meta));
293 }
294
295 /* is 'obj' a frame reference object? */
296 static int is_frameref_obj(VMG_ vm_obj_id_t obj)
297 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
298
299 /* create */
300 static vm_obj_id_t create(VMG_ vm_val_t *fp, const uchar *entry);
301
302 /*
303 * Invalidate the frame reference. The bytecode interpreter calls this
304 * when exiting a frame by 'return', 'throw', etc.
305 */
306 void inval_frame(VMG0_);
307
308 /* is the frame active? */
309 int is_active() { return get_ext()->fp != 0; }
310
311 /* get 'self' from the frame */
312 void get_self(VMG_ vm_val_t *result);
313
314 /* get 'definingobj' from the frame */
315 void get_defobj(VMG_ vm_val_t *result);
316
317 /* get 'targetobj' from the frame */
318 void get_targobj(VMG_ vm_val_t *result);
319
320 /* get 'targetprop' from the frame */
321 void get_targprop(VMG_ vm_val_t *result);
322
323 /* get 'invokee' from the frame */
324 void get_invokee(VMG_ vm_val_t *result);
325
326 /* create a method context object for the LOADCTX instruction */
327 void create_loadctx_obj(VMG_ vm_val_t *result);
328
329 /* get the value of a local given its frame descriptor */
330 void get_local_val(VMG_ vm_val_t *result,
331 const class CVmDbgFrameSymPtr *sym);
332
333 /* set the value of a local given its frame descriptor */
334 void set_local_val(VMG_ const CVmDbgFrameSymPtr *sym,
335 const vm_val_t *new_val);
336
337 /* get the integer frame index for a variable given its descriptor */
338 int get_var_frame_index(const CVmDbgFrameSymPtr *symp);
339
340 /*
341 * call a static property - we don't have any of our own, so simply
342 * "inherit" the base class handling
343 */
344 static int call_stat_prop(VMG_ vm_val_t *result,
345 const uchar **pc_ptr, uint *argc,
346 vm_prop_id_t prop)
347 { return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); }
348
349 /* set a property */
350 void set_prop(VMG_ class CVmUndo *,
351 vm_obj_id_t, vm_prop_id_t, const vm_val_t *)
352 {
353 /* cannot set frame reference properties */
354 err_throw(VMERR_INVALID_SETPROP);
355 }
356
357 /* index the frame: this looks up a variable by frame index */
358 virtual int index_val_q(VMG_ vm_val_t *result,
359 vm_obj_id_t self,
360 const vm_val_t *index_val);
361
362 /* assign an indexed value: this sets a variable by frame index */
363 virtual int set_index_val_q(VMG_ vm_val_t *new_container,
364 vm_obj_id_t self,
365 const vm_val_t *index_val,
366 const vm_val_t *new_val);
367
368 /* notify of deletion */
369 void notify_delete(VMG_ int in_root_set);
370
371 /* undo methods - we don't participate in undo, so these are no-ops */
372 void notify_new_savept() { }
373 void apply_undo(VMG_ struct CVmUndoRecord *) { }
374
375 /* mark references */
376 void mark_refs(VMG_ uint);
377
378 /* we're immutable, so we don't have to worry about undo */
379 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
380
381 /* we don't have any weak references */
382 void remove_stale_weak_refs(VMG0_) { }
383 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
384
385 /* image file operations */
386 void load_from_image(VMG_ vm_obj_id_t, const char *, size_t);
387 void reload_from_image(VMG_ vm_obj_id_t, const char *, size_t);
388 ulong rebuild_image(VMG_ char *, ulong);
389
390 /* we're not loadable and we don't change */
391 int is_changed_since_load() const { return FALSE; }
392
393 /* save/restore */
394 void save_to_file(VMG_ class CVmFile *fp);
395 void restore_from_file(VMG_ vm_obj_id_t self, class CVmFile *fp,
396 class CVmObjFixup *fixups);
397
398 /* post-load initialization */
399 void post_load_init(VMG_ vm_obj_id_t self);
400
401 protected:
402 /* construction */
403 CVmObjFrameRef() { ext_ = 0; }
404 CVmObjFrameRef(VMG_ vm_val_t *fp, const uchar *entry);
405
406 /* allocate our extension */
407 vm_frameref_ext *alloc_ext(VMG_ int nlocals, int nparams);
408
409 /* get my extension, properly cast */
410 vm_frameref_ext *get_ext() const { return (vm_frameref_ext *)ext_; }
411
412 /* make a snapshot of the locals */
413 void make_snapshot(VMG0_);
414
415 /* load the image data */
416 void load_image_data(VMG_ vm_obj_id_t self,
417 const char *ptr, size_t siz);
418 };
419
420
421 /* ------------------------------------------------------------------------ */
422 /*
423 * Registration table objects
424 */
425 class CVmMetaclassFrameDesc: public CVmMetaclass
426 {
427 public:
428 /* get the global name */
429 const char *get_meta_name() const { return "stack-frame-desc/030000"; }
430
431 /* create from image file */
432 void create_for_image_load(VMG_ vm_obj_id_t id)
433 {
434 new (vmg_ id) CVmObjFrameDesc();
435 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
436 }
437
438 /* create from restoring from saved state */
439 void create_for_restore(VMG_ vm_obj_id_t id)
440 {
441 new (vmg_ id) CVmObjFrameDesc();
442 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
443 }
444
445 /* create dynamically using stack arguments */
446 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
447 {
448 err_throw(VMERR_BAD_DYNAMIC_NEW);
449 AFTER_ERR_THROW(return VM_INVALID_OBJ;)
450 }
451
452 /* call a static property */
453 int call_stat_prop(VMG_ vm_val_t *result,
454 const uchar **pc_ptr, uint *argc,
455 vm_prop_id_t prop)
456 {
457 return CVmObjFrameDesc::call_stat_prop(
458 vmg_ result, pc_ptr, argc, prop);
459 }
460 };
461
462 class CVmMetaclassFrameRef: public CVmMetaclass
463 {
464 public:
465 /* get the global name */
466 const char *get_meta_name() const { return "stack-frame-ref/030000"; }
467
468 /* create from image file */
469 void create_for_image_load(VMG_ vm_obj_id_t id)
470 {
471 new (vmg_ id) CVmObjFrameRef();
472 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
473 }
474
475 /* create from restoring from saved state */
476 void create_for_restore(VMG_ vm_obj_id_t id)
477 {
478 new (vmg_ id) CVmObjFrameRef();
479 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
480 }
481
482 /* create dynamically using stack arguments */
483 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
484 {
485 err_throw(VMERR_BAD_DYNAMIC_NEW);
486 AFTER_ERR_THROW(return VM_INVALID_OBJ;)
487 }
488
489 /* call a static property */
490 int call_stat_prop(VMG_ vm_val_t *result,
491 const uchar **pc_ptr, uint *argc,
492 vm_prop_id_t prop)
493 {
494 return CVmObjFrameRef::call_stat_prop(
495 vmg_ result, pc_ptr, argc, prop);
496 }
497 };
498
499 #endif /* VMFREF_H */
500
501 /*
502 * Register the class
503 */
504 VM_REGISTER_METACLASS(CVmObjFrameRef)
505 VM_REGISTER_METACLASS(CVmObjFrameDesc)