| 1 | /* $Header: d:/cvsroot/tads/tads3/vmfunc.h,v 1.3 1999/07/11 00:46:59 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 | vmfunc.h - T3 VM Function Header definitions |
| 12 | Function |
| 13 | Defines the layout of a function header, which is the block of data |
| 14 | immediately preceding the first byte code instruction in every function. |
| 15 | The function header is stored in binary portable format, so that image |
| 16 | files can be loaded directly into memory and executed without translation. |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 11/20/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #ifndef VMFUNC_H |
| 24 | #define VMFUNC_H |
| 25 | |
| 26 | #include "t3std.h" |
| 27 | #include "vmtype.h" |
| 28 | #include "vmglob.h" |
| 29 | |
| 30 | |
| 31 | /* ------------------------------------------------------------------------ */ |
| 32 | /* |
| 33 | * FUNCTION HEADER |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * The function header is a packed array of bytes, with each element stored |
| 38 | * in a canonical binary format for binary portability. No padding is |
| 39 | * present except where otherwise specified. The fields in the function |
| 40 | * header, starting at offset zero, are: |
| 41 | * |
| 42 | * UBYTE argc - the number of parameters the function expects to receive. |
| 43 | * If the high-order bit is set (i.e., (argc & 0x80) != 0), then the |
| 44 | * function takes a variable number of parameters, with a minimum of (argc |
| 45 | * & 0x7f) and no maximum. If the high-order bit is clear, argc gives the |
| 46 | * exact number of parameters required. |
| 47 | * |
| 48 | * UBYTE optional_argc - number of additional optional parameters the |
| 49 | * function can receive, beyond the fixed arguments given by the 'argc' |
| 50 | * field. |
| 51 | * |
| 52 | * UINT2 locals - the number of local variables the function uses. This |
| 53 | * does not count the implicit argument counter local variable, which is |
| 54 | * always pushed by the VM after setting up a new activation frame. |
| 55 | * |
| 56 | * UINT2 total_stack - the total number of stack slots required by the |
| 57 | * function, including local variables, intermediate results of |
| 58 | * calculations, and actual parameters to functions invoked by this code. |
| 59 | * |
| 60 | * UINT2 exception_table_ofs - the byte offset from the start of this |
| 61 | * method header of the function's exception table. This value is zero if |
| 62 | * the function has no exception table. |
| 63 | * |
| 64 | * UINT2 debug_ofs - the byte offset from the start of this method header |
| 65 | * of the function's debugger records. This value is zero if the function |
| 66 | * has no debugger records. |
| 67 | */ |
| 68 | |
| 69 | /* minimum function header size supported by this version of the VM */ |
| 70 | const size_t VMFUNC_HDR_MIN_SIZE = 10; |
| 71 | |
| 72 | class CVmFuncPtr |
| 73 | { |
| 74 | public: |
| 75 | CVmFuncPtr() { p_ = 0; } |
| 76 | CVmFuncPtr(VMG_ pool_ofs_t ofs); |
| 77 | CVmFuncPtr(const char *p) { p_ = (const uchar *)p; } |
| 78 | CVmFuncPtr(const uchar *p) { p_ = p; } |
| 79 | CVmFuncPtr(VMG_ const vm_val_t *val) { set(vmg_ val); } |
| 80 | |
| 81 | /* initialize with a pointer to the start of the function */ |
| 82 | void set(const uchar *p) { p_ = p; } |
| 83 | |
| 84 | /* set from a vm_val_t; returns true on success, false on failure */ |
| 85 | int set(VMG_ const vm_val_t *val); |
| 86 | |
| 87 | /* get the header pointer */ |
| 88 | const uchar *get() const { return p_; } |
| 89 | |
| 90 | /* copy from another function pointer */ |
| 91 | void copy_from(const CVmFuncPtr *fp) { p_ = fp->p_; } |
| 92 | |
| 93 | /* get a vm_val_t pointer to this function */ |
| 94 | int get_fnptr(VMG_ vm_val_t *v); |
| 95 | |
| 96 | /* get the minimum argument count */ |
| 97 | int get_min_argc() const |
| 98 | { |
| 99 | /* get the argument count, but mask out the varargs bit */ |
| 100 | return (int)(get_argc() & 0x7f); |
| 101 | } |
| 102 | |
| 103 | /* get the maximum argument count, not counting varargs */ |
| 104 | int get_max_argc() const |
| 105 | { |
| 106 | return get_min_argc() + (int)get_opt_argc(); |
| 107 | } |
| 108 | |
| 109 | /* is this a varargs function? */ |
| 110 | int is_varargs() const { return ((get_argc() & 0x80) != 0); } |
| 111 | |
| 112 | /* |
| 113 | * check an actual parameter count for correctness; returns true if |
| 114 | * the count is correct for this function, false if not |
| 115 | */ |
| 116 | int argc_ok(int argc) const |
| 117 | { |
| 118 | /* check for match to the min-max range */ |
| 119 | if (argc >= get_min_argc() && argc <= get_max_argc()) |
| 120 | { |
| 121 | /* we have an exact match, so we're fine */ |
| 122 | return TRUE; |
| 123 | } |
| 124 | else if (is_varargs() && argc > get_min_argc()) |
| 125 | { |
| 126 | /* |
| 127 | * we have variable arguments, and we have at least the |
| 128 | * minimum, so we're okay |
| 129 | */ |
| 130 | return TRUE; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | /* |
| 135 | * either we don't have variable arguments, or we don't have |
| 136 | * the minimum varargs count - in either case, we have an |
| 137 | * argument count mistmatch |
| 138 | */ |
| 139 | return FALSE; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Get the internal argument count. This has the high bit set for a |
| 145 | * varargs function, and the low-order seven bits give the nominal |
| 146 | * argument count. If this is a varargs function, the nominal |
| 147 | * argument count is the minimum count: any actual number of arguments |
| 148 | * at least the nominal count is valid. If this is not a varargs |
| 149 | * function, the nominal count is the exact count: the actual number |
| 150 | * of arguments must match the nominal count. |
| 151 | */ |
| 152 | uchar get_argc() const { return *(p_ + 0); } |
| 153 | |
| 154 | /* get the additional optional argument count */ |
| 155 | uint get_opt_argc() const { return *(p_ + 1); } |
| 156 | |
| 157 | /* get the number of locals */ |
| 158 | uint get_local_cnt() const { return osrp2(p_ + 2); } |
| 159 | |
| 160 | /* get the total stack slots required by the function */ |
| 161 | uint get_stack_depth() const { return osrp2(p_ + 4); } |
| 162 | |
| 163 | /* get the exception table offset */ |
| 164 | uint get_exc_ofs() const { return osrp2(p_ + 6); } |
| 165 | |
| 166 | /* get the debugger records table offset */ |
| 167 | uint get_debug_ofs() const { return osrp2(p_ + 8); } |
| 168 | |
| 169 | /* |
| 170 | * Set up an exception table pointer for this function. Returns |
| 171 | * true if successful, false if there's no exception table. |
| 172 | */ |
| 173 | int set_exc_ptr(class CVmExcTablePtr *exc_ptr) const; |
| 174 | |
| 175 | /* |
| 176 | * Set up a debug table pointer for this function. Returns true if |
| 177 | * successful, false if there's no debug table. |
| 178 | */ |
| 179 | int set_dbg_ptr(class CVmDbgTablePtr *dbg_ptr) const; |
| 180 | |
| 181 | private: |
| 182 | /* pointer to the method header */ |
| 183 | const uchar *p_; |
| 184 | }; |
| 185 | |
| 186 | /* ------------------------------------------------------------------------ */ |
| 187 | /* |
| 188 | * EXCEPTION TABLE |
| 189 | */ |
| 190 | |
| 191 | /* |
| 192 | * The exception table starts with a count indicating how many elements |
| 193 | * are in the table, followed by the table entries. Each entry in the |
| 194 | * table specifies the handler for one protected range of code. We |
| 195 | * search the table in forward order, so the handlers must be stored in |
| 196 | * order of precedence. |
| 197 | * |
| 198 | * Each table entry contains: |
| 199 | * |
| 200 | * UINT2 start_ofs - the starting offset (as a byte offset from the |
| 201 | * start of the function) of the protected range for this handler. |
| 202 | * |
| 203 | * UINT2 end_ofs - the ending offset (as a byte offset from the start of |
| 204 | * the function) of the protected range for this handler. The range is |
| 205 | * inclusive of this offset, so a one-byte range would have start_ofs |
| 206 | * and end_ofs set to the same value. |
| 207 | * |
| 208 | * UINT4 exception_class - the object ID of the class of exception |
| 209 | * handled by this handler. |
| 210 | * |
| 211 | * UINT2 handler_ofs - the handler offset (as a byte offset from the |
| 212 | * start of the function). This is the offset of the first instruction |
| 213 | * of the code to be invoked to handle this exception. |
| 214 | */ |
| 215 | |
| 216 | /* |
| 217 | * Exception Table Entry Pointer |
| 218 | */ |
| 219 | class CVmExcEntryPtr |
| 220 | { |
| 221 | /* let CVmExcTablePtr initialize us */ |
| 222 | friend class CVmExcTablePtr; |
| 223 | |
| 224 | public: |
| 225 | /* get the starting/ending offset from this entry */ |
| 226 | uint get_start_ofs() const { return osrp2(p_); } |
| 227 | uint get_end_ofs() const { return osrp2(p_ + 2); } |
| 228 | |
| 229 | /* get the exception class's object ID */ |
| 230 | vm_obj_id_t get_exception() const { return (vm_obj_id_t)t3rp4u(p_ + 4); } |
| 231 | |
| 232 | /* get the handler byte code offset */ |
| 233 | uint get_handler_ofs() const { return osrp2(p_ + 8); } |
| 234 | |
| 235 | /* increment the pointer to the next entry in the table */ |
| 236 | void inc(VMG0_) { p_ += G_exc_entry_size; } |
| 237 | |
| 238 | private: |
| 239 | /* initialize with a pointer to the first byte of our entry */ |
| 240 | void set(const uchar *p) { p_ = p; } |
| 241 | |
| 242 | /* pointer to the first byte of our entry */ |
| 243 | const uchar *p_; |
| 244 | }; |
| 245 | |
| 246 | /* |
| 247 | * Exception Table Pointer |
| 248 | */ |
| 249 | class CVmExcTablePtr |
| 250 | { |
| 251 | public: |
| 252 | /* |
| 253 | * Initialize with a pointer to the start of the function -- we'll |
| 254 | * read the exception table offset out of the method header. |
| 255 | * Returns true if the function has an exception table, false if |
| 256 | * there is no exception table defined in the function. |
| 257 | */ |
| 258 | int set(const uchar *p) |
| 259 | { |
| 260 | CVmFuncPtr func; |
| 261 | |
| 262 | /* set up the function pointer */ |
| 263 | func.set(p); |
| 264 | |
| 265 | /* if there's no exception table, simply return this information */ |
| 266 | if (func.get_exc_ofs() == 0) |
| 267 | return FALSE; |
| 268 | |
| 269 | /* set up our pointer by reading from the header */ |
| 270 | p_ = p + func.get_exc_ofs(); |
| 271 | |
| 272 | /* indicate that there is a valid exception table */ |
| 273 | return TRUE; |
| 274 | } |
| 275 | |
| 276 | /* get the number of entries in the table */ |
| 277 | size_t get_count() const { return osrp2(p_); } |
| 278 | |
| 279 | /* initialize a CVmExcEntryPtr with the entry at the given index */ |
| 280 | void set_entry_ptr(VMG_ CVmExcEntryPtr *entry, size_t idx) const |
| 281 | { entry->set(p_ + 2 + (idx * G_exc_entry_size)); } |
| 282 | |
| 283 | private: |
| 284 | /* pointer to the first byte of the exception table */ |
| 285 | const uchar *p_; |
| 286 | }; |
| 287 | |
| 288 | /* ------------------------------------------------------------------------ */ |
| 289 | /* |
| 290 | * DEBUGGER RECORDS TABLE |
| 291 | */ |
| 292 | |
| 293 | /* |
| 294 | * The debugger table consists of three sections. The first section is |
| 295 | * the header, with general information on the method or function. The |
| 296 | * second section is the line records, which give the code boundaries of |
| 297 | * the source lines. The third section is the frame records, giving the |
| 298 | * local symbol tables. |
| 299 | */ |
| 300 | |
| 301 | /* |
| 302 | * Debugger source line entry pointer |
| 303 | */ |
| 304 | class CVmDbgLinePtr |
| 305 | { |
| 306 | /* let CVmDbgTablePtr initialize us */ |
| 307 | friend class CVmDbgTablePtr; |
| 308 | |
| 309 | public: |
| 310 | /* |
| 311 | * get the byte-code offset (from method start) of the start of the |
| 312 | * byte-code for this line |
| 313 | */ |
| 314 | uint get_start_ofs() const { return osrp2(p_); } |
| 315 | |
| 316 | /* get the source file ID (an index into the global source file list) */ |
| 317 | uint get_source_id() const { return osrp2(p_ + 2); } |
| 318 | |
| 319 | /* get the line number in the source file */ |
| 320 | ulong get_source_line() const { return t3rp4u(p_ + 4); } |
| 321 | |
| 322 | /* get the frame ID (a 1-based index into our local frame table) */ |
| 323 | uint get_frame_id() const { return osrp2(p_ + 8); } |
| 324 | |
| 325 | /* increment the pointer to the next entry in the table */ |
| 326 | void inc(VMG0_) { p_ += G_line_entry_size; } |
| 327 | |
| 328 | /* set from another line pointer */ |
| 329 | void copy_from(const CVmDbgLinePtr *p) { p_ = p->p_; } |
| 330 | |
| 331 | private: |
| 332 | /* initialize with a pointer to the first byte of our entry */ |
| 333 | void set(const uchar *p) { p_ = p; } |
| 334 | |
| 335 | /* pointer to the first byte of our entry */ |
| 336 | const uchar *p_; |
| 337 | }; |
| 338 | |
| 339 | /* |
| 340 | * Debugger frame symbol entry pointer |
| 341 | */ |
| 342 | class CVmDbgFrameSymPtr |
| 343 | { |
| 344 | /* let CVmDbgFramePtr initialize us */ |
| 345 | friend class CVmDbgFramePtr; |
| 346 | |
| 347 | public: |
| 348 | /* get the local/parameter number */ |
| 349 | uint get_var_num() const { return osrp2(p_); } |
| 350 | |
| 351 | /* get the context array index (for context locals) */ |
| 352 | vm_prop_id_t get_ctx_arr_idx() const |
| 353 | { return (vm_prop_id_t)osrp2(p_ + 4); } |
| 354 | |
| 355 | /* determine if I'm a local or a parameter */ |
| 356 | int is_local() const { return (get_flags() & 1) == 0; } |
| 357 | int is_param() const |
| 358 | { return (((get_flags() & 1) != 0) && !is_ctx_local()); } |
| 359 | |
| 360 | /* determine if I'm a context local */ |
| 361 | int is_ctx_local() const { return (get_flags() & 2) != 0; } |
| 362 | |
| 363 | /* get the length of my name string */ |
| 364 | uint get_sym_len(VMG0_) const { return osrp2(get_symptr(vmg0_)); } |
| 365 | |
| 366 | /* get a pointer to my name string - this is not null-terminated */ |
| 367 | const char *get_sym(VMG0_) const { return get_symptr(vmg0_) + 2; } |
| 368 | |
| 369 | /* increment this pointer to point to the next symbol in the frame */ |
| 370 | void inc(VMG0_) |
| 371 | { |
| 372 | /* skip the in-line symbol, or the pointer */ |
| 373 | if (is_sym_inline()) |
| 374 | p_ += get_sym_len(vmg0_) + 2; |
| 375 | else |
| 376 | p_ += 4; |
| 377 | |
| 378 | /* skip the header */ |
| 379 | p_ += G_dbg_lclsym_hdr_size; |
| 380 | } |
| 381 | |
| 382 | /* is my symbol in-line or in the constant pool? */ |
| 383 | int is_sym_inline() const { return !(get_flags() & 0x0004); } |
| 384 | |
| 385 | /* |
| 386 | * Set up a vm_val_t for the symbol. For an in-line symbol, this |
| 387 | * creates a new string object; for a constant pool element, this |
| 388 | * returns a VM_SSTR pointer to it. |
| 389 | */ |
| 390 | void get_str_val(VMG_ vm_val_t *val) const; |
| 391 | |
| 392 | private: |
| 393 | /* get my flags value */ |
| 394 | uint get_flags() const { return osrp2(p_ + 2); } |
| 395 | |
| 396 | /* |
| 397 | * get a pointer to my symbol data - this points to a UINT2 length |
| 398 | * prefix followed by the bytes of the UTF-8 string |
| 399 | */ |
| 400 | const char *get_symptr(VMG0_) const; |
| 401 | |
| 402 | /* initialize with a pointer to the first byte of our entry */ |
| 403 | void set(const uchar *p) { p_ = p; } |
| 404 | |
| 405 | /* pointer to the first byte of our entry */ |
| 406 | const uchar *p_; |
| 407 | }; |
| 408 | |
| 409 | /* |
| 410 | * Debugger frame entry pointer |
| 411 | */ |
| 412 | class CVmDbgFramePtr |
| 413 | { |
| 414 | /* let CVmDbgTablePtr initialize us */ |
| 415 | friend class CVmDbgTablePtr; |
| 416 | |
| 417 | public: |
| 418 | /* copy from another frame pointer */ |
| 419 | void copy_from(const CVmDbgFramePtr *frame) |
| 420 | { |
| 421 | /* copy the original frame's pointer */ |
| 422 | p_ = frame->p_; |
| 423 | } |
| 424 | |
| 425 | /* get the ID of the enclosing frame */ |
| 426 | uint get_enclosing_frame() const { return osrp2(p_); } |
| 427 | |
| 428 | /* get the number of symbols in the frame */ |
| 429 | uint get_sym_count() const { return osrp2(p_ + 2); } |
| 430 | |
| 431 | /* set up a pointer to the first symbol */ |
| 432 | void set_first_sym_ptr(VMG_ CVmDbgFrameSymPtr *entry) |
| 433 | { entry->set(p_ + G_dbg_frame_size); } |
| 434 | |
| 435 | /* get the bytecode range covered by the frame */ |
| 436 | uint get_start_ofs(VMG0_) |
| 437 | { return G_dbg_frame_size >= 8 ? osrp2(p_ + 4) : 0; } |
| 438 | uint get_end_ofs(VMG0_) |
| 439 | { return G_dbg_frame_size >= 8 ? osrp2(p_ + 6) : 0; } |
| 440 | |
| 441 | /* is this frame nested in the given frame? */ |
| 442 | int is_nested_in(VMG_ const class CVmDbgTablePtr *dp, int i); |
| 443 | |
| 444 | private: |
| 445 | /* initialize with a pointer to the first byte of our entry */ |
| 446 | void set(const uchar *p) { p_ = p; } |
| 447 | |
| 448 | /* pointer to the first byte of our entry */ |
| 449 | const uchar *p_; |
| 450 | }; |
| 451 | |
| 452 | |
| 453 | /* |
| 454 | * Debugger Records Table Pointer |
| 455 | */ |
| 456 | class CVmDbgTablePtr |
| 457 | { |
| 458 | public: |
| 459 | /* |
| 460 | * Initialize with a pointer to the start of the function -- we'll |
| 461 | * read the debugger table offset out of the method header. Returns |
| 462 | * true if the function has debugger records, false if there is no |
| 463 | * debugger table defined in the function. |
| 464 | */ |
| 465 | int set(const uchar *p) |
| 466 | { |
| 467 | CVmFuncPtr func; |
| 468 | |
| 469 | /* if the pointer is null, there's obviously no function pointer */ |
| 470 | if (p == 0) |
| 471 | return FALSE; |
| 472 | |
| 473 | /* set up the function pointer */ |
| 474 | func.set(p); |
| 475 | |
| 476 | /* if there's no debugger table, simply return this information */ |
| 477 | if (func.get_debug_ofs() == 0) |
| 478 | return FALSE; |
| 479 | |
| 480 | /* set up our pointer by reading from the header */ |
| 481 | p_ = p + func.get_debug_ofs(); |
| 482 | |
| 483 | /* indicate that there is a valid debugger records table */ |
| 484 | return TRUE; |
| 485 | } |
| 486 | |
| 487 | /* copy from another debug table pointer */ |
| 488 | void copy_from(const CVmDbgTablePtr *table) |
| 489 | { |
| 490 | /* copy the other table's location */ |
| 491 | p_ = table->p_; |
| 492 | } |
| 493 | |
| 494 | /* get the number of source line entries in the table */ |
| 495 | size_t get_line_count(VMG0_) const |
| 496 | { return osrp2(p_ + G_dbg_hdr_size); } |
| 497 | |
| 498 | /* get the number of frame entries in the table */ |
| 499 | size_t get_frame_count(VMG0_) const |
| 500 | { return osrp2(p_ + get_frame_ofs(vmg0_)); } |
| 501 | |
| 502 | /* initialize a CVmDbgLinePtr with the entry at the given index */ |
| 503 | void set_line_ptr(VMG_ CVmDbgLinePtr *entry, size_t idx) const |
| 504 | { entry->set(p_ + G_dbg_hdr_size + 2 + (idx * G_line_entry_size)); } |
| 505 | |
| 506 | /* initialize a CVmDbgFramePtr with the entry at the given index */ |
| 507 | void set_frame_ptr(VMG_ CVmDbgFramePtr *entry, size_t idx) const |
| 508 | { |
| 509 | size_t index_ofs; |
| 510 | size_t frame_ofs; |
| 511 | |
| 512 | /* |
| 513 | * Compute the location of the index table entry - note that |
| 514 | * 'idx' is a one-based index value, so we must decrement it |
| 515 | * before performing our offset arithmetic. Note also that we |
| 516 | * must add two bytes to get past the count field at the start |
| 517 | * of the frame table. Each index entry is two bytes long. |
| 518 | * |
| 519 | * (If we were clever, we would distribute the multiply-by-two, |
| 520 | * which would yield a constant subtraction of two, which would |
| 521 | * cancel the constant addition of two. Let's hope the C++ |
| 522 | * catches on to this, because we would rather not be clever and |
| 523 | * instead write it explicitly for greater clarity.) |
| 524 | */ |
| 525 | index_ofs = get_frame_ofs(vmg0_) + 2 + (2 * (idx - 1)); |
| 526 | |
| 527 | /* read the frame offset from the entry */ |
| 528 | frame_ofs = osrp2(p_ + index_ofs); |
| 529 | |
| 530 | /* |
| 531 | * the frame offset in the table is relative to the location of |
| 532 | * the table location containing the entry, so add the index |
| 533 | * offset to the frame offset to get the actual location of the |
| 534 | * frame entry |
| 535 | */ |
| 536 | entry->set(p_ + index_ofs + frame_ofs); |
| 537 | } |
| 538 | |
| 539 | private: |
| 540 | /* get the offset to the start of the frame table */ |
| 541 | size_t get_frame_ofs(VMG0_) const |
| 542 | { |
| 543 | /* |
| 544 | * the frame table follows the line records, which follow the |
| 545 | * debug table header and the line counter, plus another two |
| 546 | * bytes for the post-frame offset pointer |
| 547 | */ |
| 548 | return (G_dbg_hdr_size |
| 549 | + 2 |
| 550 | + (get_line_count(vmg0_) * G_line_entry_size) |
| 551 | + 2); |
| 552 | } |
| 553 | |
| 554 | /* pointer to the first byte of the debugger records table */ |
| 555 | const uchar *p_; |
| 556 | }; |
| 557 | |
| 558 | #endif /* VMFUNC_H */ |
| 559 |