| 1 | /* $Header: d:/cvsroot/tads/tads3/VMMETA.H,v 1.2 1999/05/17 02:52:28 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 | 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_H |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "t3std.h" |
| 31 | #include "vmtype.h" |
| 32 | #include "vmglob.h" |
| 33 | #include "vmobj.h" |
| 34 | #include "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_t |
| 43 | { |
| 44 | /* the class descriptor object for the metaclass */ |
| 45 | class CVmMetaclass *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_; |
| 55 | |
| 56 | /* |
| 57 | * the IntrinsicClass object (a CVmObjClass object) that represents |
| 58 | * this class |
| 59 | */ |
| 60 | vm_obj_id_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_t min_prop_; |
| 77 | size_t prop_xlat_cnt_; |
| 78 | unsigned short *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_t *func_xlat_; |
| 87 | size_t func_xlat_cnt_; |
| 88 | |
| 89 | /* relese storage */ |
| 90 | void release_mem() |
| 91 | { |
| 92 | /* if this entry has a property table, delete it */ |
| 93 | if (prop_xlat_ != 0) |
| 94 | { |
| 95 | /* free it */ |
| 96 | t3free(prop_xlat_); |
| 97 | |
| 98 | /* forget the pointer */ |
| 99 | prop_xlat_ = 0; |
| 100 | } |
| 101 | |
| 102 | /* if this entry has a function translation table, delete it */ |
| 103 | if (func_xlat_ != 0) |
| 104 | { |
| 105 | /* free it */ |
| 106 | t3free(func_xlat_); |
| 107 | |
| 108 | /* forget the pointer */ |
| 109 | func_xlat_ = 0; |
| 110 | } |
| 111 | |
| 112 | /* relesae our stored name */ |
| 113 | lib_free_str(image_meta_name_); |
| 114 | } |
| 115 | |
| 116 | /* store the metaclass name as loaded from the image file */ |
| 117 | void set_meta_name(const char *nm) |
| 118 | { |
| 119 | /* allocate space and store the name */ |
| 120 | image_meta_name_ = lib_copy_str(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_xlat(vm_prop_id_t prop, 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_[prop - 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_[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_prop(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_ || prop >= min_prop_ + 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_[prop - 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_t xlat_func(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_) |
| 181 | return 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_[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 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 | CVmMetaTable(size_t init_entries); |
| 208 | |
| 209 | /* delete the table */ |
| 210 | ~CVmMetaTable(); |
| 211 | |
| 212 | /* clear all existing entries from the table */ |
| 213 | void clear(); |
| 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_t *build_runtime_to_compiler_id_table(VMG0_); |
| 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_entry(const char *metaclass_id, size_t func_cnt, |
| 234 | vm_prop_id_t min_prop, vm_prop_id_t max_prop); |
| 235 | |
| 236 | /* add an entry to the table given the registration table index */ |
| 237 | void add_entry(const char *metaclass_id, uint idx, size_t func_cnt, |
| 238 | vm_prop_id_t min_prop, 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_new(uint reg_table_idx, size_t func_cnt, |
| 247 | vm_prop_id_t min_prop, 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_index(uint reg_table_idx) const |
| 255 | { return 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_t *get_entry_by_id(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_t *get_entry_from_reg(int reg_idx) |
| 270 | { |
| 271 | /* translate the registration table index into a dependency index */ |
| 272 | int dep_idx = get_dependency_index(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_[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_idx(uint reg_table_idx, vm_prop_id_t prop) |
| 298 | { |
| 299 | /* get the entry for the registration table index */ |
| 300 | vm_meta_entry_t *entry = get_entry_from_reg(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_prop(prop); |
| 311 | } |
| 312 | |
| 313 | /* get the entry at a given dependency table index */ |
| 314 | vm_meta_entry_t *get_entry(int idx) { return &table_[idx]; } |
| 315 | |
| 316 | /* get the total number of entries in the table */ |
| 317 | size_t get_count() const { return 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_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 326 | uint idx, uint argc); |
| 327 | |
| 328 | /* invoke a static property of a metaclass */ |
| 329 | int call_static_prop(VMG_ vm_val_t *result, const uchar **pc_ptr, |
| 330 | uint idx, uint *argc, 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_image(VMG_ uint idx, vm_obj_id_t id, |
| 337 | const char *ptr, 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_restore(VMG_ uint idx, 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_file(class 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_file(class CVmFile *fp); |
| 358 | |
| 359 | /* rebuild the image file (for use after preinitialization) */ |
| 360 | void rebuild_image(class 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_instances(VMG0_); |
| 369 | |
| 370 | /* forget IntrinsicClass objects we created at startup */ |
| 371 | void forget_intrinsic_class_instances(VMG0_); |
| 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_space(size_t entries, size_t increment); |
| 382 | |
| 383 | /* the table array */ |
| 384 | vm_meta_entry_t *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_; |
| 399 | |
| 400 | /* number of entries defined in the table */ |
| 401 | size_t count_; |
| 402 | |
| 403 | /* number of entries allocated for the table */ |
| 404 | size_t alloc_; |
| 405 | }; |
| 406 | |
| 407 | |
| 408 | #endif /* VMMETA_H */ |