Debian Codegraph @

terps/tads/tads3/vmfunc.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 vmfunc.cpp - T3 VM function header operations
15 Function
16
17 Notes
18
19 Modified
20 11/24/99 MJRoberts - Creation
21 */
22
23 #include "vmfunc.h"
24 #include "vmpool.h"
25 #include "vmglob.h"
26 #include "vmtype.h"
27 #include "vmobj.h"
28 #include "vmbif.h"
29 #include "vmstr.h"
30 #include "vmdynfunc.h"
31
32
33 /* ------------------------------------------------------------------------ */
34 /*
35 * Initialize from a code pool address
36 */
37 CVmFuncPtr::CVmFuncPtr(VMG_ pool_ofs_t ofs)
38 {
39 p_ = (const uchar *)G_code_pool->get_ptr(ofs);
40 }
41
42 /*
43 * Initialize from a vm_val_t
44 */
45 int CVmFuncPtr::set(VMG_ const vm_val_t *val)
46 {
47 vm_val_t invoker;
48
49 /* we might need to re-resolve from an invoker, so loop until resolved */
50 for (;;)
51 {
52 /* check the type */
53 switch (val->typ)
54 {
55 case VM_OBJ:
56 case VM_OBJX:
57 /* object - check to see if it's invokable */
58 if (vm_objp(vmg_ val->val.obj)->get_invoker(vmg_ &invoker))
59 {
60 /* it's invokable - re-resolve using the invoker */
61 val = &invoker;
62 }
63 else
64 {
65 /* other object types are not function pointers */
66 set((uchar *)0);
67 return FALSE;
68 }
69 break;
70
71 case VM_FUNCPTR:
72 case VM_CODEOFS:
73 /* function pointer - get the address from the code pool */
74 set((const uchar *)G_code_pool->get_ptr(val->val.ofs));
75 return TRUE;
76
77 case VM_CODEPTR:
78 /* direct code pointer - the value contains the address */
79 set((const uchar *)val->val.ptr);
80 return TRUE;
81
82 case VM_BIFPTR:
83 case VM_BIFPTRX:
84 /* built-in function pointer */
85 {
86 /*
87 * decode the function set information from the value, and
88 * look up the bif descriptor in the registration table
89 */
90 const vm_bif_desc *desc = G_bif_table->get_desc(
91 val->val.bifptr.set_idx, val->val.bifptr.func_idx);
92
93 /* fail if there's no descriptor */
94 if (desc == 0)
95 return FALSE;
96
97 /* point to the synthetic header */
98 set((const uchar *)desc->synth_hdr);
99 }
100 return TRUE;
101
102 default:
103 /* other types are invalid */
104 set((uchar *)0);
105 return FALSE;
106 }
107 }
108 }
109
110
111 /* ------------------------------------------------------------------------ */
112 /*
113 * Get a vm_val_t pointer to this function
114 */
115 int CVmFuncPtr::get_fnptr(VMG_ vm_val_t *v)
116 {
117 /*
118 * Try translating the method address to a code pool offset. If that
119 * succeeds, it's a regular static function.
120 */
121 pool_ofs_t entry_ofs;
122 if (G_code_pool->get_ofs((const char *)p_, &entry_ofs))
123 {
124 /* it's a regular function call */
125 v->set_fnptr(entry_ofs);
126 return TRUE;
127 }
128
129 /*
130 * It's not a static function pointer, so it must be a dynamic
131 * function. A DynamicFunc stores a prefix header before the start of
132 * our method header, containing the DynamicFunc object ID. Read the
133 * object ID, and return it as a VM_OBJ value.
134 */
135 vm_obj_id_t obj = CVmDynamicFunc::get_obj_from_prefix(vmg_ p_);
136 if (obj != VM_INVALID_OBJ)
137 {
138 v->set_obj(obj);
139 return TRUE;
140 }
141
142 /* we couldn't find any valid object */
143 v->set_nil();
144 return FALSE;
145 }
146
147
148 /* ------------------------------------------------------------------------ */
149 /*
150 * Set up an exception table pointer for this function. Returns true if
151 * successful, false if there's no exception table.
152 */
153 int CVmFuncPtr::set_exc_ptr(CVmExcTablePtr *exc_ptr) const
154 {
155 /* set the exception pointer based on my address */
156 return exc_ptr->set(p_);
157 }
158
159 /*
160 * Set up a debug table pointer for this function. Returns true if
161 * successful, false if there's no debug table.
162 */
163 int CVmFuncPtr::set_dbg_ptr(CVmDbgTablePtr *dbg_ptr)const
164 {
165 /* set the debug pointer based on my address */
166 return dbg_ptr->set(p_);
167 }
168
169 /* ------------------------------------------------------------------------ */
170 /*
171 * Get the symbol data from a local variable symbol in the frame table
172 */
173 const char *CVmDbgFrameSymPtr::get_symptr(VMG0_) const
174 {
175 /* get a pointer to the symbol field in this record */
176 const char *symp = (const char *)p_ + G_dbg_lclsym_hdr_size;
177
178 /*
179 * if it's stored inline, this points directly to the symbol data;
180 * otherwise it's a constant pool offset pointing to the symbol data
181 */
182 if (is_sym_inline())
183 return symp;
184 else
185 return G_const_pool->get_ptr(osrp4(symp));
186 }
187
188 void CVmDbgFrameSymPtr::get_str_val(VMG_ vm_val_t *val) const
189 {
190 /* get a pointer to the symbol field in this record */
191 const char *symp = (const char *)p_ + G_dbg_lclsym_hdr_size;
192
193 /* check whether the symbol string is in-line or in the constant pool */
194 if (is_sym_inline())
195 {
196 /* it's inline, so we need to create a string object for it */
197 val->set_obj(CVmObjString::create(vmg_ FALSE, symp + 2, osrp2(symp)));
198 }
199 else
200 {
201 /* it's in the constant pool, so we can just return a VM_SSTR */
202 val->set_sstring(osrp4(symp));
203 }
204 }
205
206 /* ------------------------------------------------------------------------ */
207 /*
208 * is this frame nested in the given frame?
209 */
210 int CVmDbgFramePtr::is_nested_in(VMG_ const class CVmDbgTablePtr *dp, int i)
211 {
212 /* scan our parent list for 'i' */
213 for (int parent = get_enclosing_frame() ; parent != 0 ; )
214 {
215 /* if this parent is 'i', we're inside 'i' */
216 if (parent == i)
217 return TRUE;
218
219 /* set up a pointer to the parent frame */
220 CVmDbgFramePtr fp;
221 dp->set_frame_ptr(vmg_ &fp, parent);
222
223 /* get its enclosing frame */
224 parent = fp.get_enclosing_frame();
225 }
226
227 /* it's not a nested frame */
228 return FALSE;
229 }