Debian Codegraph @

tads3/vmmeta.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/VMMETA.H,v 1.2 1999/05/17 02:52:28 MJRoberts Exp $ */cxx . . $ `<file>/tads3/vmmeta.h`/
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 vmmeta.h - metaclass dependency table
12 Function
13 The metaclass dependency table establishes the correspondence between
14 metaclass index numbers and metaclasses. The load image file contains
15 a list of the metaclasses that the program requires, identifying each
16 required metaclass with its universally unique identifier string. At
17 load time, we scan this list in the load image and create a dependency
18 table.
19 Notes
20
21 Modified
22 12/01/98 MJRoberts - Creation
23 */
24
25 #ifndef VMMETA_H
26 #define VMMETA_Hcxx . . $ `tads3/vmmeta.h:26:9`!
27
28 #include <stdlib.h>
29
30 #include "t3std.h"cxx . . $ `<file>/tads3/t3std.h`/
31 #include "vmtype.h"cxx . . $ `<file>/tads3/vmtype.h`/
32 #include "vmglob.h"cxx . . $ `<file>/tads3/vmglob.h`/
33 #include "vmobj.h"cxx . . $ `<file>/tads3/vmobj.h`/
34 #include "tcprstyp.h"cxx . . $ `<file>/tads3/tcprstyp.h`/
35
36
37 /* ------------------------------------------------------------------------ */
38 /*
39 * Metaclass table entry. This contains information on the metaclass at
40 * one index in the table.
41 */
42 struct vm_meta_entry_tcxx . . $ vm_meta_entry_t#
43 {
44 /* the class descriptor object for the metaclass */
45 class CVmMetaclasscxx . . $ CVmMetaclass# *meta_cxx . . $ vm_meta_entry_t#meta_.;
46
47 /*
48 * name of metaclass as read from image file (this is important for
49 * rewriting an image file during preinit, since it ensures that we
50 * store only the version number required by the compiled image
51 * file, not the version number actually implemented in the preinit
52 * interpreter)
53 */
54 char *image_meta_name_cxx . . $ vm_meta_entry_t#image_meta_name_.;
55
56 /*
57 * the IntrinsicClass object (a CVmObjClass object) that represents
58 * this class
59 */
60 vm_obj_id_tcxx . . $ vm_obj_id_t# class_obj_cxx . . $ vm_meta_entry_t#class_obj_.;
61
62 /*
63 * Property translation list. The translation list is an array of
64 * internal function numbers, indexed by property ID. However, not
65 * all property ID's are necessarily represented in the index -
66 * instead, the index contains entries only from a minimum property
67 * ID, and only contains a specified number of entries. So, given
68 * property ID 'prop_id', we find the internal function number for
69 * the property at index (prop_id - min_prop_id) in the table.
70 *
71 * We use an unsigned short (16 bits) for each internal function
72 * number - this limits the number of functions per metaclass to
73 * 65535, which should be more than adequate while keeping memory
74 * usage within reason.
75 */
76 vm_prop_id_tcxx . . $ vm_prop_id_t# min_prop_cxx . . $ vm_meta_entry_t#min_prop_.;
77 size_tcxx . . $ size_t# prop_xlat_cnt_cxx . . $ vm_meta_entry_t#prop_xlat_cnt_.;
78 unsigned short *prop_xlat_cxx . . $ vm_meta_entry_t#prop_xlat_.;
79
80 /*
81 * Function table. This is the reverse of the property translation
82 * list: this table gives the property ID for each of the metaclass
83 * functions. The first entry is the property of function index 1,
84 * the second entry is function index 2, and so on.
85 */
86 vm_prop_id_tcxx . . $ vm_prop_id_t# *func_xlat_cxx . . $ vm_meta_entry_t#func_xlat_.;
87 size_tcxx . . $ size_t# func_xlat_cnt_cxx . . $ vm_meta_entry_t#func_xlat_cnt_.;
88
89 /* relese storage */
90 void release_memcxx . . $ vm_meta_entry_t#release_mem(49f6e7a06ebc5aa8).()
91 {
92 /* if this entry has a property table, delete it */
93 if (prop_xlat_cxx . . $ vm_meta_entry_t#prop_xlat_. != 0)
94 {
95 /* free it */
96 t3freecxx . . $ vm_meta_entry_t#prop_xlat_.(prop_xlat_);
97
98 /* forget the pointer */
99 prop_xlat_cxx . . $ vm_meta_entry_t#prop_xlat_. = 0;
100 }
101
102 /* if this entry has a function translation table, delete it */
103 if (func_xlat_cxx . . $ vm_meta_entry_t#func_xlat_. != 0)
104 {
105 /* free it */
106 t3freecxx . . $ `tads3/t3std.h:776:9`!(func_xlat_);
107
108 /* forget the pointer */
109 func_xlat_cxx . . $ vm_meta_entry_t#func_xlat_. = 0;
110 }
111
112 /* relesae our stored name */
113 lib_free_strcxx . . $ lib_free_str(fda57c8427c60377).(image_meta_name_cxx . . $ vm_meta_entry_t#image_meta_name_.);
114 }
115
116 /* store the metaclass name as loaded from the image file */
117 void set_meta_namecxx . . $ vm_meta_entry_t#set_meta_name(85c52e162fed56f9).(const char *nm)
118 {
119 /* allocate space and store the name */
120 image_meta_name_cxx . . $ vm_meta_entry_t#image_meta_name_. = lib_copy_strcxx . . $ lib_copy_str(a8b8e32347182212).(nm);
121 }
122
123 /*
124 * Add a property to the table, given the property ID and the
125 * 1-based index of the corresponding function in the metaclass's
126 * internal function table.
127 */
128 void add_prop_xlatcxx . . $ vm_meta_entry_t#add_prop_xlat(161476d40f03fa8c).(vm_prop_id_tcxx . . $ vm_prop_id_t# prop, ushortcxx . . $ ushort# func_idx)
129 {
130 /*
131 * Add the translation entry from property ID to function index
132 * - note that we store this as a 1-based function table index
133 * value, because we reserve function index 0 to indicate that
134 * the property is invalid and has no function translation
135 */
136 prop_xlat_cxx . . $ vm_meta_entry_t#prop_xlat_.[prop - min_prop_cxx . . $ vm_meta_entry_t#min_prop_.] = func_idx;
137
138 /*
139 * Add the translation entry from function index to property ID.
140 * Note that we must adjust the 1-based function index down one
141 * to get the index into our internal array.
142 */
143 func_xlat_cxx . . $ vm_meta_entry_t#func_xlat_.[func_idx - 1] = prop;
144 }
145
146 /*
147 * Get the translation for a given property ID. This returns zero
148 * if the property does not map to a valid function index for the
149 * metaclass, or the 1-based index of the function in the
150 * metaclass's "vtable" if the property is defined.
151 */
152 unsigned short xlat_propcxx . . $ vm_meta_entry_t#xlat_prop(c1ccd8b0fe723753).(vm_prop_id_tcxx . . $ vm_prop_id_t# prop) const
153 {
154 /*
155 * Check to see if the property is represented in the table. If
156 * it's not within the range of properties in the table, there
157 * is no translation for the property.
158 */
159 if (prop < min_prop_cxx . . $ vm_meta_entry_t#min_prop_. || prop >= min_prop_cxx . . $ vm_meta_entry_t#min_prop_. + prop_xlat_cnt_cxx . . $ vm_meta_entry_t#prop_xlat_cnt_.)
160 {
161 /* the property isn't in the table, so it has no translation */
162 return 0;
163 }
164
165 /* return the function index from the table */
166 return prop_xlat_cxx . . $ vm_meta_entry_t#prop_xlat_.[prop - min_prop_cxx . . $ vm_meta_entry_t#min_prop_.];
167 }
168
169 /*
170 * Translate a function index to a property ID. The function index
171 * is given as a 1-based index, for consistency with the return
172 * value from xlat_prop().
173 */
174 vm_prop_id_tcxx . . $ vm_prop_id_t# xlat_funccxx . . $ vm_meta_entry_t#xlat_func(c1ccd8b0fe723753).(unsigned short func_idx) const
175 {
176 /*
177 * if it's zero, or it's greater than the number of functions in
178 * our table, it has no property translation
179 */
180 if (func_idx == 0 || func_idx > func_xlat_cnt_cxx . . $ vm_meta_entry_t#func_xlat_cnt_.)
181 return VM_INVALID_PROPcxx . . $ VM_INVALID_PROP.;
182
183 /*
184 * return the entry from our table for the given index, adjusted
185 * to a 1-based index value
186 */
187 return func_xlat_cxx . . $ vm_meta_entry_t#func_xlat_.[func_idx - 1];
188 }
189 };
190
191
192 /* ------------------------------------------------------------------------ */
193 /*
194 * The metaclass table. One global instance of this table is generally
195 * created when the image file is loaded.
196 */
197 class CVmMetaTablecxx . . $ CVmMetaTable#
198 {
199 public:
200 /*
201 * Create a table with a given number of initial entries. The table
202 * may be expanded in the future if necessary, but if the caller can
203 * predict the maximum number of entries required, we can
204 * preallocate the table at its final size and thus avoid the
205 * overhead and memory fragmentation of expanding the table.
206 */
207 CVmMetaTablecxx . . $ CVmMetaTable#CVmMetaTable(2c44cd906ae4e829).(size_tcxx . . $ size_t# init_entries);
208
209 /* delete the table */
210 ~cxx . . $ CVmMetaTable#`~CVmMetaTable`(49f6e7a06ebc5aa8).CVmMetaTablecxx . . $ CVmMetaTable#();
211
212 /* clear all existing entries from the table */
213 void clearcxx . . $ CVmMetaTable#clear(49f6e7a06ebc5aa8).();
214
215 /*
216 * Build a translation table from runtime metaclass index to the
217 * compiler's internal TC_META_xxx scheme. The table is simply an
218 * array of TC_META_xxx values indexed by metaclass index number. The
219 * caller is responsible for freeing the returned memory via t3free()
220 * when done with it.
221 */
222 static tc_metaclass_tcxx . . $ tc_metaclass_t# *build_runtime_to_compiler_id_tablecxx . . $ CVmMetaTable#build_runtime_to_compiler_id_table(7dff57f77d8d756f).(VMG0_cxx . . $ `tads3/vmglob.h:350:9`!);
223
224 /*
225 * Add an entry to the table, given the metaclass identifier (a
226 * string giving the universally unique name for the metaclass).
227 * Fills in the next available slot. Throws an error if the
228 * metaclass is not present in the system.
229 *
230 * If min_prop and max_prop are both VM_INVALID_PROP, the metaclass
231 * has no properties in the translation table.
232 */
233 void add_entrycxx . . $ CVmMetaTable#add_entry(f5720a33fba213ed).(const char *metaclass_id, size_tcxx . . $ size_t# func_cnt,
234 vm_prop_id_tcxx . . $ vm_prop_id_t# min_prop, vm_prop_id_tcxx . . $ vm_prop_id_t# max_prop);
235
236 /* add an entry to the table given the registration table index */
237 void add_entrycxx . . $ CVmMetaTable#add_entry(24f163fe81fbff9).(const char *metaclass_id, uintcxx . . $ uint# idx, size_tcxx . . $ size_t# func_cnt,
238 vm_prop_id_tcxx . . $ vm_prop_id_t# min_prop, vm_prop_id_tcxx . . $ vm_prop_id_t# max_prop);
239
240 /*
241 * Add an entry to the table given the registration table index, but
242 * only if the metaclass at this index isn't already defined in the
243 * dependency table; if the metaclass is already in the dependency
244 * table, this function has no effect.
245 */
246 void add_entry_if_newcxx . . $ CVmMetaTable#add_entry_if_new(770279507d984567).(uintcxx . . $ uint# reg_table_idx, size_tcxx . . $ size_t# func_cnt,
247 vm_prop_id_tcxx . . $ vm_prop_id_t# min_prop, vm_prop_id_tcxx . . $ vm_prop_id_t# max_prop);
248
249 /*
250 * Get the dependency table index for a metaclass, given the
251 * registration table index. If the metaclass is not present in the
252 * metaclass table, we'll return -1.
253 */
254 int get_dependency_indexcxx . . $ CVmMetaTable#get_dependency_index(b89e35493f65ea2b).(uintcxx . . $ uint# reg_table_idx) const
255 { return reverse_map_cxx . . $ CVmMetaTable#reverse_map_.[reg_table_idx]; }
256
257 /*
258 * Get the entry for a given external metaclass ID. This returns the
259 * table entry if the metaclass is loaded, null if not. If the name is
260 * given with a "/version" suffix, we'll return null if the loaded
261 * version is older than the specified version.
262 */
263 vm_meta_entry_tcxx . . $ vm_meta_entry_t# *get_entry_by_idcxx . . $ CVmMetaTable#get_entry_by_id(5c551379eb2822a3).(const char *id) const;
264
265 /*
266 * get the depencency table entry for a metaclass, given the
267 * registration table index
268 */
269 vm_meta_entry_tcxx . . $ vm_meta_entry_t# *get_entry_from_regcxx . . $ CVmMetaTable#get_entry_from_reg(a3f0acc5539c7826).(int reg_idx)
270 {
271 /* translate the registration table index into a dependency index */
272 int dep_idx = get_dependency_indexcxx . . $ CVmMetaTable#get_dependency_index(b89e35493f65ea2b).(reg_idx);
273
274 /*
275 * if we have a valid dependency index, return it; otherwise,
276 * return null
277 */
278 if (dep_idx >= 0)
279 return &table_cxx . . $ CVmMetaTable#table_.[dep_idx];
280 else
281 return 0;
282 }
283
284 /*
285 * Given a registration table index for a metaclass and a property
286 * ID, retrieve the metaclass function vector index for the property
287 * according to the image file's property mapping for the metaclass.
288 * Returns zero if the property is not in the mapping, 1 for the
289 * first function in the vector, 2 for the second function, and so
290 * on.
291 *
292 * Note that we use the registration table index, NOT the dependency
293 * index, because this function is provided for use by the metaclass
294 * itself, which only has convenient access to its registration
295 * index.
296 */
297 int prop_to_vector_idxcxx . . $ CVmMetaTable#prop_to_vector_idx(10dfdad8c5442a70).(uintcxx . . $ uint# reg_table_idx, vm_prop_id_tcxx . . $ vm_prop_id_t# prop)
298 {
299 /* get the entry for the registration table index */
300 vm_meta_entry_tcxx . . $ vm_meta_entry_t# *entry = get_entry_from_regcxx . . $ CVmMetaTable#get_entry_from_reg(a3f0acc5539c7826).(reg_table_idx);
301
302 /*
303 * if there's no entry for this registration index, there's
304 * obviously no metaclass function mapping for the property
305 */
306 if (entry == 0)
307 return 0;
308
309 /* return the property translation */
310 return entry->xlat_propcxx . . $ vm_meta_entry_t#xlat_prop(c1ccd8b0fe723753).(prop);
311 }
312
313 /* get the entry at a given dependency table index */
314 vm_meta_entry_tcxx . . $ vm_meta_entry_t# *get_entrycxx . . $ CVmMetaTable#get_entry(a3f0acc5539c7826).(int idx) { return &table_cxx . . $ CVmMetaTable#table_.[idx]; }
315
316 /* get the total number of entries in the table */
317 size_tcxx . . $ size_t# get_countcxx . . $ CVmMetaTable#get_count(fe4d2d262d4164a8).() const { return count_cxx . . $ CVmMetaTable#count_.; }
318
319 /*
320 * Invoke the VM-stack-based constructor for the metaclass at the
321 * given index to create a new instance of that metaclass. Throws
322 * an error if no such entry is defined. Returns the object ID of
323 * the constructed object.
324 */
325 vm_obj_id_tcxx . . $ vm_obj_id_t# create_from_stackcxx . . $ CVmMetaTable#create_from_stack(60928ef09a1f8783).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! const ucharcxx . . $ uchar# **pc_ptr,
326 uintcxx . . $ uint# idx, uintcxx . . $ uint# argc);
327
328 /* invoke a static property of a metaclass */
329 int call_static_propcxx . . $ CVmMetaTable#call_static_prop(d93e682e36c314fa).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! vm_val_tcxx . . $ vm_val_t# *result, const ucharcxx . . $ uchar# **pc_ptr,
330 uintcxx . . $ uint# idx, uintcxx . . $ uint# *argc, vm_prop_id_tcxx . . $ vm_prop_id_t# prop);
331
332 /*
333 * Create an object with the given ID and load the object from the
334 * image file.
335 */
336 void create_from_imagecxx . . $ CVmMetaTable#create_from_image(5c9678ca7816057f).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! uintcxx . . $ uint# idx, vm_obj_id_tcxx . . $ vm_obj_id_t# id,
337 const char *ptr, size_tcxx . . $ size_t# siz);
338
339 /*
340 * create an object in preparation for loading the object from a
341 * saved state file
342 */
343 void create_for_restorecxx . . $ CVmMetaTable#create_for_restore(b4d55b1286ff73e8).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! uintcxx . . $ uint# idx, vm_obj_id_tcxx . . $ vm_obj_id_t# id);
344
345 /*
346 * write the dependency table to a file, for later reloading with
347 * read_from_file()
348 */
349 void write_to_filecxx . . $ CVmMetaTable#write_to_file(4ba0339f79e70c18).(class CVmFilecxx . . $ CVmFile# *fp);
350
351 /*
352 * Read the dependency table from a file, as previously written by
353 * write_to_file(). Returns zero on success, or an error code on
354 * failure. We will entirely clear the current table before
355 * loading, so the new table will replace the existing table.
356 */
357 int read_from_filecxx . . $ CVmMetaTable#read_from_file(55f024ebabc290c2).(class CVmFilecxx . . $ CVmFile# *fp);
358
359 /* rebuild the image file (for use after preinitialization) */
360 void rebuild_imagecxx . . $ CVmMetaTable#rebuild_image(c374e6bbbdfec736).(class CVmImageWritercxx . . $ CVmImageWriter# *writer);
361
362 /*
363 * Create an IntrinsicClass object for each metaclass that doesn't
364 * already have an IntrinsicClass object. This ensures that every
365 * intrinsic class in use has an associated IntrinsicClass instance,
366 * even if the compiler didn't generate one.
367 */
368 void create_intrinsic_class_instancescxx . . $ CVmMetaTable#create_intrinsic_class_instances(49f6e7a06ebc5aa8).(VMG0_cxx . . $ `tads3/vmglob.h:350:9`!);
369
370 /* forget IntrinsicClass objects we created at startup */
371 void forget_intrinsic_class_instancescxx . . $ CVmMetaTable#forget_intrinsic_class_instances(49f6e7a06ebc5aa8).(VMG0_cxx . . $ `tads3/vmglob.h:350:9`!);
372
373 private:
374 /*
375 * Ensure we have space for a given number of entries, allocating
376 * more if necessary. If we must allocate more space, we'll
377 * increase the current allocation size by at least the given
378 * increment (more if necessary to bring it up to the required
379 * size).
380 */
381 void ensure_spacecxx . . $ CVmMetaTable#ensure_space(6714b5f2551eea49).(size_tcxx . . $ size_t# entries, size_tcxx . . $ size_t# increment);
382
383 /* the table array */
384 vm_meta_entry_tcxx . . $ vm_meta_entry_t# *table_cxx . . $ CVmMetaTable#table_.;
385
386 /*
387 * reverse dependency map - each entry in this table is the
388 * dependency table index of the metaclass at the given registration
389 * table index:
390 *
391 * reserve_map[registration_table_index] = dependency_table_index
392 *
393 * This information lets us determine if a given metaclass is in the
394 * current dependency table at all, and if so what it's index is.
395 * We store -1 in each entry that doesn't appear in the dependency
396 * table.
397 */
398 int *reverse_map_cxx . . $ CVmMetaTable#reverse_map_.;
399
400 /* number of entries defined in the table */
401 size_tcxx . . $ size_t# count_cxx . . $ CVmMetaTable#count_.;
402
403 /* number of entries allocated for the table */
404 size_tcxx . . $ size_t# alloc_cxx . . $ CVmMetaTable#alloc_.;
405 };
406
407
408 #endif /* VMMETA_H */