| 1 | /* $Header: d:/cvsroot/tads/tads3/vmbif.h,v 1.2 1999/05/17 02:52:29 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 | vmbif.h - built-in function interface |
| 12 | Function |
| 13 | Provides the interface to built-in function sets. |
| 14 | |
| 15 | The host application environment may provide more than one set of |
| 16 | built-in functions. Each function set is identified by a universally |
| 17 | unique identifier; the program image specifies one or more function |
| 18 | sets using these identifiers and maps each set to an image-specific |
| 19 | index. At run-time, the byte-code program calls built-in functions |
| 20 | by specifying a function set index and a function index within the |
| 21 | set. |
| 22 | |
| 23 | A particular function set may use more than one universal identifier, |
| 24 | because of version evolution. If new functions are added to a function |
| 25 | set, the new function set can continue to use its original universal |
| 26 | identifier, as long as the function set continues to provide the |
| 27 | original set of functions as a subset of its new vector, and as long |
| 28 | as the functions in the original set are at the same index positions |
| 29 | in the modified function set. |
| 30 | Notes |
| 31 | |
| 32 | Modified |
| 33 | 12/05/98 MJRoberts - Creation |
| 34 | */ |
| 35 | |
| 36 | #ifndef VMBIF_H |
| 37 | #define VMBIF_H |
| 38 | |
| 39 | #include "t3std.h" |
| 40 | #include "vmglob.h" |
| 41 | #include "vmtype.h" |
| 42 | #include "vmfunc.h" |
| 43 | |
| 44 | |
| 45 | /* ------------------------------------------------------------------------ */ |
| 46 | /* |
| 47 | * Image function set table. We maintain one global function set table, |
| 48 | * which we create when we load the image file. |
| 49 | */ |
| 50 | |
| 51 | class CVmBifTable |
| 52 | { |
| 53 | public: |
| 54 | /* |
| 55 | * Create a table with a given number of initial entries. The table |
| 56 | * may be expanded in the future if necessary, but if the caller can |
| 57 | * predict the maximum number of entries required, we can |
| 58 | * preallocate the table at its final size and thus avoid the |
| 59 | * overhead and memory fragmentation of expanding the table. |
| 60 | */ |
| 61 | CVmBifTable(size_t init_entries); |
| 62 | |
| 63 | /* delete the table */ |
| 64 | ~CVmBifTable(); |
| 65 | |
| 66 | /* clear all entries from the table */ |
| 67 | void clear(VMG0_); |
| 68 | |
| 69 | /* |
| 70 | * Add an entry to the table, given the function set identifier (a |
| 71 | * string giving the universally unique name for the function set). |
| 72 | * Fills in the next available slot. Throws an error if the |
| 73 | * function set is not present in the system. A function set may |
| 74 | * not be present because it's a newer version than this |
| 75 | * implementation provides, or because this particular host |
| 76 | * application environment does not provide the function set. |
| 77 | */ |
| 78 | void add_entry(VMG_ const char *func_set_id); |
| 79 | |
| 80 | /* get the total number of entries in the table */ |
| 81 | size_t get_count() const { return count_; } |
| 82 | |
| 83 | /* get the descriptor for a function; returns null if it's invalid */ |
| 84 | const struct vm_bif_desc *get_desc(uint set_index, uint func_index); |
| 85 | |
| 86 | /* validate an entry */ |
| 87 | int validate_entry(uint set_index, uint func_index); |
| 88 | |
| 89 | /* get the entry at the given index */ |
| 90 | const struct vm_bif_entry_t *get_entry(uint idx) |
| 91 | { return idx < count_ ? table_[idx] : 0; } |
| 92 | |
| 93 | /* |
| 94 | * Look up an entry by function set name. If the name has a "/nnnnnn" |
| 95 | * version suffix, we'll return null if the loaded version is older |
| 96 | * than the requested version. |
| 97 | */ |
| 98 | const struct vm_bif_entry_t *get_entry(const char *name); |
| 99 | |
| 100 | /* call the given function from the given function set */ |
| 101 | void call_func(VMG_ uint set_index, uint func_index, uint argc); |
| 102 | |
| 103 | private: |
| 104 | /* |
| 105 | * Ensure we have space for a given number of entries, allocating |
| 106 | * more if necessary. If we must allocate more space, we'll |
| 107 | * increase the current allocation size by at least the given |
| 108 | * increment (more if necessary to bring it up to the required |
| 109 | * size). |
| 110 | */ |
| 111 | void ensure_space(size_t entries, size_t increment); |
| 112 | |
| 113 | /* |
| 114 | * Add an unresolved function set to the table, or throw an error if |
| 115 | * this isn't allowed. If we require resolution of function sets at |
| 116 | * load time, this should simply throw an error; otherwise, this |
| 117 | * should make a null entry in the function set table so that we can |
| 118 | * recognize the missing function set if one of its functions is |
| 119 | * ever invoked. |
| 120 | */ |
| 121 | void add_entry_unresolved(VMG_ const char *func_set_id); |
| 122 | |
| 123 | /* the table array - we keep an array of pointers */ |
| 124 | struct vm_bif_entry_t **table_; |
| 125 | |
| 126 | /* |
| 127 | * Name array - this is a list of the global function set names; |
| 128 | * each entry corresponds to the entry of the table_ array at the |
| 129 | * same index. We use this only for call-time resolution, so that |
| 130 | * we can report the name of an unavailable function set when a |
| 131 | * function from the unavailable set is invoked. |
| 132 | */ |
| 133 | char **names_; |
| 134 | |
| 135 | /* number of entries defined in the table */ |
| 136 | ushort count_; |
| 137 | |
| 138 | /* number of entries allocated for the table */ |
| 139 | size_t alloc_; |
| 140 | }; |
| 141 | |
| 142 | /* ------------------------------------------------------------------------ */ |
| 143 | /* |
| 144 | * Built-in function descriptor. This tells us how to call a function, and |
| 145 | * provides reflection data on the function. |
| 146 | */ |
| 147 | struct vm_bif_desc |
| 148 | { |
| 149 | /* |
| 150 | * The function entrypoint. |
| 151 | * |
| 152 | * Each function has a common C interface, because the functions take |
| 153 | * arguments and return values on the VM stack. The order of the |
| 154 | * functions within the vector is defined by the function set |
| 155 | * specification; a function set which uses a particular universal |
| 156 | * identifier must always conform to the specification for that |
| 157 | * universal identifier. |
| 158 | * |
| 159 | * For each function, 'argc' is the number of actual parameters passed |
| 160 | * to the function by the caller. The function receives its parameters |
| 161 | * from the VM stack; the first argument is at the top of the stack, |
| 162 | * the second argument is the next item on the stack, and so on. The |
| 163 | * function must remove all of the arguments from the stack before |
| 164 | * returning, and must push a return value if appropriate. |
| 165 | */ |
| 166 | void (*func)(VMG_ uint argc); |
| 167 | |
| 168 | /* minimum number of arguments required */ |
| 169 | int min_argc; |
| 170 | |
| 171 | /* additional optional arguments */ |
| 172 | int opt_argc; |
| 173 | |
| 174 | /* are additional variadic arguments allowed? */ |
| 175 | int varargs; |
| 176 | |
| 177 | /* |
| 178 | * VM_BIFPTR value for this function. During image file loading, we |
| 179 | * fill this in for each function, so that the function can create a |
| 180 | * pointer to itself during execution as needed. |
| 181 | */ |
| 182 | vm_val_t bifptr; |
| 183 | |
| 184 | /* |
| 185 | * Synthetic method header. We build this during the initialization |
| 186 | * process to mimic a byte-code method header. This makes bif pointers |
| 187 | * fit in with the rest of the type system by letting CVmFuncPtr pull |
| 188 | * out reflection information using the same format as for byte-code |
| 189 | * methods. |
| 190 | */ |
| 191 | char synth_hdr[VMFUNC_HDR_MIN_SIZE]; |
| 192 | |
| 193 | /* fill in the synthetic header */ |
| 194 | void init_synth_hdr() |
| 195 | { |
| 196 | /* set everything to zeros to start with */ |
| 197 | memset(synth_hdr, 0, sizeof(synth_hdr)); |
| 198 | |
| 199 | /* |
| 200 | * Fill in the base arguments field. This is an 8-bit field |
| 201 | * containing the minimum number of arguments, with the high bit |
| 202 | * (0x80) set if it's varargs. |
| 203 | */ |
| 204 | synth_hdr[0] = (char)(min_argc | (varargs ? 0x80 : 0)); |
| 205 | |
| 206 | /* set the optional argument count field */ |
| 207 | synth_hdr[1] = (char)opt_argc; |
| 208 | |
| 209 | /* |
| 210 | * We can leave all of the other fields zeroed out: |
| 211 | * |
| 212 | * - locals, total_stack: the caller doesn't allocate a standard |
| 213 | * stack frame for a built-in, so these are unused |
| 214 | * |
| 215 | * - exception_table_ofs: we don't have any bytecode, so we |
| 216 | * obviously don't have a bytecode exception table; leave it zeroed |
| 217 | * out to indicate the absence of a table |
| 218 | * |
| 219 | * - debug_ofs: we don't have any debugger information since we |
| 220 | * don't have any bytecode; zero indicates no records |
| 221 | */ |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | /* ------------------------------------------------------------------------ */ |
| 226 | /* |
| 227 | * Function set table entry. This contains information on the function |
| 228 | * set at one index in the table. |
| 229 | */ |
| 230 | struct vm_bif_entry_t |
| 231 | { |
| 232 | /* |
| 233 | * Function set identifier - a string of 7-bit ASCII characters, one |
| 234 | * byte per character, in the range 32 to 126 inclusive, of length 1 |
| 235 | * to 255 characters, null-terminated. |
| 236 | */ |
| 237 | const char *func_set_id; |
| 238 | |
| 239 | /* number of functions in the function set */ |
| 240 | size_t func_count; |
| 241 | |
| 242 | /* the function vector */ |
| 243 | vm_bif_desc *func; |
| 244 | |
| 245 | /* "attach" function for the set - perform static initialization */ |
| 246 | void (*attach)(VMG0_); |
| 247 | |
| 248 | /* "detach" function for the set - perform static cleanup */ |
| 249 | void (*detach)(VMG0_); |
| 250 | |
| 251 | /* link to the image file */ |
| 252 | void link_to_image(VMG_ ushort set_idx) |
| 253 | { |
| 254 | /* initialize the function set */ |
| 255 | (*attach)(vmg0_); |
| 256 | |
| 257 | /* link each function entry */ |
| 258 | for (ushort i = 0 ; i < func_count ; ++i) |
| 259 | { |
| 260 | /* set up the bifptr value for the function */ |
| 261 | func[i].bifptr.set_bifptr(set_idx, i); |
| 262 | |
| 263 | /* generate the synthetic method header for the function */ |
| 264 | func[i].init_synth_hdr(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* termination */ |
| 269 | void unload_image(VMG_ int set_idx) |
| 270 | { |
| 271 | /* perform static cleanup */ |
| 272 | (*detach)(vmg0_); |
| 273 | } |
| 274 | }; |
| 275 | |
| 276 | /* ------------------------------------------------------------------------ */ |
| 277 | /* |
| 278 | * Base class for function set collections. This class provides some |
| 279 | * utility functions that intrinsics might find useful. |
| 280 | */ |
| 281 | class CVmBif |
| 282 | { |
| 283 | public: |
| 284 | /* |
| 285 | * Attach to the function set. The image file loader calls this during |
| 286 | * program load for each function set linked into the image file. This |
| 287 | * gives the function set a chance to to any static initialization. We |
| 288 | * only call this when the function set is actually going to be used, |
| 289 | * which avoids unnecessarily allocation resources when the function |
| 290 | * set isn't needed. |
| 291 | * |
| 292 | * Note that this is a static function, but it's kind of like a "class |
| 293 | * virtual" in that the loader explicitly calls the suitable |
| 294 | * Subclass::attach() and Subclass::detach() for each CVmBif subclass |
| 295 | * linked from the image file. If you don't define an overriding |
| 296 | * static in a subclass, the compiler will simply link to this base |
| 297 | * class method. |
| 298 | */ |
| 299 | static void attach(VMG0_) { } |
| 300 | |
| 301 | /* |
| 302 | * Detach from the function set. The system calls this during program |
| 303 | * termination to let the function set do any static cleanup of |
| 304 | * resources allocated during attach(). |
| 305 | */ |
| 306 | static void detach(VMG0_) { } |
| 307 | |
| 308 | /* |
| 309 | * check arguments; throws an error if the argument count doesn't |
| 310 | * match the given value |
| 311 | */ |
| 312 | static void check_argc(VMG_ uint argc, uint needed_argc); |
| 313 | |
| 314 | /* |
| 315 | * check arguments; throws an error if the argument count is outside |
| 316 | * of the given range |
| 317 | */ |
| 318 | static void check_argc_range(VMG_ uint argc, |
| 319 | uint argc_min, uint argc_max); |
| 320 | |
| 321 | /* pop an integer/long value */ |
| 322 | static int pop_int_val(VMG0_); |
| 323 | static int32_t pop_long_val(VMG0_); |
| 324 | |
| 325 | /* pop an integer value, or use a default if the argument is nil */ |
| 326 | static int pop_int_or_nil(VMG_ int defval); |
| 327 | |
| 328 | /* pop a true/nil logical value */ |
| 329 | static int pop_bool_val(VMG0_); |
| 330 | |
| 331 | /* pop an object ID value */ |
| 332 | static vm_obj_id_t pop_obj_val(VMG0_); |
| 333 | |
| 334 | /* pop a property ID value */ |
| 335 | static vm_prop_id_t pop_propid_val(VMG0_); |
| 336 | |
| 337 | /* |
| 338 | * Pop a string or list value, returning a pointer to the |
| 339 | * string/list data. If the value is a constant string or constant |
| 340 | * list, as appropriate, we'll return the constant pool pointer; if |
| 341 | * the value is an object of metaclass String or List, as |
| 342 | * appropriate, we'll return the metaclass data. Throws an error if |
| 343 | * the value is of any other type. |
| 344 | */ |
| 345 | static const char *pop_str_val(VMG0_); |
| 346 | static const char *pop_list_val(VMG0_); |
| 347 | |
| 348 | /* get a value as a string */ |
| 349 | static const char *get_str_val(VMG_ const vm_val_t *val); |
| 350 | |
| 351 | /* |
| 352 | * Pop a null-terminated string into the given buffer. If the |
| 353 | * string is too long for the buffer, we'll truncate it to the given |
| 354 | * size. |
| 355 | */ |
| 356 | static void pop_str_val_buf(VMG_ char *buf, size_t buflen); |
| 357 | static void get_str_val_buf(VMG_ char *buf, size_t buflen, |
| 358 | const vm_val_t *val); |
| 359 | |
| 360 | /* |
| 361 | * Pop a string value, returning the result as a filename in the given |
| 362 | * buffer. The returned filename is null-terminated and converted to |
| 363 | * the local filename character set. The source value must be a |
| 364 | * string. |
| 365 | */ |
| 366 | static void pop_str_val_fname(VMG_ char *buf, size_t buflen); |
| 367 | static void get_str_val_fname(VMG_ char *buf, size_t buflen, |
| 368 | const char *strp); |
| 369 | |
| 370 | /* |
| 371 | * Pop a filename value, returning the result as a filename in the |
| 372 | * given buffer. The returned filename is null-terminated and |
| 373 | * converted to the local filename character set. The source value can |
| 374 | * be a string or FileName object. |
| 375 | */ |
| 376 | static void pop_fname_val(VMG_ char *buf, size_t buflen); |
| 377 | static void get_fname_val(VMG_ char *buf, size_t buflen, |
| 378 | const vm_val_t *val); |
| 379 | |
| 380 | /* |
| 381 | * Pop a null-terminated string into the given buffer, converting the |
| 382 | * string to the UI character set. Null-terminates the resulting |
| 383 | * string. If the given buffer is null, we'll allocate a buffer with |
| 384 | * t3malloc() and return it; the caller is responsible for freeing the |
| 385 | * buffer with t3free(). In any case, returns the buffer into which we |
| 386 | * store the results. |
| 387 | */ |
| 388 | static char *pop_str_val_ui(VMG_ char *buf, size_t buflen); |
| 389 | |
| 390 | /* create a string object from a C-style string in the UI character set */ |
| 391 | static vm_obj_id_t str_from_ui_str(VMG_ const char *str); |
| 392 | static vm_obj_id_t str_from_ui_str(VMG_ const char *str, size_t len); |
| 393 | |
| 394 | /* |
| 395 | * Get/pop a CharacterSet argument from the stack. If the argument is |
| 396 | * an object, it must be a CharacterSet object. If it's a string, |
| 397 | * we'll create a CharacterSet object based on the given character set |
| 398 | * name. We'll also allow nil if desired. |
| 399 | */ |
| 400 | static vm_obj_id_t pop_charset_obj(VMG0_); |
| 401 | static vm_obj_id_t get_charset_obj(VMG_ int stk_idx); |
| 402 | |
| 403 | /* get a character set object from a value */ |
| 404 | static vm_obj_id_t get_charset_obj(VMG_ const vm_val_t *val); |
| 405 | |
| 406 | /* |
| 407 | * Return a value |
| 408 | */ |
| 409 | static void retval(VMG_ const struct vm_val_t *val); |
| 410 | static void retval_nil(VMG0_); |
| 411 | static void retval_true(VMG0_); |
| 412 | static void retval_bool(VMG_ int val); |
| 413 | static void retval_int(VMG_ long val); |
| 414 | static void retval_obj(VMG_ vm_obj_id_t obj); |
| 415 | static void retval_prop(VMG_ vm_prop_id_t prop); |
| 416 | static void retval_str(VMG_ const char *str); |
| 417 | static void retval_str(VMG_ const char *str, size_t len); |
| 418 | static void retval_ui_str(VMG_ const char *str); |
| 419 | static void retval_ui_str(VMG_ const char *str, size_t len); |
| 420 | static void retval_fnptr(VMG_ pool_ofs_t func); |
| 421 | static void retval_bifptr(VMG_ ushort set_idx, ushort func_idx); |
| 422 | |
| 423 | /* return the value at top of stack */ |
| 424 | static void retval_pop(VMG0_); |
| 425 | }; |
| 426 | |
| 427 | |
| 428 | #endif /* VMBIF_H */ |
| 429 |