| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/VMRUN.CPP,v 1.4 1999/07/11 00:46:58 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1998, 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 | vmrun.cpp - VM Execution |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 11/12/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #include "t3std.h" |
| 26 | #include "os.h" |
| 27 | #include "vmrun.h" |
| 28 | #include "vmdbg.h" |
| 29 | #include "vmop.h" |
| 30 | #include "vmstack.h" |
| 31 | #include "vmerr.h" |
| 32 | #include "vmerrnum.h" |
| 33 | #include "vmobj.h" |
| 34 | #include "vmlst.h" |
| 35 | #include "vmstr.h" |
| 36 | #include "vmtobj.h" |
| 37 | #include "vmfunc.h" |
| 38 | #include "vmmeta.h" |
| 39 | #include "vmbif.h" |
| 40 | #include "vmpredef.h" |
| 41 | #include "vmfile.h" |
| 42 | #include "vmsave.h" |
| 43 | #include "vmprof.h" |
| 44 | #include "vmhash.h" |
| 45 | #include "vmlookup.h" |
| 46 | #include "vmfref.h" |
| 47 | #include "vmop.h" |
| 48 | #include "vmbignum.h" |
| 49 | |
| 50 | |
| 51 | /* ------------------------------------------------------------------------ */ |
| 52 | /* |
| 53 | * Define static global variables for certain VM registers, if we're |
| 54 | * compiling the system in a static global configuration. |
| 55 | * |
| 56 | * Empirically, it makes a measurable difference (on the order of 2% |
| 57 | * compared to the struct configuration) to keep these in globals and to |
| 58 | * keep them together. These are the VM's equivalent of CPU registers, and |
| 59 | * they get hit a lot during bytecode execution; keeping this small cluster |
| 60 | * of hot memory locations together seems to enable the CPU to keep them in |
| 61 | * cache. |
| 62 | */ |
| 63 | VM_IF_REGS_IN_GLOBALS(vm_val_t *sp_;) |
| 64 | VM_IF_REGS_IN_GLOBALS(vm_val_t *frame_ptr_;) |
| 65 | VM_IF_REGS_IN_GLOBALS(vm_val_t r0_;) |
| 66 | VM_IF_REGS_IN_GLOBALS(const uchar *entry_ptr_native_;) |
| 67 | VM_IF_REGS_IN_GLOBALS(const uchar **pc_ptr_;) |
| 68 | |
| 69 | |
| 70 | /* ------------------------------------------------------------------------ */ |
| 71 | /* |
| 72 | * Initialize |
| 73 | */ |
| 74 | CVmRun::CVmRun(size_t max_depth, size_t reserve_depth) |
| 75 | : CVmStack(max_depth, reserve_depth) |
| 76 | { |
| 77 | init(); |
| 78 | } |
| 79 | |
| 80 | void CVmRun::init() |
| 81 | { |
| 82 | /* inherit base class handling */ |
| 83 | CVmStack::init(); |
| 84 | |
| 85 | /* start out with 'nil' in R0 */ |
| 86 | r0_.set_nil(); |
| 87 | |
| 88 | /* there's no frame yet */ |
| 89 | frame_ptr_ = 0; |
| 90 | |
| 91 | /* there's no entry pointer yet */ |
| 92 | entry_ptr_native_ = 0; |
| 93 | |
| 94 | /* function header size is not yet known */ |
| 95 | funchdr_size_ = 0; |
| 96 | |
| 97 | /* we have no 'say' function yet */ |
| 98 | say_func_ = 0; |
| 99 | |
| 100 | /* no default 'say' method */ |
| 101 | say_method_ = VM_INVALID_PROP; |
| 102 | |
| 103 | /* no debugger halt requested yet */ |
| 104 | halt_vm_ = FALSE; |
| 105 | |
| 106 | /* we have no program counter yet */ |
| 107 | pc_ptr_ = 0; |
| 108 | |
| 109 | /* |
| 110 | * If we're including the profiler in the build, allocate and |
| 111 | * initialize its memory structures. |
| 112 | */ |
| 113 | #ifdef VM_PROFILER |
| 114 | |
| 115 | /* |
| 116 | * Allocate the profiler stack. This stack will contain one record per |
| 117 | * activation frame in the regular VM stack. |
| 118 | */ |
| 119 | prof_stack_max_ = 250; |
| 120 | (prof_stack_ = (vm_profiler_rec *) |
| 121 | t3malloc(prof_stack_max_ * sizeof(prof_stack_[0]))); |
| 122 | |
| 123 | /* we don't have anything on the profiler stack yet */ |
| 124 | prof_stack_idx_ = 0; |
| 125 | |
| 126 | /* create the profiler master hash table */ |
| 127 | prof_master_table_ = new CVmHashTable(512, new CVmHashFuncCI(), TRUE); |
| 128 | |
| 129 | /* we're not running the profiler yet */ |
| 130 | profiling_ = FALSE; |
| 131 | |
| 132 | #endif /* VM_PROFILER */ |
| 133 | } |
| 134 | |
| 135 | /* ------------------------------------------------------------------------ */ |
| 136 | /* |
| 137 | * Terminate |
| 138 | */ |
| 139 | CVmRun::~CVmRun() |
| 140 | { |
| 141 | terminate(); |
| 142 | } |
| 143 | |
| 144 | void CVmRun::terminate() |
| 145 | { |
| 146 | /* |
| 147 | * If we're including the profiler in the build, delete its memory |
| 148 | * structures. |
| 149 | */ |
| 150 | #ifdef VM_PROFILER |
| 151 | |
| 152 | /* delete the profiler stack */ |
| 153 | if (prof_stack_ != 0) |
| 154 | { |
| 155 | t3free(prof_stack_); |
| 156 | prof_stack_ = 0; |
| 157 | } |
| 158 | |
| 159 | /* delete the profiler master hash table */ |
| 160 | if (prof_master_table_ != 0) |
| 161 | { |
| 162 | delete prof_master_table_; |
| 163 | prof_master_table_ = 0; |
| 164 | } |
| 165 | |
| 166 | #endif /* VM_PROFILER */ |
| 167 | } |
| 168 | |
| 169 | /* ------------------------------------------------------------------------ */ |
| 170 | /* |
| 171 | * Set the function header size |
| 172 | */ |
| 173 | void CVmRun::set_funchdr_size(size_t siz) |
| 174 | { |
| 175 | /* remember the new size */ |
| 176 | funchdr_size_ = siz; |
| 177 | |
| 178 | /* |
| 179 | * Ensure that the size is at least as large as our required function |
| 180 | * header block - if it's not, this version of the VM can't run this |
| 181 | * image file. If we throw an error, flag it as a version mismatch |
| 182 | * error. |
| 183 | */ |
| 184 | if (siz < VMFUNC_HDR_MIN_SIZE) |
| 185 | err_throw_a(VMERR_IMAGE_INCOMPAT_HDR_FMT, 1, ERR_TYPE_VERSION_FLAG); |
| 186 | } |
| 187 | |
| 188 | /* ------------------------------------------------------------------------ */ |
| 189 | /* |
| 190 | * Add two values, leaving the result in *val1 |
| 191 | */ |
| 192 | int CVmRun::compute_sum(VMG_ vm_val_t *val1, const vm_val_t *val2) |
| 193 | { |
| 194 | /* the meaning of "add" depends on the type of the first operand */ |
| 195 | check_type: |
| 196 | switch(val1->typ) |
| 197 | { |
| 198 | case VM_SSTRING: |
| 199 | /* |
| 200 | * string constant - add the second value to the string, using |
| 201 | * the static string add method |
| 202 | */ |
| 203 | CVmObjString::add_to_str(vmg_ val1, val1, val2); |
| 204 | return TRUE; |
| 205 | |
| 206 | case VM_LIST: |
| 207 | /* |
| 208 | * list constant - add the second value to the list, using the |
| 209 | * static list add method |
| 210 | */ |
| 211 | CVmObjList::add_to_list(vmg_ val1, VM_INVALID_OBJ, |
| 212 | get_const_ptr(vmg_ val1->val.ofs), val2); |
| 213 | return TRUE; |
| 214 | |
| 215 | case VM_OBJ: |
| 216 | /* |
| 217 | * object - add the second value to the object, using the |
| 218 | * object's virtual metaclass add method |
| 219 | */ |
| 220 | return vm_objp(vmg_ val1->val.obj)->add_val( |
| 221 | vmg_ val1, val1->val.obj, val2); |
| 222 | |
| 223 | case VM_INT: |
| 224 | /* |
| 225 | * callers handle the int+int case inline, so we can skip that; we |
| 226 | * just need to check for other numeric types |
| 227 | */ |
| 228 | if (val2->is_numeric(vmg0_)) |
| 229 | { |
| 230 | /* |
| 231 | * the other value is numeric but not an integer; promote the |
| 232 | * integer to the other type, then re-do the type check |
| 233 | */ |
| 234 | val2->promote_int(vmg_ val1); |
| 235 | goto check_type; |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | /* can't add a non-numeric value to an integer */ |
| 240 | err_throw(VMERR_NUM_VAL_REQD); |
| 241 | AFTER_ERR_THROW(return FALSE;) |
| 242 | } |
| 243 | |
| 244 | default: |
| 245 | /* other types don't understand '+' as a native operator */ |
| 246 | return FALSE; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Compute the sum for an INC instruction |
| 252 | */ |
| 253 | const uchar *CVmRun::compute_sum_inc(VMG_ const uchar *p) |
| 254 | { |
| 255 | /* add 1 to the value at TOS, leaving it on the stack */ |
| 256 | vm_val_t val2; |
| 257 | val2.set_int(1); |
| 258 | if (compute_sum(vmg_ get(0), &val2)) |
| 259 | { |
| 260 | return p; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | /* no native implementation - check for an overload */ |
| 265 | vm_val_t val; |
| 266 | pop(&val); |
| 267 | push(&val2); |
| 268 | return op_overload(vmg_ p - entry_ptr_native_, -1, |
| 269 | &val, G_predef->operator_add, 1, |
| 270 | VMERR_BAD_TYPE_ADD); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Compute the sum for an ADD instruction |
| 276 | */ |
| 277 | const uchar *CVmRun::compute_sum_add(VMG_ const uchar *p) |
| 278 | { |
| 279 | if (compute_sum(vmg_ get(1), get(0))) |
| 280 | { |
| 281 | /* success - the result is at TOS-1 */ |
| 282 | discard(); |
| 283 | return p; |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | /* try the operator overload */ |
| 288 | vm_val_t val; |
| 289 | pop_left_op(vmg_ &val); |
| 290 | return op_overload(vmg_ p - entry_ptr_native_, -1, |
| 291 | &val, G_predef->operator_add, 1, |
| 292 | VMERR_BAD_TYPE_ADD); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Compute the sum of a local and an immediate value, assigning the result |
| 298 | * to the local |
| 299 | */ |
| 300 | const uchar *CVmRun::compute_sum_lcl_imm(VMG_ vm_val_t *lclp, |
| 301 | const vm_val_t *ival, |
| 302 | int lclidx, const uchar *p) |
| 303 | { |
| 304 | /* compute the sum, leaving the result in the local */ |
| 305 | if (compute_sum(vmg_ lclp, ival)) |
| 306 | { |
| 307 | /* success */ |
| 308 | return p; |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | /* check for an overload */ |
| 313 | push(ival); |
| 314 | return op_overload(vmg_ p - entry_ptr_native_, lclidx, |
| 315 | lclp, G_predef->operator_add, 1, |
| 316 | VMERR_BAD_TYPE_ADD); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /* ------------------------------------------------------------------------ */ |
| 322 | /* |
| 323 | * Compute the difference of two values, leaving the result in *val1 |
| 324 | */ |
| 325 | int CVmRun::compute_diff(VMG_ vm_val_t *val1, vm_val_t *val2) |
| 326 | { |
| 327 | /* the meaning of "subtract" depends on the type of the first operand */ |
| 328 | check_type: |
| 329 | switch(val1->typ) |
| 330 | { |
| 331 | case VM_LIST: |
| 332 | /* |
| 333 | * list constant - remove the second value from the list, using |
| 334 | * the static list subtraction method |
| 335 | */ |
| 336 | CVmObjList::sub_from_list(vmg_ val1, val1, |
| 337 | get_const_ptr(vmg_ val1->val.ofs), val2); |
| 338 | return TRUE; |
| 339 | |
| 340 | case VM_OBJ: |
| 341 | /* object - use the object's virtual subtraction method */ |
| 342 | return vm_objp(vmg_ val1->val.obj)->sub_val( |
| 343 | vmg_ val1, val1->val.obj, val2); |
| 344 | |
| 345 | case VM_INT: |
| 346 | /* |
| 347 | * callers handle the int-int case inline, so we can skip that; |
| 348 | * check for other numeric types |
| 349 | */ |
| 350 | if (val2->is_numeric(vmg0_)) |
| 351 | { |
| 352 | /* |
| 353 | * the other value is numeric but not an integer; promote the |
| 354 | * integer to the other type, then re-do the type check |
| 355 | */ |
| 356 | val2->promote_int(vmg_ val1); |
| 357 | goto check_type; |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | /* other values can't be subtracted */ |
| 362 | err_throw(VMERR_NUM_VAL_REQD); |
| 363 | AFTER_ERR_THROW(return FALSE;) |
| 364 | } |
| 365 | |
| 366 | default: |
| 367 | /* other types cannot be subtracted */ |
| 368 | return FALSE; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Compute the difference for a DEC instruction |
| 374 | */ |
| 375 | const uchar *CVmRun::compute_diff_dec(VMG_ const uchar *p) |
| 376 | { |
| 377 | /* compute TOS - 1, leaving the result in TOS */ |
| 378 | vm_val_t val2; |
| 379 | val2.set_int(1); |
| 380 | if (compute_diff(vmg_ get(0), &val2)) |
| 381 | { |
| 382 | return p; |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | /* no native implementation - check for an overload */ |
| 387 | vm_val_t val; |
| 388 | pop(&val); |
| 389 | push(&val2); |
| 390 | return op_overload(vmg_ p - entry_ptr_native_, -1, |
| 391 | &val, G_predef->operator_sub, 1, |
| 392 | VMERR_BAD_TYPE_SUB); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Compute the difference for a SUB instruction |
| 398 | */ |
| 399 | const uchar *CVmRun::compute_diff_sub(VMG_ const uchar *p) |
| 400 | { |
| 401 | if (compute_diff(vmg_ get(1), get(0))) |
| 402 | { |
| 403 | /* the difference is at TOS-1 - discard the second value */ |
| 404 | discard(); |
| 405 | return p; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | /* try for an operator overload */ |
| 410 | vm_val_t val; |
| 411 | pop_left_op(vmg_ &val); |
| 412 | return op_overload(vmg_ p - entry_ptr_native_, -1, |
| 413 | &val, G_predef->operator_sub, 1, |
| 414 | VMERR_BAD_TYPE_SUB); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* ------------------------------------------------------------------------ */ |
| 419 | /* |
| 420 | * Compute the product val1 * val2, leaving the result in val1 |
| 421 | */ |
| 422 | int CVmRun::compute_product(VMG_ vm_val_t *val1, vm_val_t *val2) |
| 423 | { |
| 424 | check_type: |
| 425 | switch(val1->typ) |
| 426 | { |
| 427 | case VM_OBJ: |
| 428 | /* use the object's virtual multiplication method */ |
| 429 | return vm_objp(vmg_ val1->val.obj)->mul_val( |
| 430 | vmg_ val1, val1->val.obj, val2); |
| 431 | |
| 432 | case VM_INT: |
| 433 | /* callers handle int*int inline; check for other numeric types */ |
| 434 | if (val2->is_numeric(vmg0_)) |
| 435 | { |
| 436 | /* |
| 437 | * int+other numeric - promote the integer to the other type, |
| 438 | * then restart the calculation with the promoted value |
| 439 | */ |
| 440 | val2->promote_int(vmg_ val1); |
| 441 | goto check_type; |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | /* other types are invalid */ |
| 446 | err_throw(VMERR_NUM_VAL_REQD); |
| 447 | AFTER_ERR_THROW(return FALSE;) |
| 448 | } |
| 449 | |
| 450 | default: |
| 451 | /* other types are invalid */ |
| 452 | return FALSE; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* ------------------------------------------------------------------------ */ |
| 457 | /* |
| 458 | * Compute the quotient val1/val2, leaving the result in val1. |
| 459 | */ |
| 460 | int CVmRun::compute_quotient(VMG_ vm_val_t *val1, vm_val_t *val2) |
| 461 | { |
| 462 | check_type: |
| 463 | switch(val1->typ) |
| 464 | { |
| 465 | case VM_OBJ: |
| 466 | /* use the object's virtual division method */ |
| 467 | return vm_objp(vmg_ val1->val.obj)->div_val( |
| 468 | vmg_ val1, val1->val.obj, val2); |
| 469 | break; |
| 470 | |
| 471 | case VM_INT: |
| 472 | /* callers handle int/int inline; check for other numeric types */ |
| 473 | if (val2->is_numeric(vmg0_)) |
| 474 | { |
| 475 | /* promote to integer, then restart with the promoted value */ |
| 476 | val2->promote_int(vmg_ val1); |
| 477 | goto check_type; |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | err_throw(VMERR_NUM_VAL_REQD); |
| 482 | AFTER_ERR_THROW(return FALSE;) |
| 483 | } |
| 484 | |
| 485 | default: |
| 486 | /* other types are invalid */ |
| 487 | return FALSE; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* ------------------------------------------------------------------------ */ |
| 492 | /* |
| 493 | * XOR two values and push the result. The values can be numeric or |
| 494 | * logical. If either value is logical, the result will be logical; |
| 495 | * otherwise, the result will be a bitwise XOR of the integers. |
| 496 | */ |
| 497 | int CVmRun::xor_and_push(VMG_ vm_val_t *val1, vm_val_t *val2) |
| 498 | { |
| 499 | /* figure what to do based on the types */ |
| 500 | if (val1->is_logical() && val2->is_logical()) |
| 501 | { |
| 502 | /* both values are logical - compute the logical XOR */ |
| 503 | val1->set_logical(val1->get_logical() ^ val2->get_logical()); |
| 504 | } |
| 505 | else if (val1->is_logical() || val2->is_logical()) |
| 506 | { |
| 507 | /* |
| 508 | * one value is logical, but not both - convert the other value |
| 509 | * from a number to a logical and compute the result as a |
| 510 | * logical value |
| 511 | */ |
| 512 | if (!val1->is_logical()) |
| 513 | val1->num_to_logical(); |
| 514 | else if (!val2->is_logical()) |
| 515 | val2->num_to_logical(); |
| 516 | |
| 517 | /* compute the logical xor */ |
| 518 | val1->set_logical(val1->get_logical() ^ val2->get_logical()); |
| 519 | } |
| 520 | else if (val1->typ == VM_INT && val2->typ == VM_INT) |
| 521 | { |
| 522 | /* compute and store the bitwise XOR */ |
| 523 | val1->val.intval = val1->val.intval ^ val2->val.intval; |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | /* there's no logical conversion, so we can't compute it here */ |
| 528 | return FALSE; |
| 529 | } |
| 530 | |
| 531 | /* push the result */ |
| 532 | pushval(vmg_ val1); |
| 533 | |
| 534 | /* handled */ |
| 535 | return TRUE; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | /* ------------------------------------------------------------------------ */ |
| 540 | /* |
| 541 | * Set an indexed value. Updates *container_val with the modified |
| 542 | * container, if the operation requires this. (For example, setting an |
| 543 | * indexed element of a list will create a new list, and return the new |
| 544 | * list in *container_val. Setting an element of a vector simply modifies |
| 545 | * the vector in place, hence the container reference is unchanged.) |
| 546 | */ |
| 547 | int CVmRun::set_index(VMG_ vm_val_t *container_val, |
| 548 | const vm_val_t *index_val, |
| 549 | const vm_val_t *new_val) |
| 550 | { |
| 551 | switch(container_val->typ) |
| 552 | { |
| 553 | case VM_LIST: |
| 554 | /* list constant - use the static list set-index method */ |
| 555 | CVmObjList::set_index_list(vmg_ container_val, |
| 556 | get_const_ptr(vmg_ container_val->val.ofs), |
| 557 | index_val, new_val); |
| 558 | return TRUE; |
| 559 | |
| 560 | case VM_OBJ: |
| 561 | /* object - use the object's virtual set-index method */ |
| 562 | return vm_objp(vmg_ container_val->val.obj) |
| 563 | ->set_index_val_q(vmg_ container_val, |
| 564 | container_val->val.obj, index_val, new_val); |
| 565 | |
| 566 | default: |
| 567 | /* other values cannot be indexed */ |
| 568 | return FALSE; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /* ------------------------------------------------------------------------ */ |
| 573 | /* |
| 574 | * Create a new object and store it in R0 |
| 575 | */ |
| 576 | const uchar *CVmRun::new_and_store_r0(VMG_ const uchar *pc, |
| 577 | uint metaclass_idx, uint argc, |
| 578 | int is_transient) |
| 579 | { |
| 580 | vm_obj_id_t obj; |
| 581 | |
| 582 | /* create the object */ |
| 583 | obj = G_meta_table->create_from_stack(vmg_ &pc, metaclass_idx, argc); |
| 584 | |
| 585 | /* if we got a valid object, store a reference to it in R0 */ |
| 586 | if (obj != VM_INVALID_OBJ) |
| 587 | { |
| 588 | /* set the object return value */ |
| 589 | r0_.set_obj(obj); |
| 590 | |
| 591 | /* make the object transient if desired */ |
| 592 | if (is_transient) |
| 593 | G_obj_table->set_obj_transient(obj); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | /* failed - return nil */ |
| 598 | r0_.set_nil(); |
| 599 | } |
| 600 | |
| 601 | /* return the new instruction pointer */ |
| 602 | return pc; |
| 603 | } |
| 604 | |
| 605 | /* ------------------------------------------------------------------------ */ |
| 606 | /* |
| 607 | * Process a MAKELSTPAR instruction |
| 608 | */ |
| 609 | void CVmRun::makelstpar(VMG0_) |
| 610 | { |
| 611 | /* pop the value and the argument counter so far */ |
| 612 | vm_val_t val, val2; |
| 613 | popval(vmg_ &val); |
| 614 | pop_int(vmg_ &val2); |
| 615 | |
| 616 | /* if it's not a list, just push it again unchanged */ |
| 617 | int lstcnt; |
| 618 | if (!val.is_listlike(vmg0_) |
| 619 | || (lstcnt = val.ll_length(vmg0_)) < 0) |
| 620 | { |
| 621 | /* put it back on the stack */ |
| 622 | pushval(vmg_ &val); |
| 623 | |
| 624 | /* increment the argument count and push it */ |
| 625 | ++val2.val.intval; |
| 626 | pushval(vmg_ &val2); |
| 627 | |
| 628 | /* our work here is done */ |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | /* set up a pointer to the current function header */ |
| 633 | CVmFuncPtr hdr_ptr; |
| 634 | hdr_ptr.set(entry_ptr_native_); |
| 635 | |
| 636 | /* get the depth required for the header */ |
| 637 | uint hdr_depth = hdr_ptr.get_stack_depth(); |
| 638 | |
| 639 | /* |
| 640 | * deduct the amount stack space we've already used from the amount |
| 641 | * noted in the header, because that's the amount more that we could |
| 642 | * need for the fixed stuff |
| 643 | */ |
| 644 | hdr_depth -= (get_depth_rel(frame_ptr_) - 1); |
| 645 | |
| 646 | /* make sure we have enough stack space available */ |
| 647 | if (!check_space(lstcnt + hdr_depth)) |
| 648 | err_throw(VMERR_STACK_OVERFLOW); |
| 649 | |
| 650 | /* push the elements of the list from last to first */ |
| 651 | for (uint i = lstcnt ; i != 0 ; --i) |
| 652 | { |
| 653 | /* push this element's value */ |
| 654 | val.ll_index(vmg_ push(), i); |
| 655 | } |
| 656 | |
| 657 | /* increment and push the argument count */ |
| 658 | val2.val.intval += lstcnt; |
| 659 | pushval(vmg_ &val2); |
| 660 | } |
| 661 | |
| 662 | |
| 663 | /* ------------------------------------------------------------------------ */ |
| 664 | /* |
| 665 | * Index a value and push the result. |
| 666 | */ |
| 667 | int CVmRun::apply_index(VMG_ vm_val_t *result, |
| 668 | const vm_val_t *container_val, |
| 669 | const vm_val_t *index_val) |
| 670 | { |
| 671 | /* check the type of the value we're indexing */ |
| 672 | switch(container_val->typ) |
| 673 | { |
| 674 | case VM_LIST: |
| 675 | /* list constant - use the static list indexing method */ |
| 676 | CVmObjList::index_list( |
| 677 | vmg_ result, get_const_ptr(vmg_ container_val->val.ofs), |
| 678 | index_val); |
| 679 | return TRUE; |
| 680 | |
| 681 | case VM_OBJ: |
| 682 | /* object - use the object's virtual indexing method */ |
| 683 | { |
| 684 | vm_obj_id_t obj = container_val->val.obj; |
| 685 | return vm_objp(vmg_ obj)->index_val_q(vmg_ result, obj, index_val); |
| 686 | } |
| 687 | |
| 688 | default: |
| 689 | /* other values cannot be indexed */ |
| 690 | return FALSE; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* ------------------------------------------------------------------------ */ |
| 695 | /* |
| 696 | * Property evaluation invoker. This is a streamlined invoker for simple |
| 697 | * evaluations where targetobj == self. |
| 698 | */ |
| 699 | class vmrun_prop_eval |
| 700 | { |
| 701 | public: |
| 702 | /* caller offset */ |
| 703 | uint caller_ofs; |
| 704 | |
| 705 | /* target property */ |
| 706 | vm_prop_id_t target_prop; |
| 707 | |
| 708 | /* self and target object */ |
| 709 | vm_val_t self; |
| 710 | |
| 711 | /* number of arguments */ |
| 712 | uint argc; |
| 713 | |
| 714 | /* the property value */ |
| 715 | vm_val_t val; |
| 716 | |
| 717 | /* defining object */ |
| 718 | vm_obj_id_t defining_obj; |
| 719 | |
| 720 | const uchar *get_prop( |
| 721 | VMG_ uint caller_ofs, vm_obj_id_t self, vm_prop_id_t target_prop) |
| 722 | { |
| 723 | this->self.set_obj(self); |
| 724 | this->caller_ofs = caller_ofs; |
| 725 | this->argc = 0; |
| 726 | this->target_prop = target_prop; |
| 727 | return get_prop(vmg0_); |
| 728 | } |
| 729 | |
| 730 | const uchar *get_prop( |
| 731 | VMG_ uint caller_ofs, vm_prop_id_t target_prop) |
| 732 | { |
| 733 | this->caller_ofs = caller_ofs; |
| 734 | this->argc = 0; |
| 735 | this->target_prop = target_prop; |
| 736 | return get_prop(vmg0_); |
| 737 | } |
| 738 | |
| 739 | const uchar *get_prop( |
| 740 | VMG_ uint caller_ofs, vm_prop_id_t target_prop, uint argc) |
| 741 | { |
| 742 | this->caller_ofs = caller_ofs; |
| 743 | this->argc = argc; |
| 744 | this->target_prop = target_prop; |
| 745 | return get_prop(vmg0_); |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Evaluate a property of an object. The caller must fill in |
| 750 | * caller_ofs, target_obj, target_prop, self, and argc. |
| 751 | */ |
| 752 | const uchar *get_prop(VMG0_) |
| 753 | { |
| 754 | /* find the property without evaluating it */ |
| 755 | if (get_prop_no_eval(vmg0_)) |
| 756 | return eval_prop_val(vmg0_); |
| 757 | |
| 758 | /* try propNotDefined */ |
| 759 | if (G_predef->prop_not_defined_prop != VM_INVALID_PROP) |
| 760 | { |
| 761 | /* save the original target property */ |
| 762 | vm_prop_id_t real_target_prop = target_prop; |
| 763 | |
| 764 | /* look up propNotDefined */ |
| 765 | target_prop = G_predef->prop_not_defined_prop; |
| 766 | if (get_prop_no_eval(vmg0_)) |
| 767 | { |
| 768 | /* if we found a method, set up to call it */ |
| 769 | if (val.typ == VM_CODEOFS |
| 770 | || val.typ == VM_OBJX |
| 771 | || val.typ == VM_BIFPTRX) |
| 772 | { |
| 773 | /* |
| 774 | * add the target property as the additional first |
| 775 | * argument to propNotDefined (we push backwards, so |
| 776 | * this will conveniently become the new first |
| 777 | * argument) |
| 778 | */ |
| 779 | G_stk->push()->set_propid(real_target_prop); |
| 780 | |
| 781 | /* count the additional argument */ |
| 782 | ++argc; |
| 783 | } |
| 784 | |
| 785 | return eval_prop_val(vmg0_); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * the property or method is not defined - discard arguments and |
| 791 | * set R0 to nil |
| 792 | */ |
| 793 | G_stk->discard(argc); |
| 794 | G_interpreter->get_r0()->set_nil(); |
| 795 | |
| 796 | /* resume execution where we left off */ |
| 797 | return G_interpreter->get_entry_ptr() + caller_ofs; |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * Look up a property without evaluating it. |
| 802 | */ |
| 803 | int get_prop_no_eval(VMG0_) |
| 804 | { |
| 805 | int found; |
| 806 | const char *target_ptr; |
| 807 | int (*f_const_get_prop)(VMG_ vm_val_t *, const vm_val_t *, |
| 808 | const char *, vm_prop_id_t, vm_obj_id_t *, |
| 809 | uint *); |
| 810 | vm_obj_id_t (*f_const_create)(VMG_ const char *); |
| 811 | |
| 812 | /* |
| 813 | * we can evaluate properties of regular objects, as well as string |
| 814 | * and list constants - see what we have |
| 815 | */ |
| 816 | switch(self.typ) |
| 817 | { |
| 818 | case VM_OBJ: |
| 819 | /* get the property value from the target object */ |
| 820 | return vm_objp(vmg_ self.val.obj) |
| 821 | ->get_prop(vmg_ target_prop, &val, self.val.obj, |
| 822 | &defining_obj, &argc); |
| 823 | |
| 824 | case VM_LIST: |
| 825 | f_const_get_prop = &CVmObjList::const_get_prop; |
| 826 | f_const_create = &CVmObjListConst::create; |
| 827 | goto const_common; |
| 828 | |
| 829 | case VM_SSTRING: |
| 830 | f_const_get_prop = &CVmObjString::const_get_prop; |
| 831 | f_const_create = &CVmObjStringConst::create; |
| 832 | |
| 833 | const_common: |
| 834 | /* translate the list offset to a physical pointer */ |
| 835 | target_ptr = G_const_pool->get_ptr(self.val.ofs); |
| 836 | |
| 837 | /* evaluate the constant list property */ |
| 838 | found = f_const_get_prop( |
| 839 | vmg_ &val, &self, target_ptr, target_prop, |
| 840 | &defining_obj, &argc); |
| 841 | |
| 842 | /* |
| 843 | * If the result is a method to run, we need an actual object |
| 844 | * for 'self'. In this case, create a dynamic list object with |
| 845 | * the same contents as the constant list value. |
| 846 | */ |
| 847 | if (found |
| 848 | && (val.typ == VM_CODEOFS |
| 849 | || val.typ == VM_OBJX |
| 850 | || val.typ == VM_BIFPTRX)) |
| 851 | { |
| 852 | /* create the list */ |
| 853 | self.set_obj(f_const_create(vmg_ target_ptr)); |
| 854 | } |
| 855 | |
| 856 | /* go evaluate the result as normal */ |
| 857 | return found; |
| 858 | |
| 859 | case VM_NIL: |
| 860 | /* nil pointer dereferenced */ |
| 861 | err_throw(VMERR_NIL_DEREF); |
| 862 | AFTER_ERR_THROW(return FALSE;) |
| 863 | |
| 864 | default: |
| 865 | /* we can't evaluate properties of anything else */ |
| 866 | err_throw(VMERR_OBJ_VAL_REQD); |
| 867 | AFTER_ERR_THROW(return FALSE;) |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * Given a value that has been retrieved from an object property, |
| 873 | * evaluate the value. If the value contains code, we'll execute the |
| 874 | * code; if it contains a self-printing string, we'll display the |
| 875 | * string; otherwise, we'll just store the value in R0. |
| 876 | * |
| 877 | * If the value wasn't found, the caller sets the value type to 'empty' |
| 878 | * to indicate that there's no value. |
| 879 | */ |
| 880 | const uchar *eval_prop_val(VMG0_) |
| 881 | { |
| 882 | /* take appropriate action based on the datatype of the result */ |
| 883 | switch(val.typ) |
| 884 | { |
| 885 | case VM_CODEOFS: |
| 886 | /* |
| 887 | * It's a method - invoke the method. This will set us up to |
| 888 | * start executing this new code, so there's nothing more we |
| 889 | * need to do here. |
| 890 | */ |
| 891 | |
| 892 | /* push targetprop, targetobj, definingobj, self, and invokee */ |
| 893 | { |
| 894 | vm_val_t *fp = G_stk->push(5); |
| 895 | (fp++)->set_propid(target_prop); |
| 896 | (fp++)->set_obj(self.val.obj); |
| 897 | (fp++)->set_obj(defining_obj); |
| 898 | (fp++)->set_obj(self.val.obj); |
| 899 | (fp++)->set_fnptr(val.val.ofs); |
| 900 | } |
| 901 | |
| 902 | /* call the function */ |
| 903 | return G_interpreter->do_call( |
| 904 | vmg_ caller_ofs, |
| 905 | (const uchar *)G_code_pool->get_ptr(val.val.ofs), |
| 906 | argc, 0); |
| 907 | |
| 908 | default: |
| 909 | /* for any other value, no arguments are allowed */ |
| 910 | if (argc != 0) |
| 911 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 912 | |
| 913 | /* store the result in R0 */ |
| 914 | *G_interpreter->get_r0() = val; |
| 915 | |
| 916 | /* resume execution where we left off */ |
| 917 | return G_interpreter->get_entry_ptr() + caller_ofs; |
| 918 | |
| 919 | case VM_DSTRING: |
| 920 | /* no arguments are allowed */ |
| 921 | if (argc != 0) |
| 922 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 923 | |
| 924 | /* |
| 925 | * it's a self-printing string - invoke the default string |
| 926 | * output function (this is effectively a do_call()) |
| 927 | */ |
| 928 | return G_interpreter->disp_dstring( |
| 929 | vmg_ val.val.ofs, caller_ofs, self.val.obj); |
| 930 | |
| 931 | case VM_OBJX: |
| 932 | /* |
| 933 | * Execute-on-eval object. If the value is an anonymous |
| 934 | * function or other invokable object, call it. If it's a |
| 935 | * string, print it. |
| 936 | */ |
| 937 | if (vm_objp(vmg_ val.val.obj)->get_invoker(vmg_ 0)) |
| 938 | { |
| 939 | /* convert to an ordinary anonymous function object */ |
| 940 | vm_val_t funcptr; |
| 941 | funcptr.set_obj(val.val.obj); |
| 942 | |
| 943 | /* prepare the invocation frame */ |
| 944 | vm_val_t *fp = G_stk->push(5); |
| 945 | (fp++)->set_propid(target_prop); |
| 946 | (fp++)->set_obj(self.val.obj); |
| 947 | (fp++)->set_obj(defining_obj); |
| 948 | (fp++)->set_obj(self.val.obj); |
| 949 | (fp++)->set_obj(val.val.obj); |
| 950 | |
| 951 | /* invoke the function */ |
| 952 | return G_interpreter->call_func_ptr_fr( |
| 953 | vmg_ &funcptr, argc, 0, caller_ofs); |
| 954 | } |
| 955 | else if (CVmObjString::is_string_obj(vmg_ val.val.obj)) |
| 956 | { |
| 957 | /* print the string */ |
| 958 | G_interpreter->push_obj(vmg_ val.val.obj); |
| 959 | return G_interpreter->disp_string_val( |
| 960 | vmg_ caller_ofs, self.val.obj); |
| 961 | } |
| 962 | err_throw(VMERR_BAD_TYPE_CALL); |
| 963 | |
| 964 | case VM_BIFPTRX: |
| 965 | /* Execute-on-eval built-in function. Call the function. */ |
| 966 | G_interpreter->call_bif( |
| 967 | vmg_ val.val.bifptr.set_idx, val.val.bifptr.func_idx, argc); |
| 968 | |
| 969 | /* resume execution where we left off */ |
| 970 | return G_interpreter->get_entry_ptr() + caller_ofs; |
| 971 | } |
| 972 | } |
| 973 | }; |
| 974 | |
| 975 | /* ------------------------------------------------------------------------ */ |
| 976 | /* |
| 977 | * Integer arithmetic with promotions to BigNumber on overflow |
| 978 | */ |
| 979 | |
| 980 | #define VMRUN_PROMOTE_INTS_ON_OVERFLOW |
| 981 | #ifdef VMRUN_PROMOTE_INTS_ON_OVERFLOW |
| 982 | |
| 983 | static void promote_int_add(VMG_ vm_val_t *aval, int32_t b) |
| 984 | { |
| 985 | bignum_t<10> sum((long)aval->val.intval); |
| 986 | sum += (long)b; |
| 987 | aval->set_obj(CVmObjBigNum::create(vmg_ FALSE, sum)); |
| 988 | } |
| 989 | |
| 990 | static inline void int_add(VMG_ vm_val_t *aval, int32_t b) |
| 991 | { |
| 992 | int32_t a = aval->val.intval, sum = a + b; |
| 993 | if (a >= 0 ? b <= 0 || sum >= a : b >= 0 || sum <= a) |
| 994 | aval->val.intval = sum; |
| 995 | else |
| 996 | promote_int_add(vmg_ aval, b); |
| 997 | } |
| 998 | |
| 999 | static void promote_int_sub(VMG_ vm_val_t *aval, int32_t b) |
| 1000 | { |
| 1001 | bignum_t<10> diff((long)aval->val.intval); |
| 1002 | diff -= (long)b; |
| 1003 | aval->set_obj(CVmObjBigNum::create(vmg_ FALSE, diff)); |
| 1004 | } |
| 1005 | |
| 1006 | static inline void int_sub(VMG_ vm_val_t *aval, int32_t b) |
| 1007 | { |
| 1008 | int32_t a = aval->val.intval, diff = a - b; |
| 1009 | if (a >= 0 ? b >= 0 || diff >= a : b < 0 || diff <= a) |
| 1010 | aval->val.intval = diff; |
| 1011 | else |
| 1012 | promote_int_sub(vmg_ aval, b); |
| 1013 | } |
| 1014 | |
| 1015 | static void promote_int_mul(VMG_ vm_val_t *aval, int32_t b) |
| 1016 | { |
| 1017 | bignum_t<20> prod((long)aval->val.intval); |
| 1018 | prod *= (long)b; |
| 1019 | aval->set_obj(CVmObjBigNum::create(vmg_ FALSE, prod)); |
| 1020 | } |
| 1021 | |
| 1022 | static inline void int_mul(VMG_ vm_val_t *aval, int32_t b) |
| 1023 | { |
| 1024 | int64_t a = aval->val.intval, prod = a * b; |
| 1025 | if (prod <= (int64_t)INT32MAXVAL && prod >= (int64_t)INT32MINVAL) |
| 1026 | aval->val.intval = (int32_t)prod; |
| 1027 | else |
| 1028 | promote_int_mul(vmg_ aval, b); |
| 1029 | } |
| 1030 | |
| 1031 | static void promote_int_div(VMG_ vm_val_t *aval, int32_t b) |
| 1032 | { |
| 1033 | bignum_t<10> quo((long)aval->val.intval); |
| 1034 | quo /= (long)b; |
| 1035 | aval->set_obj(CVmObjBigNum::create(vmg_ FALSE, quo)); |
| 1036 | } |
| 1037 | |
| 1038 | static inline void int_div(VMG_ vm_val_t *aval, int32_t b) |
| 1039 | { |
| 1040 | /* check for division by zero */ |
| 1041 | if (b == 0) |
| 1042 | err_throw(VMERR_DIVIDE_BY_ZERO); |
| 1043 | |
| 1044 | /* |
| 1045 | * NB - assuming 2's complement notation; the only integer division |
| 1046 | * that can result in overflow is INT32MINVAL/-1 |
| 1047 | */ |
| 1048 | int32_t a = aval->val.intval; |
| 1049 | if (a != INT32MINVAL || b != -1) |
| 1050 | aval->val.intval = a / b; |
| 1051 | else |
| 1052 | promote_int_div(vmg_ aval, b); |
| 1053 | } |
| 1054 | |
| 1055 | static void promote_int_neg(VMG_ vm_val_t *aval) |
| 1056 | { |
| 1057 | bignum_t<10> neg((long)aval->val.intval); |
| 1058 | neg = -neg; |
| 1059 | aval->set_obj(CVmObjBigNum::create(vmg_ FALSE, neg)); |
| 1060 | } |
| 1061 | |
| 1062 | static inline void int_neg(VMG_ vm_val_t *aval) |
| 1063 | { |
| 1064 | /* |
| 1065 | * NB - assuming 2's complement representation; the only integer |
| 1066 | * negation that can result in overflow is -INT32MINVAL |
| 1067 | */ |
| 1068 | if (aval->val.intval != INT32MINVAL) |
| 1069 | aval->val.intval = -aval->val.intval; |
| 1070 | else |
| 1071 | promote_int_neg(vmg_ aval); |
| 1072 | } |
| 1073 | |
| 1074 | #define INT_ADD(aval, b) int_add(vmg_ aval, b) |
| 1075 | #define INT_SUB(aval, b) int_sub(vmg_ aval, b) |
| 1076 | #define INT_MUL(aval, b) int_mul(vmg_ aval, b) |
| 1077 | #define INT_DIV(aval, b) int_div(vmg_ aval, b) |
| 1078 | #define INT_NEG(aval) int_neg(vmg_ aval) |
| 1079 | |
| 1080 | #else |
| 1081 | |
| 1082 | #define INT_ADD(aval, b) ((aval)->val.intval += (b)) |
| 1083 | #define INT_SUB(aval, b) ((aval)->val.intval -= (b)) |
| 1084 | #define INT_MUL(aval, b) ((aval)->val.intval *= (b)) |
| 1085 | #define INT_DIV(aval, b) \ |
| 1086 | if (b == 0) err_throw(VMERR_DIVIDE_BY_ZERO); \ |
| 1087 | else ((aval)->val.intval /= (b)) |
| 1088 | #define INT_NEG(aval) ((aval)->val.intval = -(aval)->val.intval) |
| 1089 | |
| 1090 | #endif |
| 1091 | |
| 1092 | |
| 1093 | /* ------------------------------------------------------------------------ */ |
| 1094 | /* |
| 1095 | * Calculations for conditionals |
| 1096 | */ |
| 1097 | #if 1 |
| 1098 | # define false_for_cond(v) \ |
| 1099 | ((v)->typ == VM_NIL || ((v)->typ == VM_INT && (v)->val.intval == 0)) |
| 1100 | # define true_for_cond(v) \ |
| 1101 | ((v)->typ == VM_TRUE \ |
| 1102 | || (v)->typ == VM_ENUM \ |
| 1103 | || ((v)->typ == VM_INT && !(v)->val.intval == 0)) |
| 1104 | # define is_valid_for_jst(v) \ |
| 1105 | ((v)->typ == VM_NIL || (v)->typ == VM_INT) |
| 1106 | #else |
| 1107 | # define false_for_cond(v) \ |
| 1108 | ((v)->typ == VM_NIL \ |
| 1109 | || ((v)->is_numeric(vmg0_) && (v)->num_is_zero(vmg0_))) |
| 1110 | # define true_for_cond(v) \ |
| 1111 | ((v)->typ == VM_TRUE \ |
| 1112 | || (v)->typ == VM_ENUM \ |
| 1113 | || ((v)->is_numeric(vmg0_) && !(v)->num_is_zero(vmg0_))) |
| 1114 | # define is_valid_for_jst(v) \ |
| 1115 | ((v)->typ == VM_NIL || (v)->is_numeric(vmg0_)) |
| 1116 | #endif |
| 1117 | |
| 1118 | |
| 1119 | /* ------------------------------------------------------------------------ */ |
| 1120 | /* |
| 1121 | * Execute byte code |
| 1122 | */ |
| 1123 | void CVmRun::run(VMG_ const uchar *start_pc) |
| 1124 | { |
| 1125 | /* |
| 1126 | * If you're concerned about a compiler warning on the following |
| 1127 | * 'register' declaration, refer to the footnote at the bottom of this |
| 1128 | * file (search for [REGISTER_P_FOOTNOTE]). Executive summary: you can |
| 1129 | * safely ignore the warning, and I'm keeping the code as it is because |
| 1130 | * it causes better optimization on some platforms, and is harmless |
| 1131 | * when it doesn't help with optimization. |
| 1132 | */ |
| 1133 | register const uchar *p = start_pc; |
| 1134 | vmrun_prop_eval propev; |
| 1135 | vm_val_t *valp; |
| 1136 | vm_val_t *valp2; |
| 1137 | vm_val_t val; |
| 1138 | vm_val_t val2; |
| 1139 | vm_val_t val3; |
| 1140 | int done; |
| 1141 | vm_obj_id_t obj; |
| 1142 | vm_prop_id_t prop; |
| 1143 | uint argc; |
| 1144 | vm_obj_id_t unhandled_exc; |
| 1145 | const uchar *last_pc = start_pc; |
| 1146 | const uchar **old_pc_ptr; |
| 1147 | |
| 1148 | /* save the enclosing program counter pointer, and remember the new one */ |
| 1149 | old_pc_ptr = pc_ptr_; |
| 1150 | pc_ptr_ = &last_pc; |
| 1151 | |
| 1152 | /* we're not done yet */ |
| 1153 | done = FALSE; |
| 1154 | |
| 1155 | /* no unhandled exception yet */ |
| 1156 | unhandled_exc = VM_INVALID_OBJ; |
| 1157 | |
| 1158 | /* |
| 1159 | * Come back here whenever we catch a run-time exception and find a |
| 1160 | * byte-code error handler to process it in the stack. We'll |
| 1161 | * re-enter our exception handler and resume byte-code execution at |
| 1162 | * the handler. |
| 1163 | */ |
| 1164 | resume_execution: |
| 1165 | |
| 1166 | /* |
| 1167 | * Execute all code within an exception frame. If any routine we |
| 1168 | * call throws an exception, we'll catch the exception and process |
| 1169 | * it as a run-time error. |
| 1170 | */ |
| 1171 | err_try |
| 1172 | { |
| 1173 | /* execute code until something makes us stop */ |
| 1174 | for (;;) |
| 1175 | { |
| 1176 | #ifdef VM_DEBUGGER |
| 1177 | /* |
| 1178 | * check for user-requested break, and step into the debugger |
| 1179 | * if we find it |
| 1180 | */ |
| 1181 | static int brkchk = 0; |
| 1182 | if (++brkchk > 10000) |
| 1183 | { |
| 1184 | /* reset the break counter */ |
| 1185 | brkchk = 0; |
| 1186 | |
| 1187 | /* check for break, and step into debugger if found */ |
| 1188 | if (os_break()) |
| 1189 | { |
| 1190 | if (G_debugger->is_in_debugger()) |
| 1191 | err_throw(VMERR_DBG_INTERRUPT); |
| 1192 | else |
| 1193 | G_debugger->set_break_stop(); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | /* if we're single-stepping, break into the debugger */ |
| 1198 | if (G_debugger->is_single_step()) |
| 1199 | G_debugger->step(vmg_ &p, entry_ptr_native_, FALSE, 0); |
| 1200 | |
| 1201 | /* check for a halt request from the debugger */ |
| 1202 | if (halt_vm_) |
| 1203 | err_throw(VMERR_DBG_HALT); |
| 1204 | |
| 1205 | exec_instruction: |
| 1206 | |
| 1207 | #endif /* VM_DEBUGGER */ |
| 1208 | |
| 1209 | /* |
| 1210 | * Remember the location of this instruction in a non-register |
| 1211 | * variable, in case there's an exception. (We know that |
| 1212 | * last_pc is guaranteed to be a non-register variable because |
| 1213 | * we take its address and store it in our pc_ptr_ member.) |
| 1214 | * |
| 1215 | * We need to know the location of the last instruction when |
| 1216 | * an exception occurs so that we can find the exception |
| 1217 | * handler. We want to encourage the compiler to enregister |
| 1218 | * 'p', since we access it so frequently in this routine; but |
| 1219 | * if it's in a register, there's a risk we'd get the |
| 1220 | * setjmp-time value in our exception handler. To handle both |
| 1221 | * needs, simply copy the value to our non-register variable |
| 1222 | * last_pc; this will still let the vast majority of our |
| 1223 | * access to 'p' use fast register operations if the compiler |
| 1224 | * allows this, while ensuring we have a safe copy around in |
| 1225 | * case of exceptions. |
| 1226 | */ |
| 1227 | last_pc = p; |
| 1228 | |
| 1229 | /* |
| 1230 | * Execute the current instruction. |
| 1231 | * |
| 1232 | * The order of the case table is in descending order of |
| 1233 | * frequency of instruction usage for the first 30 or so |
| 1234 | * instructions, according to empirical testing. This is |
| 1235 | * intended to keep the code for the most common instructions |
| 1236 | * within a short jump of the 'switch', to increase the chances |
| 1237 | * that an instruction jump will go to a location in cache. |
| 1238 | * This seems to yield a slight speed improvement on Intel |
| 1239 | * machines, and even if it doesn't help on a given machine, it |
| 1240 | * shouldn't do any harm relative to a randomly ordered case |
| 1241 | * table. |
| 1242 | */ |
| 1243 | switch(*p++) |
| 1244 | { |
| 1245 | case OPC_GETARGN0: |
| 1246 | push(get_param(vmg_ 0)); |
| 1247 | continue; |
| 1248 | |
| 1249 | case OPC_GETPROPSELF: |
| 1250 | /* evaluate the property of 'self' */ |
| 1251 | prop = get_op_uint16(&p); |
| 1252 | p = propev.get_prop( |
| 1253 | vmg_ p - entry_ptr_native_, get_self(vmg0_), prop); |
| 1254 | continue; |
| 1255 | |
| 1256 | case OPC_GETR0: |
| 1257 | /* push the contents of R0 */ |
| 1258 | push(&r0_); |
| 1259 | continue; |
| 1260 | |
| 1261 | case OPC_DUPR0: |
| 1262 | /* push the contents of R0 twice */ |
| 1263 | push(&r0_); |
| 1264 | push(&r0_); |
| 1265 | continue; |
| 1266 | |
| 1267 | case OPC_GETSETLCL1R0: |
| 1268 | /* set local from R0 and leave value on stack */ |
| 1269 | push(&r0_); |
| 1270 | *get_local(vmg_ get_op_uint8(&p)) = r0_; |
| 1271 | continue; |
| 1272 | |
| 1273 | case OPC_GETSETLCL1: |
| 1274 | /* set local and leave value on stack */ |
| 1275 | *get_local(vmg_ get_op_uint8(&p)) = *get(0); |
| 1276 | continue; |
| 1277 | |
| 1278 | case OPC_SETPROPSELF: |
| 1279 | /* get the value to set */ |
| 1280 | pop(&val); |
| 1281 | |
| 1282 | /* set it */ |
| 1283 | set_prop(vmg_ get_self(vmg0_), get_op_uint16(&p), &val); |
| 1284 | continue; |
| 1285 | |
| 1286 | case OPC_SETLCL1R0: |
| 1287 | /* store R0 in the specific local */ |
| 1288 | *get_local(vmg_ get_op_uint8(&p)) = r0_; |
| 1289 | continue; |
| 1290 | |
| 1291 | case OPC_GETARGN1: |
| 1292 | push(get_param(vmg_ 1)); |
| 1293 | continue; |
| 1294 | |
| 1295 | case OPC_GETLCLN0: |
| 1296 | push(get_local(vmg_ 0)); |
| 1297 | continue; |
| 1298 | |
| 1299 | case OPC_SETLCL1: |
| 1300 | /* get a pointer to the local */ |
| 1301 | valp = get_local(vmg_ get_op_uint8(&p)); |
| 1302 | |
| 1303 | /* pop the value into the local */ |
| 1304 | pop(valp); |
| 1305 | continue; |
| 1306 | |
| 1307 | case OPC_PUSHSELF: |
| 1308 | /* push 'self' */ |
| 1309 | push(get_self_val(vmg0_)); |
| 1310 | continue; |
| 1311 | |
| 1312 | case OPC_RETNIL: |
| 1313 | /* store nil in R0 */ |
| 1314 | r0_.set_nil(); |
| 1315 | |
| 1316 | /* return */ |
| 1317 | if ((p = do_return(vmg0_)) == 0) |
| 1318 | goto exit_loop; |
| 1319 | continue; |
| 1320 | |
| 1321 | case OPC_RETVAL: |
| 1322 | /* pop the return value into R0 */ |
| 1323 | pop(&r0_); |
| 1324 | |
| 1325 | /* return */ |
| 1326 | if ((p = do_return(vmg0_)) == 0) |
| 1327 | goto exit_loop; |
| 1328 | continue; |
| 1329 | |
| 1330 | case OPC_GETPROPLCL1: |
| 1331 | /* get the local whose property we're evaluating */ |
| 1332 | propev.self = *get_local(vmg_ get_op_uint8(&p)); |
| 1333 | |
| 1334 | /* evaluate the property of the local variable */ |
| 1335 | prop = get_op_uint16(&p); |
| 1336 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop); |
| 1337 | continue; |
| 1338 | |
| 1339 | case OPC_JNIL: |
| 1340 | /* jump if top of stack is nil */ |
| 1341 | valp = get(0); |
| 1342 | p += (valp->typ == VM_NIL ? osrp2s(p) : 2); |
| 1343 | |
| 1344 | /* discard the top value, regardless of what happened */ |
| 1345 | discard(); |
| 1346 | continue; |
| 1347 | |
| 1348 | case OPC_RET: |
| 1349 | /* return, leaving R0 unchanged */ |
| 1350 | if ((p = do_return(vmg0_)) == 0) |
| 1351 | goto exit_loop; |
| 1352 | continue; |
| 1353 | |
| 1354 | case OPC_PUSHENUM: |
| 1355 | /* push a UINT4 operand value */ |
| 1356 | push()->set_enum(get_op_uint32(&p)); |
| 1357 | continue; |
| 1358 | |
| 1359 | case OPC_JMP: |
| 1360 | /* unconditionally jump to the given offset */ |
| 1361 | p += osrp2s(p); |
| 1362 | continue; |
| 1363 | |
| 1364 | case OPC_JNE: |
| 1365 | /* jump if the two values at top of stack are not equal */ |
| 1366 | p += (!pop2_equal(vmg0_) ? osrp2s(p) : 2); |
| 1367 | continue; |
| 1368 | |
| 1369 | case OPC_JR0F: |
| 1370 | /* |
| 1371 | * if R0 is true, or it's a non-zero numeric value, or any |
| 1372 | * non-numeric and non-boolean value, stay put; otherwise, |
| 1373 | * jump |
| 1374 | */ |
| 1375 | if (false_for_cond(&r0_)) |
| 1376 | { |
| 1377 | /* it's zero or nil - jump */ |
| 1378 | p += osrp2s(p); |
| 1379 | } |
| 1380 | else |
| 1381 | { |
| 1382 | /* it's non-zero and non-nil - do not jump */ |
| 1383 | p += 2; |
| 1384 | } |
| 1385 | continue; |
| 1386 | |
| 1387 | case OPC_GETARGN2: |
| 1388 | push(get_param(vmg_ 2)); |
| 1389 | continue; |
| 1390 | |
| 1391 | case OPC_JGT: |
| 1392 | /* jump if greater */ |
| 1393 | p += (pop2_compare_gt(vmg0_) ? osrp2s(p) : 2); |
| 1394 | continue; |
| 1395 | |
| 1396 | case OPC_CALLPROPSELF: |
| 1397 | /* get the argument count */ |
| 1398 | argc = get_op_uint8(&p); |
| 1399 | |
| 1400 | do_opc_callpropself: |
| 1401 | /* evaluate the property of 'self' */ |
| 1402 | propev.self.set_obj(get_self(vmg0_)); |
| 1403 | prop = get_op_uint16(&p); |
| 1404 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop, argc); |
| 1405 | continue; |
| 1406 | |
| 1407 | case OPC_INDEX: |
| 1408 | /* index TOS-1 by TOS, storing the result at TOS-1 */ |
| 1409 | valp = get(1); |
| 1410 | if (apply_index(vmg_ valp, valp, get(0))) |
| 1411 | { |
| 1412 | /* discard the index value */ |
| 1413 | discard(); |
| 1414 | } |
| 1415 | else |
| 1416 | { |
| 1417 | /* try an operator overload */ |
| 1418 | pop_left_op(vmg_ &val); |
| 1419 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1420 | &val, G_predef->operator_idx, 1, |
| 1421 | VMERR_CANNOT_INDEX_TYPE); |
| 1422 | } |
| 1423 | continue; |
| 1424 | |
| 1425 | case OPC_DUP: |
| 1426 | /* re-push the item at top of stack */ |
| 1427 | push(get(0)); |
| 1428 | continue; |
| 1429 | |
| 1430 | case OPC_IDXLCL1INT8: |
| 1431 | /* get the local */ |
| 1432 | valp = get_local(vmg_ get_op_uint8(&p)); |
| 1433 | |
| 1434 | /* get the index value */ |
| 1435 | val2.set_int(get_op_uint8(&p)); |
| 1436 | |
| 1437 | /* |
| 1438 | * look up the indexed value of the local, storing the |
| 1439 | * result in a newly-pushed stack element |
| 1440 | */ |
| 1441 | if (!apply_index(vmg_ push(), valp, &val2)) |
| 1442 | { |
| 1443 | /* |
| 1444 | * try an operator overload - push the index value as |
| 1445 | * the argument |
| 1446 | */ |
| 1447 | push(&val2); |
| 1448 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1449 | valp, G_predef->operator_idx, 1, |
| 1450 | VMERR_CANNOT_INDEX_TYPE); |
| 1451 | } |
| 1452 | continue; |
| 1453 | |
| 1454 | case OPC_GETLCLN2: |
| 1455 | push(get_local(vmg_ 2)); |
| 1456 | continue; |
| 1457 | |
| 1458 | case OPC_CALLPROP: |
| 1459 | /* get the argument count */ |
| 1460 | argc = get_op_uint8(&p); |
| 1461 | |
| 1462 | do_opc_callprop: |
| 1463 | /* pop the object whose property we're fetching */ |
| 1464 | pop(&propev.self); |
| 1465 | |
| 1466 | /* evaluate the property given by the immediate data */ |
| 1467 | prop = get_op_uint16(&p); |
| 1468 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop, argc); |
| 1469 | continue; |
| 1470 | |
| 1471 | case OPC_GETLCLN1: |
| 1472 | push(get_local(vmg_ 1)); |
| 1473 | continue; |
| 1474 | |
| 1475 | case OPC_GETARGN3: |
| 1476 | push(get_param(vmg_ 3)); |
| 1477 | continue; |
| 1478 | |
| 1479 | case OPC_GETLCLN3: |
| 1480 | push(get_local(vmg_ 3)); |
| 1481 | continue; |
| 1482 | |
| 1483 | case OPC_JNOTNIL: |
| 1484 | /* jump if top of stack is not nil */ |
| 1485 | valp = get(0); |
| 1486 | p += (valp->typ != VM_NIL ? osrp2s(p) : 2); |
| 1487 | |
| 1488 | /* discard the top value, regardless of what happened */ |
| 1489 | discard(); |
| 1490 | continue; |
| 1491 | |
| 1492 | case OPC_ITERNEXT: |
| 1493 | /* get the iterator object from the local */ |
| 1494 | valp = get_local(vmg_ get_op_uint16(&p)); |
| 1495 | |
| 1496 | /* get the next value from the iterator */ |
| 1497 | if (valp->typ == VM_OBJ |
| 1498 | && vm_objp(vmg_ valp->val.obj)->iter_next( |
| 1499 | vmg_ valp->val.obj, &val)) |
| 1500 | { |
| 1501 | /* another value is available - push it and proceed */ |
| 1502 | push(&val); |
| 1503 | p += 2; |
| 1504 | } |
| 1505 | else |
| 1506 | { |
| 1507 | /* no more values available - exit the loop */ |
| 1508 | p += osrp2s(p); |
| 1509 | } |
| 1510 | break; |
| 1511 | |
| 1512 | case OPC_PUSH_0: |
| 1513 | /* push the constant value 0 */ |
| 1514 | push()->set_int(0); |
| 1515 | continue; |
| 1516 | |
| 1517 | case OPC_GETPROP: |
| 1518 | /* get the object whose property we're fetching */ |
| 1519 | pop(&propev.self); |
| 1520 | |
| 1521 | /* evaluate the property */ |
| 1522 | prop = get_op_uint16(&p); |
| 1523 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop); |
| 1524 | continue; |
| 1525 | |
| 1526 | case OPC_GETLCLN4: |
| 1527 | push(get_local(vmg_ 4)); |
| 1528 | continue; |
| 1529 | |
| 1530 | case OPC_JE: |
| 1531 | /* jump if the two values at top of stack are equal */ |
| 1532 | p += (pop2_equal(vmg0_) ? osrp2s(p) : 2); |
| 1533 | continue; |
| 1534 | |
| 1535 | /* |
| 1536 | * End of case table sorting by instruction execution |
| 1537 | * frequency. The order of the remainder is more or less |
| 1538 | * arbitrary; we get diminishing returns from the frequency |
| 1539 | * ordering past a certain point, because CPU caches are |
| 1540 | * only so large. |
| 1541 | */ |
| 1542 | |
| 1543 | case OPC_PUSHNIL: |
| 1544 | /* push nil */ |
| 1545 | push()->set_nil(); |
| 1546 | continue; |
| 1547 | |
| 1548 | case OPC_PUSHTRUE: |
| 1549 | /* push true */ |
| 1550 | push()->set_true(); |
| 1551 | continue; |
| 1552 | |
| 1553 | case OPC_PUSH_1: |
| 1554 | /* push the constant value 1 */ |
| 1555 | push()->set_int(1); |
| 1556 | continue; |
| 1557 | |
| 1558 | case OPC_PUSHINT8: |
| 1559 | /* push an SBYTE operand value */ |
| 1560 | push()->set_int(get_op_int8(&p)); |
| 1561 | continue; |
| 1562 | |
| 1563 | case OPC_PUSHINT: |
| 1564 | /* push a UINT4 operand value */ |
| 1565 | push()->set_int(get_op_int32(&p)); |
| 1566 | continue; |
| 1567 | |
| 1568 | case OPC_INC: |
| 1569 | /* |
| 1570 | * Increment the value at top of stack. We must perform |
| 1571 | * the same type conversions as the ADD instruction does. |
| 1572 | * As an optimization, check to see if we have an integer |
| 1573 | * on top of the stack, and if so simply increment its |
| 1574 | * value without popping and repushing. |
| 1575 | */ |
| 1576 | if ((valp = get(0))->typ == VM_INT) |
| 1577 | { |
| 1578 | /* it's an integer - increment it, and we're done */ |
| 1579 | ++(valp->val.intval); |
| 1580 | } |
| 1581 | else |
| 1582 | { |
| 1583 | /* for other types, use the general handler */ |
| 1584 | p = compute_sum_inc(vmg_ p); |
| 1585 | } |
| 1586 | continue; |
| 1587 | |
| 1588 | case OPC_ADD: |
| 1589 | /* if they're both integers, add them the quick way */ |
| 1590 | valp = get(0); |
| 1591 | valp2 = get(1); |
| 1592 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1593 | { |
| 1594 | /* compute the sum */ |
| 1595 | INT_ADD(valp2, valp->val.intval); |
| 1596 | |
| 1597 | /* discard the second value */ |
| 1598 | discard(); |
| 1599 | } |
| 1600 | else |
| 1601 | { |
| 1602 | /* for other types, use the general handler */ |
| 1603 | p = compute_sum_add(vmg_ p); |
| 1604 | } |
| 1605 | continue; |
| 1606 | |
| 1607 | case OPC_DEC: |
| 1608 | /* |
| 1609 | * Decrement the value at top of stack. We must perform |
| 1610 | * the same type conversions as the SUB instruction does. |
| 1611 | * As an optimization, check to see if we have an integer |
| 1612 | * on top of the stack, and if so simply decrement its |
| 1613 | * value without popping and repushing. |
| 1614 | */ |
| 1615 | if ((valp = get(0))->typ == VM_INT) |
| 1616 | { |
| 1617 | /* it's an integer - decrement it, and we're done */ |
| 1618 | INT_SUB(valp, 1); |
| 1619 | } |
| 1620 | else |
| 1621 | { |
| 1622 | /* for other types, use the general handler */ |
| 1623 | p = compute_diff_dec(vmg_ p); |
| 1624 | } |
| 1625 | continue; |
| 1626 | |
| 1627 | case OPC_SUB: |
| 1628 | /* if they're both integers, subtract them the quick way */ |
| 1629 | valp = get(0); |
| 1630 | valp2 = get(1); |
| 1631 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1632 | { |
| 1633 | /* compute the difference */ |
| 1634 | INT_SUB(valp2, valp->val.intval); |
| 1635 | |
| 1636 | /* discard the second value */ |
| 1637 | discard(); |
| 1638 | } |
| 1639 | else |
| 1640 | { |
| 1641 | /* for other types, use the general handler */ |
| 1642 | p = compute_diff_sub(vmg_ p); |
| 1643 | } |
| 1644 | continue; |
| 1645 | |
| 1646 | case OPC_PUSHSTR: |
| 1647 | /* push UINT4 offset operand as a string */ |
| 1648 | push()->set_sstring(get_op_uint32(&p)); |
| 1649 | continue; |
| 1650 | |
| 1651 | case OPC_DISC: |
| 1652 | /* discard the item at the top of the stack */ |
| 1653 | discard(); |
| 1654 | continue; |
| 1655 | |
| 1656 | case OPC_DISC1: |
| 1657 | /* discard n items */ |
| 1658 | discard(get_op_uint8(&p)); |
| 1659 | continue; |
| 1660 | |
| 1661 | case OPC_PUSHLST: |
| 1662 | /* push UINT4 offset operand as a list */ |
| 1663 | push()->set_list(get_op_uint32(&p)); |
| 1664 | continue; |
| 1665 | |
| 1666 | case OPC_PUSHOBJ: |
| 1667 | /* push UINT4 object ID operand */ |
| 1668 | push()->set_obj(get_op_uint32(&p)); |
| 1669 | continue; |
| 1670 | |
| 1671 | case OPC_PUSHPROPID: |
| 1672 | /* push UINT2 property ID operand */ |
| 1673 | push()->set_propid(get_op_uint16(&p)); |
| 1674 | continue; |
| 1675 | |
| 1676 | case OPC_PUSHFNPTR: |
| 1677 | /* push a function pointer operand */ |
| 1678 | push()->set_fnptr(get_op_uint32(&p)); |
| 1679 | continue; |
| 1680 | |
| 1681 | case OPC_PUSHPARLST: |
| 1682 | { |
| 1683 | /* get the number of fixed parameters */ |
| 1684 | uint cnt = *p++; |
| 1685 | |
| 1686 | /* allocate the list from the parameters */ |
| 1687 | obj = CVmObjList::create_from_params( |
| 1688 | vmg_ cnt, get_cur_argc(vmg0_) - cnt); |
| 1689 | |
| 1690 | /* push the new list */ |
| 1691 | push()->set_obj(obj); |
| 1692 | } |
| 1693 | continue; |
| 1694 | |
| 1695 | case OPC_MAKELSTPAR: |
| 1696 | makelstpar(vmg0_); |
| 1697 | continue; |
| 1698 | |
| 1699 | case OPC_NEG: |
| 1700 | /* if it's an integer value, do the calculation inline */ |
| 1701 | if ((valp = get(0))->typ == VM_INT) |
| 1702 | { |
| 1703 | /* negate number in place */ |
| 1704 | INT_NEG(valp); |
| 1705 | } |
| 1706 | else if (valp->typ == VM_OBJ |
| 1707 | && vm_objp(vmg_ valp->val.obj)->neg_val( |
| 1708 | vmg_ &val2, valp->val.obj)) |
| 1709 | { |
| 1710 | /* the object defined it natively - store the result */ |
| 1711 | *valp = val2; |
| 1712 | } |
| 1713 | else |
| 1714 | { |
| 1715 | /* try the overloaded operator */ |
| 1716 | pop(&val); |
| 1717 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1718 | &val, G_predef->operator_neg, 0, |
| 1719 | VMERR_BAD_TYPE_NEG); |
| 1720 | } |
| 1721 | continue; |
| 1722 | |
| 1723 | case OPC_BNOT: |
| 1724 | /* check the type */ |
| 1725 | if ((valp = get(0))->typ == VM_INT) |
| 1726 | { |
| 1727 | /* bitwise NOT the integer on top of stack */ |
| 1728 | valp->val.intval = ~valp->val.intval; |
| 1729 | } |
| 1730 | else |
| 1731 | { |
| 1732 | /* try the overloaded operator */ |
| 1733 | pop(&val); |
| 1734 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1735 | &val, G_predef->operator_bit_not, 0, |
| 1736 | VMERR_BAD_TYPE_BIT_NOT); |
| 1737 | } |
| 1738 | continue; |
| 1739 | |
| 1740 | case OPC_MUL: |
| 1741 | /* if they're both integers, this is easy */ |
| 1742 | valp = get(0); |
| 1743 | valp2 = get(1); |
| 1744 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1745 | { |
| 1746 | /* compute the product */ |
| 1747 | INT_MUL(valp2, valp->val.intval); |
| 1748 | |
| 1749 | /* discard the second value */ |
| 1750 | discard(); |
| 1751 | } |
| 1752 | else if (compute_product(vmg_ valp2, valp)) |
| 1753 | { |
| 1754 | /* success - discard the second value */ |
| 1755 | discard(); |
| 1756 | } |
| 1757 | else |
| 1758 | { |
| 1759 | /* try for an operator overload */ |
| 1760 | pop_left_op(vmg_ &val); |
| 1761 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1762 | &val, G_predef->operator_mul, 1, |
| 1763 | VMERR_BAD_TYPE_MUL); |
| 1764 | } |
| 1765 | continue; |
| 1766 | |
| 1767 | case OPC_DIV: |
| 1768 | /* if they're both integers, do the division inline */ |
| 1769 | valp = get(0); |
| 1770 | valp2 = get(1); |
| 1771 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1772 | { |
| 1773 | /* compute the result of the division */ |
| 1774 | INT_DIV(valp2, valp->val.intval); |
| 1775 | |
| 1776 | /* discard the second value */ |
| 1777 | discard(); |
| 1778 | } |
| 1779 | else if (compute_quotient(vmg_ valp2, valp)) |
| 1780 | { |
| 1781 | /* success - discard the second operand */ |
| 1782 | discard(); |
| 1783 | } |
| 1784 | else |
| 1785 | { |
| 1786 | /* try for an operator overload */ |
| 1787 | pop_left_op(vmg_ &val); |
| 1788 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1789 | &val, G_predef->operator_div, 1, |
| 1790 | VMERR_BAD_TYPE_DIV); |
| 1791 | } |
| 1792 | continue; |
| 1793 | |
| 1794 | case OPC_MOD: |
| 1795 | /* remainder number at (TOS-1) by number at top of stack */ |
| 1796 | valp = get(0); |
| 1797 | valp2 = get(1); |
| 1798 | |
| 1799 | /* if they're both integers, do a quick local operation */ |
| 1800 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1801 | { |
| 1802 | /* check for division by zero */ |
| 1803 | if (valp->val.intval == 0) |
| 1804 | err_throw(VMERR_DIVIDE_BY_ZERO); |
| 1805 | |
| 1806 | /* |
| 1807 | * compute the remainder (TOS-1) % (TOS), leaving the |
| 1808 | * result at (TOS-1), and discard the second operand |
| 1809 | */ |
| 1810 | valp2->val.intval = os_remainder_long( |
| 1811 | valp2->val.intval, valp->val.intval); |
| 1812 | |
| 1813 | /* discard the second value */ |
| 1814 | discard(); |
| 1815 | } |
| 1816 | else |
| 1817 | { |
| 1818 | /* try for an operator overload */ |
| 1819 | pop_left_op(vmg_ &val); |
| 1820 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1821 | &val, G_predef->operator_mod, 1, |
| 1822 | VMERR_BAD_TYPE_MOD); |
| 1823 | } |
| 1824 | continue; |
| 1825 | |
| 1826 | case OPC_BAND: |
| 1827 | /* bitwise AND two integers on top of stack */ |
| 1828 | valp = get(0); |
| 1829 | valp2 = get(1); |
| 1830 | |
| 1831 | /* if we have two integers, do it the quick way */ |
| 1832 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1833 | { |
| 1834 | /* compute the result and discard the second operand */ |
| 1835 | valp2->val.intval &= valp->val.intval; |
| 1836 | |
| 1837 | /* discard the second value */ |
| 1838 | discard(); |
| 1839 | } |
| 1840 | else |
| 1841 | { |
| 1842 | /* try for an operator overload */ |
| 1843 | pop_left_op(vmg_ &val); |
| 1844 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1845 | &val, G_predef->operator_bit_and, 1, |
| 1846 | VMERR_BAD_TYPE_BIT_AND); |
| 1847 | } |
| 1848 | continue; |
| 1849 | |
| 1850 | case OPC_BOR: |
| 1851 | /* bitwise OR two integers on top of stack */ |
| 1852 | valp = get(0); |
| 1853 | valp2 = get(1); |
| 1854 | |
| 1855 | /* check the types */ |
| 1856 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1857 | { |
| 1858 | /* compute the result */ |
| 1859 | valp2->val.intval |= valp->val.intval; |
| 1860 | |
| 1861 | /* discard the second operand */ |
| 1862 | discard(); |
| 1863 | } |
| 1864 | else |
| 1865 | { |
| 1866 | /* try for an operator overload */ |
| 1867 | pop_left_op(vmg_ &val); |
| 1868 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1869 | &val, G_predef->operator_bit_or, 1, |
| 1870 | VMERR_BAD_TYPE_BIT_OR); |
| 1871 | } |
| 1872 | continue; |
| 1873 | |
| 1874 | case OPC_SHL: |
| 1875 | /* |
| 1876 | * bit-shift left integer at (TOS-1) by integer at top |
| 1877 | * of stack |
| 1878 | */ |
| 1879 | valp = get(0); |
| 1880 | valp2 = get(1); |
| 1881 | |
| 1882 | /* check the types */ |
| 1883 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1884 | { |
| 1885 | /* compute the result */ |
| 1886 | valp2->val.intval <<= valp->val.intval; |
| 1887 | |
| 1888 | /* discard the second operand */ |
| 1889 | discard(); |
| 1890 | } |
| 1891 | else |
| 1892 | { |
| 1893 | /* try for an operator overload */ |
| 1894 | pop_left_op(vmg_ &val); |
| 1895 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1896 | &val, G_predef->operator_shl, 1, |
| 1897 | VMERR_BAD_TYPE_SHL); |
| 1898 | } |
| 1899 | continue; |
| 1900 | |
| 1901 | case OPC_ASHR: |
| 1902 | /* |
| 1903 | * arithmetic shift right integer at (TOS-1) by integer at |
| 1904 | * top of stack |
| 1905 | */ |
| 1906 | valp = get(0); |
| 1907 | valp2 = get(1); |
| 1908 | |
| 1909 | /* check types */ |
| 1910 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1911 | { |
| 1912 | /* compute the result */ |
| 1913 | valp2->val.intval = |
| 1914 | t3_ashr(valp2->val.intval, valp->val.intval); |
| 1915 | |
| 1916 | /* discard the second operand */ |
| 1917 | discard(); |
| 1918 | } |
| 1919 | else |
| 1920 | { |
| 1921 | /* try for an operator overload */ |
| 1922 | pop_left_op(vmg_ &val); |
| 1923 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1924 | &val, G_predef->operator_ashr, 1, |
| 1925 | VMERR_BAD_TYPE_ASHR); |
| 1926 | } |
| 1927 | continue; |
| 1928 | |
| 1929 | case OPC_LSHR: |
| 1930 | /* |
| 1931 | * logical shift right integer at (TOS-1) by integer at |
| 1932 | * top of stack |
| 1933 | */ |
| 1934 | valp = get(0); |
| 1935 | valp2 = get(1); |
| 1936 | |
| 1937 | /* check types */ |
| 1938 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 1939 | { |
| 1940 | /* compute the result */ |
| 1941 | valp2->val.intval = |
| 1942 | t3_lshr(valp2->val.intval, valp->val.intval); |
| 1943 | |
| 1944 | /* discard the second operand */ |
| 1945 | discard(); |
| 1946 | } |
| 1947 | else |
| 1948 | { |
| 1949 | /* try for an operator overload */ |
| 1950 | pop_left_op(vmg_ &val); |
| 1951 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1952 | &val, G_predef->operator_lshr, 1, |
| 1953 | VMERR_BAD_TYPE_LSHR); |
| 1954 | } |
| 1955 | continue; |
| 1956 | |
| 1957 | case OPC_XOR: |
| 1958 | /* XOR two values at top of stack */ |
| 1959 | popval_2(vmg_ &val, &val2); |
| 1960 | if (!xor_and_push(vmg_ &val, &val2)) |
| 1961 | { |
| 1962 | /* try for an operator overload */ |
| 1963 | push(&val2); |
| 1964 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 1965 | &val, G_predef->operator_xor, 1, |
| 1966 | VMERR_BAD_TYPE_XOR); |
| 1967 | } |
| 1968 | continue; |
| 1969 | |
| 1970 | case OPC_NOT: |
| 1971 | /* |
| 1972 | * invert the logic value; if the value is a number, |
| 1973 | * treat 0 as nil and non-zero as true |
| 1974 | */ |
| 1975 | valp = get(0); |
| 1976 | switch(valp->typ) |
| 1977 | { |
| 1978 | case VM_NIL: |
| 1979 | /* !nil -> true */ |
| 1980 | valp->set_true(); |
| 1981 | continue; |
| 1982 | |
| 1983 | case VM_OBJ: |
| 1984 | /* !obj -> true if obj is nil, nil otherwise */ |
| 1985 | valp->set_logical(valp->val.obj == VM_INVALID_OBJ); |
| 1986 | continue; |
| 1987 | |
| 1988 | case VM_TRUE: |
| 1989 | case VM_PROP: |
| 1990 | case VM_SSTRING: |
| 1991 | case VM_LIST: |
| 1992 | case VM_CODEOFS: |
| 1993 | case VM_FUNCPTR: |
| 1994 | case VM_ENUM: |
| 1995 | /* these are all considered true, so !them -> nil */ |
| 1996 | valp->set_nil(); |
| 1997 | continue; |
| 1998 | |
| 1999 | case VM_INT: |
| 2000 | /* !int -> true if int is 0, nil otherwise */ |
| 2001 | valp->set_logical(valp->val.intval == 0); |
| 2002 | continue; |
| 2003 | |
| 2004 | default: |
| 2005 | err_throw(VMERR_NO_LOG_CONV); |
| 2006 | } |
| 2007 | continue; |
| 2008 | |
| 2009 | case OPC_BOOLIZE: |
| 2010 | /* set to a boolean value */ |
| 2011 | valp = get(0); |
| 2012 | switch(valp->typ) |
| 2013 | { |
| 2014 | case VM_NIL: |
| 2015 | case VM_TRUE: |
| 2016 | /* it's already a logical value - leave it alone */ |
| 2017 | continue; |
| 2018 | |
| 2019 | case VM_INT: |
| 2020 | /* integer: 0 -> nil, non-zero -> true */ |
| 2021 | valp->set_logical(valp->val.intval); |
| 2022 | continue; |
| 2023 | |
| 2024 | case VM_ENUM: |
| 2025 | /* an enum is always non-nil */ |
| 2026 | valp->set_true(); |
| 2027 | continue; |
| 2028 | |
| 2029 | default: |
| 2030 | err_throw(VMERR_NO_LOG_CONV); |
| 2031 | } |
| 2032 | continue; |
| 2033 | |
| 2034 | case OPC_EQ: |
| 2035 | /* compare two values at top of stack for equality */ |
| 2036 | push_bool(vmg_ pop2_equal(vmg0_)); |
| 2037 | continue; |
| 2038 | |
| 2039 | case OPC_NE: |
| 2040 | /* compare two values at top of stack for inequality */ |
| 2041 | push_bool(vmg_ !pop2_equal(vmg0_)); |
| 2042 | continue; |
| 2043 | |
| 2044 | case OPC_LT: |
| 2045 | /* compare values at top of stack - true if (TOS-1) < TOS */ |
| 2046 | push_bool(vmg_ pop2_compare_lt(vmg0_)); |
| 2047 | continue; |
| 2048 | |
| 2049 | case OPC_LE: |
| 2050 | /* compare values at top of stack - true if (TOS-1) <= TOS */ |
| 2051 | push_bool(vmg_ pop2_compare_le(vmg0_)); |
| 2052 | continue; |
| 2053 | |
| 2054 | case OPC_GT: |
| 2055 | /* compare values at top of stack - true if (TOS-1) > TOS */ |
| 2056 | push_bool(vmg_ pop2_compare_gt(vmg0_)); |
| 2057 | continue; |
| 2058 | |
| 2059 | case OPC_GE: |
| 2060 | /* compare values at top of stack - true if (TOS-1) >= TOS */ |
| 2061 | push_bool(vmg_ pop2_compare_ge(vmg0_)); |
| 2062 | continue; |
| 2063 | |
| 2064 | case OPC_VARARGC: |
| 2065 | { |
| 2066 | /* get the modified opcode */ |
| 2067 | uchar opc = *p++; |
| 2068 | |
| 2069 | /* |
| 2070 | * skip the immediate data argument count - this is |
| 2071 | * superseded by our dynamic argument counter |
| 2072 | */ |
| 2073 | ++p; |
| 2074 | |
| 2075 | /* pop the argument counter */ |
| 2076 | pop_int(vmg_ &val); |
| 2077 | argc = val.val.intval; |
| 2078 | |
| 2079 | /* execute the appropriate next opcode */ |
| 2080 | switch(opc) |
| 2081 | { |
| 2082 | case OPC_CALL: |
| 2083 | goto do_opc_call; |
| 2084 | |
| 2085 | case OPC_PTRCALL: |
| 2086 | goto do_opc_ptrcall; |
| 2087 | |
| 2088 | case OPC_CALLPROP: |
| 2089 | goto do_opc_callprop; |
| 2090 | |
| 2091 | case OPC_PTRCALLPROP: |
| 2092 | goto do_opc_ptrcallprop; |
| 2093 | |
| 2094 | case OPC_CALLPROPSELF: |
| 2095 | goto do_opc_callpropself; |
| 2096 | |
| 2097 | case OPC_PTRCALLPROPSELF: |
| 2098 | goto do_opc_ptrcallpropself; |
| 2099 | |
| 2100 | case OPC_OBJCALLPROP: |
| 2101 | goto do_opc_objcallprop; |
| 2102 | |
| 2103 | case OPC_CALLPROPLCL1: |
| 2104 | goto do_opc_callproplcl1; |
| 2105 | |
| 2106 | case OPC_CALLPROPR0: |
| 2107 | goto do_opc_callpropr0; |
| 2108 | |
| 2109 | case OPC_INHERIT: |
| 2110 | goto do_opc_inherit; |
| 2111 | |
| 2112 | case OPC_PTRINHERIT: |
| 2113 | goto do_opc_ptrinherit; |
| 2114 | |
| 2115 | case OPC_EXPINHERIT: |
| 2116 | goto do_opc_expinherit; |
| 2117 | |
| 2118 | case OPC_PTREXPINHERIT: |
| 2119 | goto do_opc_ptrexpinherit; |
| 2120 | |
| 2121 | case OPC_DELEGATE: |
| 2122 | goto do_opc_delegate; |
| 2123 | |
| 2124 | case OPC_PTRDELEGATE: |
| 2125 | goto do_opc_ptrdelegate; |
| 2126 | |
| 2127 | case OPC_BUILTIN_A: |
| 2128 | goto do_opc_builtin_a; |
| 2129 | |
| 2130 | case OPC_BUILTIN_B: |
| 2131 | goto do_opc_builtin_b; |
| 2132 | |
| 2133 | case OPC_BUILTIN_C: |
| 2134 | goto do_opc_builtin_c; |
| 2135 | |
| 2136 | case OPC_BUILTIN_D: |
| 2137 | goto do_opc_builtin_d; |
| 2138 | |
| 2139 | case OPC_BUILTIN1: |
| 2140 | goto do_opc_builtin1; |
| 2141 | |
| 2142 | case OPC_BUILTIN2: |
| 2143 | goto do_opc_builtin2; |
| 2144 | |
| 2145 | case OPC_NEW1: |
| 2146 | goto do_opc_new1; |
| 2147 | |
| 2148 | case OPC_TRNEW1: |
| 2149 | goto do_opc_trnew1; |
| 2150 | |
| 2151 | case OPC_NEW2: |
| 2152 | goto do_opc_new2; |
| 2153 | |
| 2154 | case OPC_TRNEW2: |
| 2155 | goto do_opc_trnew2; |
| 2156 | |
| 2157 | default: |
| 2158 | err_throw(VMERR_INVALID_OPCODE_MOD); |
| 2159 | continue; |
| 2160 | } |
| 2161 | } |
| 2162 | continue; |
| 2163 | |
| 2164 | case OPC_NAMEDARGPTR: |
| 2165 | /* |
| 2166 | * Pointer to named argument table. Discard the named |
| 2167 | * arguments (the count is given by a one-byte operand), |
| 2168 | * then skip the table pointer (a two-byte operand). |
| 2169 | */ |
| 2170 | discard(get_op_uint8(&p)); |
| 2171 | p += 2; |
| 2172 | continue; |
| 2173 | |
| 2174 | case OPC_NAMEDARGTAB: |
| 2175 | /* |
| 2176 | * Named argument table. As with NAMEDARGPTR, we must |
| 2177 | * discard the named arguments. Then we simply skip the |
| 2178 | * table - the table is for the benefit of callees. |
| 2179 | */ |
| 2180 | { |
| 2181 | pool_ofs_t ofs = get_op_uint16(&p); |
| 2182 | discard(get_op_uint16(&p)); |
| 2183 | p += ofs - 2; |
| 2184 | } |
| 2185 | continue; |
| 2186 | |
| 2187 | case OPC_CALL: |
| 2188 | /* get the argument count */ |
| 2189 | argc = get_op_uint8(&p); |
| 2190 | |
| 2191 | do_opc_call: |
| 2192 | { |
| 2193 | /* get the code offset to invoke */ |
| 2194 | pool_ofs_t ofs = get_op_int32(&p); |
| 2195 | |
| 2196 | /* call it */ |
| 2197 | p = do_call_func_nr(vmg_ p - entry_ptr_native_, ofs, argc); |
| 2198 | } |
| 2199 | continue; |
| 2200 | |
| 2201 | case OPC_PTRCALL: |
| 2202 | /* get the argument count */ |
| 2203 | argc = get_op_uint8(&p); |
| 2204 | |
| 2205 | do_opc_ptrcall: |
| 2206 | /* retrieve the target of the call */ |
| 2207 | popval(vmg_ &val); |
| 2208 | |
| 2209 | /* |
| 2210 | * if it's a prop ID, and there's a valid "self" object, |
| 2211 | * treat it as a PTRCALLPROPSELF |
| 2212 | */ |
| 2213 | if (val.typ == VM_PROP |
| 2214 | && get_self_check(vmg0_) != VM_INVALID_OBJ) |
| 2215 | goto do_opc_ptrcallpropself_val; |
| 2216 | |
| 2217 | /* call the function */ |
| 2218 | p = call_func_ptr(vmg_ &val, argc, 0, p - entry_ptr_native_); |
| 2219 | continue; |
| 2220 | |
| 2221 | case OPC_RETTRUE: |
| 2222 | /* store true in R0 */ |
| 2223 | r0_.set_true(); |
| 2224 | |
| 2225 | /* return */ |
| 2226 | if ((p = do_return(vmg0_)) == 0) |
| 2227 | goto exit_loop; |
| 2228 | continue; |
| 2229 | |
| 2230 | case OPC_GETPROPR0: |
| 2231 | /* evaluate the property of R0 */ |
| 2232 | propev.self = r0_; |
| 2233 | prop = get_op_uint16(&p); |
| 2234 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop); |
| 2235 | continue; |
| 2236 | |
| 2237 | case OPC_CALLPROPLCL1: |
| 2238 | /* get the argument count */ |
| 2239 | argc = get_op_uint8(&p); |
| 2240 | |
| 2241 | do_opc_callproplcl1: |
| 2242 | /* get the local whose property we're calling */ |
| 2243 | propev.self = *get_local(vmg_ get_op_uint8(&p)); |
| 2244 | |
| 2245 | /* call the property of the local */ |
| 2246 | prop = get_op_uint16(&p); |
| 2247 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop, argc); |
| 2248 | continue; |
| 2249 | |
| 2250 | case OPC_CALLPROPR0: |
| 2251 | /* get the argument count */ |
| 2252 | argc = get_op_uint8(&p); |
| 2253 | |
| 2254 | do_opc_callpropr0: |
| 2255 | /* call the property of R0 */ |
| 2256 | propev.self = r0_; |
| 2257 | prop = get_op_uint16(&p); |
| 2258 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop, argc); |
| 2259 | continue; |
| 2260 | |
| 2261 | case OPC_PTRCALLPROP: |
| 2262 | /* get the argument count */ |
| 2263 | argc = get_op_uint8(&p); |
| 2264 | |
| 2265 | do_opc_ptrcallprop: |
| 2266 | /* |
| 2267 | * pop the property to be evaluated, and the object whose |
| 2268 | * property we're evaluating |
| 2269 | */ |
| 2270 | pop_prop(vmg_ &val); |
| 2271 | pop(&propev.self); |
| 2272 | |
| 2273 | /* evaluate the property */ |
| 2274 | p = propev.get_prop(vmg_ p - entry_ptr_native_, |
| 2275 | val.val.prop, argc); |
| 2276 | continue; |
| 2277 | |
| 2278 | case OPC_PTRCALLPROPSELF: |
| 2279 | /* get the argument count */ |
| 2280 | argc = get_op_uint8(&p); |
| 2281 | |
| 2282 | do_opc_ptrcallpropself: |
| 2283 | /* get the property to be evaluated */ |
| 2284 | pop_prop(vmg_ &val); |
| 2285 | |
| 2286 | do_opc_ptrcallpropself_val: |
| 2287 | /* evaluate the property of 'self' */ |
| 2288 | propev.self.set_obj(get_self(vmg0_)); |
| 2289 | p = propev.get_prop(vmg_ p - entry_ptr_native_, |
| 2290 | val.val.prop, argc); |
| 2291 | continue; |
| 2292 | |
| 2293 | case OPC_OBJGETPROP: |
| 2294 | /* get the object */ |
| 2295 | propev.self.set_obj((vm_obj_id_t)get_op_uint32(&p)); |
| 2296 | |
| 2297 | /* evaluate the property */ |
| 2298 | prop = get_op_uint16(&p); |
| 2299 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop); |
| 2300 | continue; |
| 2301 | |
| 2302 | case OPC_OBJCALLPROP: |
| 2303 | /* get the argument count */ |
| 2304 | argc = get_op_uint8(&p); |
| 2305 | |
| 2306 | do_opc_objcallprop: |
| 2307 | /* get the object */ |
| 2308 | propev.self.set_obj((vm_obj_id_t)get_op_uint32(&p)); |
| 2309 | |
| 2310 | /* evaluate the property */ |
| 2311 | prop = get_op_uint16(&p); |
| 2312 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop, argc); |
| 2313 | continue; |
| 2314 | |
| 2315 | case OPC_GETLCL1: |
| 2316 | /* push the local */ |
| 2317 | pushval(vmg_ get_local(vmg_ get_op_uint8(&p))); |
| 2318 | continue; |
| 2319 | |
| 2320 | case OPC_GETLCLN5: |
| 2321 | pushval(vmg_ get_local(vmg_ 5)); |
| 2322 | continue; |
| 2323 | |
| 2324 | case OPC_GETLCL2: |
| 2325 | /* push the local */ |
| 2326 | pushval(vmg_ get_local(vmg_ get_op_uint16(&p))); |
| 2327 | continue; |
| 2328 | |
| 2329 | case OPC_GETARG1: |
| 2330 | /* push the argument */ |
| 2331 | pushval(vmg_ get_param(vmg_ get_op_uint8(&p))); |
| 2332 | continue; |
| 2333 | |
| 2334 | case OPC_GETARG2: |
| 2335 | /* push the argument */ |
| 2336 | pushval(vmg_ get_param(vmg_ get_op_uint16(&p))); |
| 2337 | continue; |
| 2338 | |
| 2339 | case OPC_SETSELF: |
| 2340 | /* retrieve the 'self' object */ |
| 2341 | pop(&val); |
| 2342 | |
| 2343 | /* if it's nil, set the 'obj' field to null */ |
| 2344 | if (val.typ == VM_NIL) |
| 2345 | val.val.obj = VM_INVALID_OBJ; |
| 2346 | |
| 2347 | /* set 'self' */ |
| 2348 | set_self(vmg_ &val); |
| 2349 | continue; |
| 2350 | |
| 2351 | case OPC_STORECTX: |
| 2352 | /* create the context object */ |
| 2353 | create_loadctx_obj(vmg_ push(), |
| 2354 | get_self(vmg0_), |
| 2355 | get_defining_obj(vmg0_), |
| 2356 | get_orig_target_obj(vmg0_), |
| 2357 | get_target_prop(vmg0_)); |
| 2358 | continue; |
| 2359 | |
| 2360 | case OPC_LOADCTX: |
| 2361 | { |
| 2362 | /* |
| 2363 | * convert the context object (at top of stack) to a |
| 2364 | * list pointer |
| 2365 | */ |
| 2366 | const char *lstp = get(0)->get_as_list(vmg0_); |
| 2367 | |
| 2368 | /* throw an error if it's not what we're expecting */ |
| 2369 | if (lstp == 0 || vmb_get_len(lstp) < 4) |
| 2370 | err_throw(VMERR_LIST_VAL_REQD); |
| 2371 | |
| 2372 | /* retrieve and store the context elements */ |
| 2373 | set_method_ctx( |
| 2374 | vmg_ vmb_get_dh_obj(lstp + VMB_LEN), |
| 2375 | vmb_get_dh_prop(lstp + VMB_LEN |
| 2376 | + VMB_DATAHOLDER), |
| 2377 | vmb_get_dh_obj(lstp + VMB_LEN |
| 2378 | + 2*VMB_DATAHOLDER), |
| 2379 | vmb_get_dh_obj(lstp + VMB_LEN |
| 2380 | + 3*VMB_DATAHOLDER)); |
| 2381 | |
| 2382 | /* discard the context object at top of stack */ |
| 2383 | discard(); |
| 2384 | } |
| 2385 | continue; |
| 2386 | |
| 2387 | case OPC_PUSHCTXELE: |
| 2388 | /* check our context element type */ |
| 2389 | switch(*p++) |
| 2390 | { |
| 2391 | case PUSHCTXELE_TARGPROP: |
| 2392 | /* push the target property ID */ |
| 2393 | push(get_from_frame(frame_ptr_, VMRUN_FPOFS_PROP)); |
| 2394 | continue; |
| 2395 | |
| 2396 | case PUSHCTXELE_TARGOBJ: |
| 2397 | /* push the original target object ID */ |
| 2398 | push(get_from_frame(frame_ptr_, VMRUN_FPOFS_ORIGTARG)); |
| 2399 | continue; |
| 2400 | |
| 2401 | case PUSHCTXELE_DEFOBJ: |
| 2402 | /* push the defining object */ |
| 2403 | push(get_from_frame(frame_ptr_, VMRUN_FPOFS_DEFOBJ)); |
| 2404 | continue; |
| 2405 | |
| 2406 | case PUSHCTXELE_INVOKEE: |
| 2407 | /* push the invokee object */ |
| 2408 | push(get_from_frame(frame_ptr_, VMRUN_FPOFS_INVOKEE)); |
| 2409 | continue; |
| 2410 | |
| 2411 | default: |
| 2412 | /* the opcode is not valid in this VM version */ |
| 2413 | err_throw(VMERR_INVALID_OPCODE); |
| 2414 | } |
| 2415 | continue; |
| 2416 | |
| 2417 | case OPC_GETARGC: |
| 2418 | /* push the argument counter */ |
| 2419 | push_int(vmg_ get_cur_argc(vmg0_)); |
| 2420 | continue; |
| 2421 | |
| 2422 | case OPC_DUP2: |
| 2423 | /* |
| 2424 | * duplicate the top two elements: first push the |
| 2425 | * second-from-top, then push the old top (which will now |
| 2426 | * be the second-from-top, thanks to our first push) |
| 2427 | */ |
| 2428 | pushval(vmg_ get(1)); |
| 2429 | pushval(vmg_ get(1)); |
| 2430 | continue; |
| 2431 | |
| 2432 | case OPC_SWITCH: |
| 2433 | { |
| 2434 | /* get the control value */ |
| 2435 | valp = get(0); |
| 2436 | |
| 2437 | /* get the case count */ |
| 2438 | uint cnt = get_op_uint16(&p); |
| 2439 | |
| 2440 | /* iterate through the case table */ |
| 2441 | for ( ; cnt != 0 ; p += 7, --cnt) |
| 2442 | { |
| 2443 | /* get this value */ |
| 2444 | vmb_get_dh((const char *)p, &val2); |
| 2445 | |
| 2446 | /* check if the values match */ |
| 2447 | if (valp->equals(vmg_ &val2)) |
| 2448 | { |
| 2449 | /* it matches - jump to this offset */ |
| 2450 | p += VMB_DATAHOLDER; |
| 2451 | p += osrp2s(p); |
| 2452 | |
| 2453 | /* no need to look any further */ |
| 2454 | break; |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | /* discard the control value */ |
| 2459 | discard(); |
| 2460 | |
| 2461 | /* if we didn't find it, jump to the default case */ |
| 2462 | if (cnt == 0) |
| 2463 | p += osrp2s(p); |
| 2464 | } |
| 2465 | continue; |
| 2466 | |
| 2467 | case OPC_JT: |
| 2468 | /* get the value */ |
| 2469 | valp = get(0); |
| 2470 | |
| 2471 | /* |
| 2472 | * if it's true, or a non-zero numeric value, or any |
| 2473 | * non-numeric and non-boolean value, jump |
| 2474 | */ |
| 2475 | if (false_for_cond(valp)) |
| 2476 | { |
| 2477 | /* it's zero or nil - do not jump */ |
| 2478 | p += 2; |
| 2479 | } |
| 2480 | else |
| 2481 | { |
| 2482 | /* it's non-zero and non-nil - jump */ |
| 2483 | p += osrp2s(p); |
| 2484 | } |
| 2485 | |
| 2486 | /* discard the value */ |
| 2487 | discard(); |
| 2488 | continue; |
| 2489 | |
| 2490 | case OPC_JR0T: |
| 2491 | /* |
| 2492 | * if R0 is true, or it's a non-zero numeric value, or any |
| 2493 | * non-numeric and non-boolean value, jump |
| 2494 | */ |
| 2495 | if (false_for_cond(&r0_)) |
| 2496 | { |
| 2497 | /* it's zero or nil - do not jump */ |
| 2498 | p += 2; |
| 2499 | } |
| 2500 | else |
| 2501 | { |
| 2502 | /* it's non-zero and non-nil - jump */ |
| 2503 | p += osrp2s(p); |
| 2504 | } |
| 2505 | continue; |
| 2506 | |
| 2507 | case OPC_JF: |
| 2508 | /* get the value */ |
| 2509 | valp = get(0); |
| 2510 | |
| 2511 | /* |
| 2512 | * if it's true, or a non-zero numeric value, or any |
| 2513 | * non-numeric and non-boolean value, do not jump; |
| 2514 | * otherwise, jump |
| 2515 | */ |
| 2516 | if (false_for_cond(valp)) |
| 2517 | { |
| 2518 | /* it's zero or nil - jump */ |
| 2519 | p += osrp2s(p); |
| 2520 | } |
| 2521 | else |
| 2522 | { |
| 2523 | /* it's non-zero and non-nil - do not jump */ |
| 2524 | p += 2; |
| 2525 | } |
| 2526 | |
| 2527 | /* discard the value */ |
| 2528 | discard(); |
| 2529 | continue; |
| 2530 | |
| 2531 | case OPC_JGE: |
| 2532 | /* jump if greater or equal */ |
| 2533 | p += (pop2_compare_ge(vmg0_) ? osrp2s(p) : 2); |
| 2534 | continue; |
| 2535 | |
| 2536 | case OPC_JLT: |
| 2537 | /* jump if less */ |
| 2538 | p += (pop2_compare_lt(vmg0_) ? osrp2s(p) : 2); |
| 2539 | continue; |
| 2540 | |
| 2541 | case OPC_JLE: |
| 2542 | /* jump if less or equal */ |
| 2543 | p += (pop2_compare_le(vmg0_) ? osrp2s(p) : 2); |
| 2544 | continue; |
| 2545 | |
| 2546 | case OPC_JST: |
| 2547 | /* get (do not remove) the element at top of stack */ |
| 2548 | valp = get(0); |
| 2549 | |
| 2550 | /* |
| 2551 | * if it's true, an enum value, or a non-zero number, jump, |
| 2552 | * saving the value; otherwise, require that it be a |
| 2553 | * logical value, pop it, and proceed |
| 2554 | */ |
| 2555 | if (true_for_cond(valp)) |
| 2556 | { |
| 2557 | /* it's true - save it and jump */ |
| 2558 | p += osrp2s(p); |
| 2559 | } |
| 2560 | else |
| 2561 | { |
| 2562 | /* |
| 2563 | * it's not true - discard the value, but require |
| 2564 | * that it be a valid logical value |
| 2565 | */ |
| 2566 | if (!is_valid_for_jst(valp)) |
| 2567 | err_throw(VMERR_LOG_VAL_REQD); |
| 2568 | discard(); |
| 2569 | |
| 2570 | /* skip to the next instruction */ |
| 2571 | p += 2; |
| 2572 | } |
| 2573 | continue; |
| 2574 | |
| 2575 | case OPC_JSF: |
| 2576 | /* get (do not remove) the element at top of stack */ |
| 2577 | valp = get(0); |
| 2578 | |
| 2579 | /* |
| 2580 | * if it's nil or zero, jump, saving the value; |
| 2581 | * otherwise, discard the value and proceed |
| 2582 | */ |
| 2583 | if (false_for_cond(valp)) |
| 2584 | { |
| 2585 | /* it's nil or zero - save it and jump */ |
| 2586 | p += osrp2s(p); |
| 2587 | } |
| 2588 | else |
| 2589 | { |
| 2590 | /* it's something non-false - discard it */ |
| 2591 | discard(); |
| 2592 | |
| 2593 | /* skip to the next instruction */ |
| 2594 | p += 2; |
| 2595 | } |
| 2596 | continue; |
| 2597 | |
| 2598 | case OPC_LJSR: |
| 2599 | /* |
| 2600 | * compute and push the offset of the next instruction |
| 2601 | * (at +2 because of the branch offset operand) from our |
| 2602 | * method header - this will be the return address, |
| 2603 | * which in this offset format will survive any code |
| 2604 | * swapping that might occur in subsequent execution |
| 2605 | */ |
| 2606 | push_int(vmg_ pc_to_method_ofs(p + 2)); |
| 2607 | |
| 2608 | /* jump to the target address */ |
| 2609 | p += osrp2s(p); |
| 2610 | continue; |
| 2611 | |
| 2612 | case OPC_LRET: |
| 2613 | /* get the indicated local variable */ |
| 2614 | valp = get_local(vmg_ get_op_uint16(&p)); |
| 2615 | |
| 2616 | /* the value must be an integer */ |
| 2617 | if (valp->typ != VM_INT) |
| 2618 | err_throw(VMERR_INT_VAL_REQD); |
| 2619 | |
| 2620 | /* |
| 2621 | * jump to the code address obtained from adding the |
| 2622 | * integer value in the given local variable to the |
| 2623 | * current method header pointer |
| 2624 | */ |
| 2625 | p = entry_ptr_native_ + valp->val.intval; |
| 2626 | continue; |
| 2627 | |
| 2628 | case OPC_SWAP: |
| 2629 | /* swap the top two elements on the stack */ |
| 2630 | valp = get(0); |
| 2631 | valp2 = get(1); |
| 2632 | |
| 2633 | /* make a working copy of TOS */ |
| 2634 | val = *valp; |
| 2635 | |
| 2636 | /* copy TOS-1 over TOS */ |
| 2637 | *valp = *valp2; |
| 2638 | |
| 2639 | /* copy the working copy of TOS over TOS-1 */ |
| 2640 | *valp2 = val; |
| 2641 | continue; |
| 2642 | |
| 2643 | case OPC_SWAP2: |
| 2644 | /* swap the top two elements with the next two */ |
| 2645 | valp = get(0); |
| 2646 | valp2 = get(1); |
| 2647 | |
| 2648 | /* make a working copy of elements 2 & 3 */ |
| 2649 | val = *get(2); |
| 2650 | val2 = *get(3); |
| 2651 | |
| 2652 | /* copy 0,1 over 2,3 */ |
| 2653 | *get(2) = *valp; |
| 2654 | *get(3) = *valp2; |
| 2655 | |
| 2656 | /* copy the saved 2,3 over 0,1 */ |
| 2657 | *valp = val; |
| 2658 | *valp2 = val2; |
| 2659 | continue; |
| 2660 | |
| 2661 | case OPC_SWAPN: |
| 2662 | /* swap elements at two given stack indices */ |
| 2663 | valp = get(get_op_uint8(&p)); |
| 2664 | valp2 = get(get_op_uint8(&p)); |
| 2665 | |
| 2666 | /* make a working copy of val1 */ |
| 2667 | val = *valp; |
| 2668 | |
| 2669 | /* write val2 over val1 */ |
| 2670 | *valp = *valp2; |
| 2671 | |
| 2672 | /* write the copy of val1 over val2 */ |
| 2673 | *valp2 = val; |
| 2674 | continue; |
| 2675 | |
| 2676 | case OPC_GETSPN: |
| 2677 | /* push stack element at index */ |
| 2678 | push(get(get_op_uint8(&p))); |
| 2679 | continue; |
| 2680 | |
| 2681 | case OPC_SAY: |
| 2682 | { |
| 2683 | /* get the string offset */ |
| 2684 | pool_ofs_t ofs = get_op_int32(&p); |
| 2685 | |
| 2686 | /* display it */ |
| 2687 | p = disp_dstring(vmg_ ofs, p - entry_ptr_native_, |
| 2688 | get_self_check(vmg0_)); |
| 2689 | } |
| 2690 | continue; |
| 2691 | |
| 2692 | case OPC_SAYVAL: |
| 2693 | /* invoke the default string display function */ |
| 2694 | p = disp_string_val(vmg_ p - entry_ptr_native_, |
| 2695 | get_self_check(vmg0_)); |
| 2696 | continue; |
| 2697 | |
| 2698 | case OPC_INHERIT: |
| 2699 | /* get the argument count */ |
| 2700 | argc = get_op_uint8(&p); |
| 2701 | |
| 2702 | do_opc_inherit: |
| 2703 | /* inherit the property */ |
| 2704 | prop = (vm_prop_id_t)get_op_uint16(&p); |
| 2705 | p = inh_prop(vmg_ p - entry_ptr_native_, prop, argc); |
| 2706 | continue; |
| 2707 | |
| 2708 | case OPC_PTRINHERIT: |
| 2709 | /* get the argument count */ |
| 2710 | argc = get_op_uint8(&p); |
| 2711 | |
| 2712 | do_opc_ptrinherit: |
| 2713 | /* pop the property to be inherited */ |
| 2714 | pop_prop(vmg_ &val); |
| 2715 | |
| 2716 | /* inherit it */ |
| 2717 | p = inh_prop(vmg_ p - entry_ptr_native_, val.val.prop, argc); |
| 2718 | continue; |
| 2719 | |
| 2720 | case OPC_EXPINHERIT: |
| 2721 | /* get the argument count */ |
| 2722 | argc = get_op_uint8(&p); |
| 2723 | |
| 2724 | do_opc_expinherit: |
| 2725 | /* get the property to inherit */ |
| 2726 | prop = (vm_prop_id_t)get_op_uint16(&p); |
| 2727 | |
| 2728 | /* get the superclass to inherit it from */ |
| 2729 | val.set_obj((vm_obj_id_t)get_op_uint32(&p)); |
| 2730 | |
| 2731 | /* |
| 2732 | * inherit it -- process this essentially the same way |
| 2733 | * as a normal CALLPROP, since we're going to evaluate |
| 2734 | * the given property of the given object, but retain |
| 2735 | * the current 'self' object |
| 2736 | */ |
| 2737 | val2.set_obj(get_self(vmg0_)); |
| 2738 | p = get_prop(vmg_ p - entry_ptr_native_, |
| 2739 | &val, prop, &val2, argc, 0); |
| 2740 | continue; |
| 2741 | |
| 2742 | case OPC_PTREXPINHERIT: |
| 2743 | /* get the argument count */ |
| 2744 | argc = get_op_uint8(&p); |
| 2745 | |
| 2746 | do_opc_ptrexpinherit: |
| 2747 | /* pop the property to inherit */ |
| 2748 | pop_prop(vmg_ &val); |
| 2749 | |
| 2750 | /* get the superclass to inherit it from */ |
| 2751 | val3.set_obj((vm_obj_id_t)get_op_uint32(&p)); |
| 2752 | |
| 2753 | /* inherit it */ |
| 2754 | val2.set_obj(get_self(vmg0_)); |
| 2755 | p = get_prop(vmg_ p - entry_ptr_native_, |
| 2756 | &val3, val.val.prop, &val2, argc, 0); |
| 2757 | continue; |
| 2758 | |
| 2759 | case OPC_DELEGATE: |
| 2760 | /* get the argument count */ |
| 2761 | argc = get_op_uint8(&p); |
| 2762 | |
| 2763 | do_opc_delegate: |
| 2764 | /* get the property to inherit */ |
| 2765 | prop = (vm_prop_id_t)get_op_uint16(&p); |
| 2766 | |
| 2767 | /* get the object to delegate to */ |
| 2768 | pop(&val); |
| 2769 | |
| 2770 | /* delegate it */ |
| 2771 | val2.set_obj(get_self(vmg0_)); |
| 2772 | |
| 2773 | p = get_prop(vmg_ p - entry_ptr_native_, |
| 2774 | &val, prop, &val2, argc, 0); |
| 2775 | continue; |
| 2776 | |
| 2777 | case OPC_PTRDELEGATE: |
| 2778 | /* get the argument count */ |
| 2779 | argc = get_op_uint8(&p); |
| 2780 | |
| 2781 | do_opc_ptrdelegate: |
| 2782 | /* pop the property to delegate to */ |
| 2783 | pop_prop(vmg_ &val); |
| 2784 | |
| 2785 | /* pop the object to delegate to */ |
| 2786 | pop(&val2); |
| 2787 | |
| 2788 | /* delegate it */ |
| 2789 | val3.set_obj(get_self(vmg0_)); |
| 2790 | p = get_prop(vmg_ p - entry_ptr_native_, |
| 2791 | &val2, val.val.prop, &val3, argc, 0); |
| 2792 | continue; |
| 2793 | |
| 2794 | case OPC_BUILTIN_A: |
| 2795 | /* get the argument count */ |
| 2796 | argc = get_op_uint8(&p); |
| 2797 | |
| 2798 | do_opc_builtin_a: |
| 2799 | { |
| 2800 | /* get the function index */ |
| 2801 | uint idx = get_op_uint8(&p); |
| 2802 | |
| 2803 | /* call the function in set #0 */ |
| 2804 | call_bif(vmg_ 0, idx, argc); |
| 2805 | } |
| 2806 | continue; |
| 2807 | |
| 2808 | case OPC_BUILTIN_B: |
| 2809 | /* get the argument count */ |
| 2810 | argc = get_op_uint8(&p); |
| 2811 | |
| 2812 | do_opc_builtin_b: |
| 2813 | { |
| 2814 | /* get the function index */ |
| 2815 | uint idx = get_op_uint8(&p); |
| 2816 | |
| 2817 | /* call the function in set #1 */ |
| 2818 | call_bif(vmg_ 1, idx, argc); |
| 2819 | } |
| 2820 | continue; |
| 2821 | |
| 2822 | case OPC_BUILTIN_C: |
| 2823 | /* get the argument count */ |
| 2824 | argc = get_op_uint8(&p); |
| 2825 | |
| 2826 | do_opc_builtin_c: |
| 2827 | { |
| 2828 | /* get the function index */ |
| 2829 | uint idx = get_op_uint8(&p); |
| 2830 | |
| 2831 | /* call the function in set #2 */ |
| 2832 | call_bif(vmg_ 2, idx, argc); |
| 2833 | } |
| 2834 | continue; |
| 2835 | |
| 2836 | case OPC_BUILTIN_D: |
| 2837 | /* get the argument count */ |
| 2838 | argc = get_op_uint8(&p); |
| 2839 | |
| 2840 | do_opc_builtin_d: |
| 2841 | { |
| 2842 | /* get the function index */ |
| 2843 | uint idx = get_op_uint8(&p); |
| 2844 | |
| 2845 | /* call the function in set #3 */ |
| 2846 | call_bif(vmg_ 3, idx, argc); |
| 2847 | } |
| 2848 | continue; |
| 2849 | |
| 2850 | case OPC_BUILTIN1: |
| 2851 | /* get the argument count */ |
| 2852 | argc = get_op_uint8(&p); |
| 2853 | |
| 2854 | do_opc_builtin1: |
| 2855 | { |
| 2856 | /* get the function index and function set ID */ |
| 2857 | uint idx = get_op_uint8(&p); |
| 2858 | uint set_idx = get_op_uint8(&p); |
| 2859 | |
| 2860 | /* call the function */ |
| 2861 | call_bif(vmg_ set_idx, idx, argc); |
| 2862 | } |
| 2863 | continue; |
| 2864 | |
| 2865 | case OPC_BUILTIN2: |
| 2866 | /* get the argument count */ |
| 2867 | argc = get_op_uint8(&p); |
| 2868 | |
| 2869 | do_opc_builtin2: |
| 2870 | { |
| 2871 | /* get the function index and function set ID */ |
| 2872 | uint idx = get_op_uint16(&p); |
| 2873 | uint set_idx = get_op_uint8(&p); |
| 2874 | |
| 2875 | /* call the function in set #0 */ |
| 2876 | call_bif(vmg_ set_idx, idx, argc); |
| 2877 | } |
| 2878 | continue; |
| 2879 | |
| 2880 | case OPC_IDXINT8: |
| 2881 | /* |
| 2882 | * make a copy of the value to index, so we can overwrite |
| 2883 | * the stack slot with the result |
| 2884 | */ |
| 2885 | val = *(valp = get(0)); |
| 2886 | |
| 2887 | /* set up the index value */ |
| 2888 | val2.set_int(get_op_uint8(&p)); |
| 2889 | |
| 2890 | /* apply the index, storing the result at TOS */ |
| 2891 | if (!apply_index(vmg_ valp, &val, &val2)) |
| 2892 | { |
| 2893 | /* |
| 2894 | * try an operator overload - push the index value as |
| 2895 | * the argument |
| 2896 | */ |
| 2897 | pop(&val); |
| 2898 | push(&val2); |
| 2899 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 2900 | &val, G_predef->operator_idx, 1, |
| 2901 | VMERR_CANNOT_INDEX_TYPE); |
| 2902 | } |
| 2903 | continue; |
| 2904 | |
| 2905 | case OPC_NEW1: |
| 2906 | /* get the argument count */ |
| 2907 | argc = get_op_uint8(&p); |
| 2908 | |
| 2909 | /* fall through to do_opc_new1 */ |
| 2910 | do_opc_new1: |
| 2911 | { |
| 2912 | /* get the metaclass ID and create the object */ |
| 2913 | uint idx = get_op_uint8(&p); |
| 2914 | p = new_and_store_r0(vmg_ p, idx, argc, FALSE); |
| 2915 | } |
| 2916 | continue; |
| 2917 | |
| 2918 | case OPC_TRNEW1: |
| 2919 | /* get the argument count */ |
| 2920 | argc = get_op_uint8(&p); |
| 2921 | |
| 2922 | /* fall through to do_opc_trnew1 */ |
| 2923 | |
| 2924 | do_opc_trnew1: |
| 2925 | { |
| 2926 | /* get the metaclass ID and create the object */ |
| 2927 | uint idx = get_op_uint8(&p); |
| 2928 | p = new_and_store_r0(vmg_ p, idx, argc, TRUE); |
| 2929 | } |
| 2930 | continue; |
| 2931 | |
| 2932 | case OPC_NEW2: |
| 2933 | /* get the argument count */ |
| 2934 | argc = get_op_uint16(&p); |
| 2935 | |
| 2936 | /* fall through to do_opc_new2 */ |
| 2937 | do_opc_new2: |
| 2938 | { |
| 2939 | /* get the metaclass ID */ |
| 2940 | uint idx = get_op_uint16(&p); |
| 2941 | |
| 2942 | /* create the new object */ |
| 2943 | p = new_and_store_r0(vmg_ p, idx, argc, FALSE); |
| 2944 | } |
| 2945 | continue; |
| 2946 | |
| 2947 | case OPC_TRNEW2: |
| 2948 | /* get the argument count */ |
| 2949 | argc = get_op_uint16(&p); |
| 2950 | |
| 2951 | /* fall through to do_opc_trnew2 */ |
| 2952 | do_opc_trnew2: |
| 2953 | { |
| 2954 | /* get the metaclass ID */ |
| 2955 | uint idx = get_op_uint16(&p); |
| 2956 | |
| 2957 | /* create the new object */ |
| 2958 | p = new_and_store_r0(vmg_ p, idx, argc, TRUE); |
| 2959 | } |
| 2960 | continue; |
| 2961 | |
| 2962 | case OPC_NOP: |
| 2963 | /* NO OP - no effect */ |
| 2964 | continue; |
| 2965 | |
| 2966 | case OPC_INCLCL: |
| 2967 | /* get the local */ |
| 2968 | { |
| 2969 | int itmp = get_op_uint16(&p); |
| 2970 | valp = get_local(vmg_ itmp); |
| 2971 | |
| 2972 | /* do the increment inline if it's an integer */ |
| 2973 | if (valp->typ == VM_INT) |
| 2974 | { |
| 2975 | /* it's a number - just increment the value */ |
| 2976 | INT_ADD(valp, 1); |
| 2977 | } |
| 2978 | else |
| 2979 | { |
| 2980 | /* it's a non-numeric value - do the full addition */ |
| 2981 | val2.set_int(1); |
| 2982 | if (!compute_sum(vmg_ valp, &val2)) |
| 2983 | { |
| 2984 | /* |
| 2985 | * Check for an operator overload. We need to |
| 2986 | * do this with a recursive call to update the |
| 2987 | * local on return. |
| 2988 | */ |
| 2989 | push(&val2); |
| 2990 | p = op_overload(vmg_ p - entry_ptr_native_, itmp, |
| 2991 | valp, G_predef->operator_add, 1, |
| 2992 | VMERR_BAD_TYPE_ADD); |
| 2993 | } |
| 2994 | } |
| 2995 | } |
| 2996 | continue; |
| 2997 | |
| 2998 | case OPC_DECLCL: |
| 2999 | { |
| 3000 | /* get the local */ |
| 3001 | int itmp = get_op_uint16(&p); |
| 3002 | valp = get_local(vmg_ itmp); |
| 3003 | |
| 3004 | /* handle integers inline */ |
| 3005 | if (valp->typ == VM_INT) |
| 3006 | { |
| 3007 | /* it's a number - just decrement the value */ |
| 3008 | INT_SUB(valp, 1); |
| 3009 | } |
| 3010 | else |
| 3011 | { |
| 3012 | /* non-int - we must do the full subtraction work */ |
| 3013 | val2.set_int(1); |
| 3014 | if (!compute_diff(vmg_ valp, &val2)) |
| 3015 | { |
| 3016 | /* |
| 3017 | * Check for an operator overload. We need to |
| 3018 | * do this with a recursive call to update the |
| 3019 | * local on return. |
| 3020 | */ |
| 3021 | push(&val2); |
| 3022 | p = op_overload(vmg_ p - entry_ptr_native_, itmp, |
| 3023 | valp, G_predef->operator_sub, 1, |
| 3024 | VMERR_BAD_TYPE_SUB); |
| 3025 | } |
| 3026 | } |
| 3027 | } |
| 3028 | continue; |
| 3029 | |
| 3030 | case OPC_ADDILCL1: |
| 3031 | { |
| 3032 | /* get the local */ |
| 3033 | int itmp = get_op_uint8(&p); |
| 3034 | valp = get_local(vmg_ itmp); |
| 3035 | |
| 3036 | /* handle it inline if it's an integer */ |
| 3037 | if (valp->typ == VM_INT) |
| 3038 | { |
| 3039 | /* it's a number - just add the value */ |
| 3040 | INT_ADD(valp, get_op_int8(&p)); |
| 3041 | } |
| 3042 | else |
| 3043 | { |
| 3044 | /* get the number to add */ |
| 3045 | val2.set_int(get_op_int8(&p)); |
| 3046 | |
| 3047 | /* compute the sum */ |
| 3048 | p = compute_sum_lcl_imm(vmg_ valp, &val2, itmp, p); |
| 3049 | } |
| 3050 | } |
| 3051 | continue; |
| 3052 | |
| 3053 | case OPC_ADDILCL4: |
| 3054 | { |
| 3055 | /* get the local */ |
| 3056 | int itmp = get_op_uint16(&p); |
| 3057 | valp = get_local(vmg_ itmp); |
| 3058 | |
| 3059 | /* if it's an integer, handle it in-line */ |
| 3060 | if (valp->typ == VM_INT) |
| 3061 | { |
| 3062 | /* it's a number - just add the value */ |
| 3063 | INT_ADD(valp, get_op_int32(&p)); |
| 3064 | } |
| 3065 | else |
| 3066 | { |
| 3067 | /* get the number to add */ |
| 3068 | val2.set_int(get_op_int32(&p)); |
| 3069 | |
| 3070 | /* compute the sum */ |
| 3071 | p = compute_sum_lcl_imm(vmg_ valp, &val2, itmp, p); |
| 3072 | } |
| 3073 | } |
| 3074 | continue; |
| 3075 | |
| 3076 | case OPC_ADDTOLCL: |
| 3077 | { |
| 3078 | /* get the local */ |
| 3079 | int itmp = get_op_uint16(&p); |
| 3080 | valp = get_local(vmg_ itmp); |
| 3081 | |
| 3082 | /* get the value to add */ |
| 3083 | valp2 = get(0); |
| 3084 | |
| 3085 | /* if they're both integers, handle in-line */ |
| 3086 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 3087 | { |
| 3088 | /* add the value to the local */ |
| 3089 | INT_ADD(valp, valp2->val.intval); |
| 3090 | |
| 3091 | /* done with the addend */ |
| 3092 | discard(); |
| 3093 | } |
| 3094 | else |
| 3095 | { |
| 3096 | /* compute the sum, leaving the result in the local */ |
| 3097 | if (compute_sum(vmg_ valp, valp2)) |
| 3098 | { |
| 3099 | /* done - discard the addend */ |
| 3100 | discard(); |
| 3101 | } |
| 3102 | else |
| 3103 | { |
| 3104 | /* |
| 3105 | * check for an overload - do it recursively, |
| 3106 | * since we need to update the local on return |
| 3107 | */ |
| 3108 | push(valp2); |
| 3109 | p = op_overload(vmg_ p - entry_ptr_native_, itmp, |
| 3110 | valp, G_predef->operator_add, 1, |
| 3111 | VMERR_BAD_TYPE_ADD); |
| 3112 | } |
| 3113 | } |
| 3114 | } |
| 3115 | continue; |
| 3116 | |
| 3117 | case OPC_SUBFROMLCL: |
| 3118 | { |
| 3119 | /* get the local */ |
| 3120 | int itmp = get_op_uint16(&p); |
| 3121 | valp = get_local(vmg_ itmp); |
| 3122 | |
| 3123 | /* get the value to add */ |
| 3124 | valp2 = get(0); |
| 3125 | |
| 3126 | /* if they're both integers, handle in-line */ |
| 3127 | if (valp->typ == VM_INT && valp2->typ == VM_INT) |
| 3128 | { |
| 3129 | /* subtract the value from the local */ |
| 3130 | INT_SUB(valp, valp2->val.intval); |
| 3131 | |
| 3132 | /* discard the subtrahend */ |
| 3133 | discard(); |
| 3134 | } |
| 3135 | else if (compute_diff(vmg_ valp, valp2)) |
| 3136 | { |
| 3137 | /* done - discard the subtrahend */ |
| 3138 | discard(); |
| 3139 | } |
| 3140 | else |
| 3141 | { |
| 3142 | /* |
| 3143 | * no native implementation - check for an |
| 3144 | * overload; do this recursively since we need to |
| 3145 | * update the local on return |
| 3146 | */ |
| 3147 | push(valp2); |
| 3148 | p = op_overload(vmg_ p - entry_ptr_native_, itmp, |
| 3149 | valp, G_predef->operator_sub, 1, |
| 3150 | VMERR_BAD_TYPE_SUB); |
| 3151 | } |
| 3152 | } |
| 3153 | continue; |
| 3154 | |
| 3155 | case OPC_ZEROLCL1: |
| 3156 | /* get the local and set it to zero */ |
| 3157 | get_local(vmg_ get_op_uint8(&p))->set_int(0); |
| 3158 | continue; |
| 3159 | |
| 3160 | case OPC_ZEROLCL2: |
| 3161 | /* get the local and set it to zero */ |
| 3162 | get_local(vmg_ get_op_uint16(&p))->set_int(0); |
| 3163 | continue; |
| 3164 | |
| 3165 | case OPC_NILLCL1: |
| 3166 | /* get the local and set it to zero */ |
| 3167 | get_local(vmg_ get_op_uint8(&p))->set_nil(); |
| 3168 | continue; |
| 3169 | |
| 3170 | case OPC_NILLCL2: |
| 3171 | /* get the local and set it to zero */ |
| 3172 | get_local(vmg_ get_op_uint16(&p))->set_nil(); |
| 3173 | continue; |
| 3174 | |
| 3175 | case OPC_ONELCL1: |
| 3176 | /* get the local and set it to zero */ |
| 3177 | get_local(vmg_ get_op_uint8(&p))->set_int(1); |
| 3178 | continue; |
| 3179 | |
| 3180 | case OPC_ONELCL2: |
| 3181 | /* get the local and set it to zero */ |
| 3182 | get_local(vmg_ get_op_uint16(&p))->set_int(1); |
| 3183 | continue; |
| 3184 | |
| 3185 | case OPC_SETLCL2: |
| 3186 | /* get a pointer to the local */ |
| 3187 | valp = get_local(vmg_ get_op_uint16(&p)); |
| 3188 | |
| 3189 | /* pop the value into the local */ |
| 3190 | popval(vmg_ valp); |
| 3191 | continue; |
| 3192 | |
| 3193 | case OPC_SETARG1: |
| 3194 | /* get a pointer to the parameter */ |
| 3195 | valp = get_param(vmg_ get_op_uint8(&p)); |
| 3196 | |
| 3197 | /* pop the value into the parameter */ |
| 3198 | popval(vmg_ valp); |
| 3199 | continue; |
| 3200 | |
| 3201 | case OPC_SETARG2: |
| 3202 | /* get a pointer to the parameter */ |
| 3203 | valp = get_param(vmg_ get_op_uint16(&p)); |
| 3204 | |
| 3205 | /* pop the value into the parameter */ |
| 3206 | popval(vmg_ valp); |
| 3207 | continue; |
| 3208 | |
| 3209 | case OPC_SETIND: |
| 3210 | /* pop the index */ |
| 3211 | popval(vmg_ &val2); |
| 3212 | |
| 3213 | /* pop the value to be indexed */ |
| 3214 | popval(vmg_ &val); |
| 3215 | |
| 3216 | /* pop the value to assign */ |
| 3217 | popval(vmg_ &val3); |
| 3218 | |
| 3219 | /* assign into the index */ |
| 3220 | if (set_index(vmg_ &val, &val2, &val3)) |
| 3221 | { |
| 3222 | /* push the new container value */ |
| 3223 | pushval(vmg_ &val); |
| 3224 | } |
| 3225 | else |
| 3226 | { |
| 3227 | /* try operator overloading */ |
| 3228 | push(&val3); |
| 3229 | push(&val2); |
| 3230 | p = op_overload(vmg_ p - entry_ptr_native_, -1, |
| 3231 | &val, G_predef->operator_setidx, 2, |
| 3232 | VMERR_CANNOT_INDEX_TYPE); |
| 3233 | } |
| 3234 | continue; |
| 3235 | |
| 3236 | case OPC_SETINDLCL1I8: |
| 3237 | { |
| 3238 | /* get the local */ |
| 3239 | int itmp = get_op_uint8(&p); |
| 3240 | valp = get_local(vmg_ itmp); |
| 3241 | |
| 3242 | /* get the index value */ |
| 3243 | val2.set_int(get_op_uint8(&p)); |
| 3244 | |
| 3245 | /* pop the value to assign */ |
| 3246 | popval(vmg_ &val3); |
| 3247 | |
| 3248 | /* |
| 3249 | * set the index value - this will update the local |
| 3250 | * variable directly if the container value changes |
| 3251 | */ |
| 3252 | if (!set_index(vmg_ valp, &val2, &val3)) |
| 3253 | { |
| 3254 | /* try operator overloading */ |
| 3255 | push(&val3); |
| 3256 | push(&val2); |
| 3257 | p = op_overload(vmg_ p - entry_ptr_native_, itmp, |
| 3258 | valp, G_predef->operator_setidx, 2, |
| 3259 | VMERR_CANNOT_INDEX_TYPE); |
| 3260 | } |
| 3261 | } |
| 3262 | continue; |
| 3263 | |
| 3264 | case OPC_SETPROP: |
| 3265 | /* get the object whose property we're setting */ |
| 3266 | pop_obj(vmg_ &val); |
| 3267 | |
| 3268 | /* pop the value we're setting */ |
| 3269 | popval(vmg_ &val2); |
| 3270 | |
| 3271 | /* set the value */ |
| 3272 | set_prop(vmg_ val.val.obj, get_op_uint16(&p), &val2); |
| 3273 | continue; |
| 3274 | |
| 3275 | case OPC_PTRSETPROP: |
| 3276 | /* get the property and object to set */ |
| 3277 | pop_prop(vmg_ &val); |
| 3278 | pop_obj(vmg_ &val2); |
| 3279 | |
| 3280 | /* get the value to set */ |
| 3281 | popval(vmg_ &val3); |
| 3282 | |
| 3283 | /* set it */ |
| 3284 | set_prop(vmg_ val2.val.obj, val.val.prop, &val3); |
| 3285 | continue; |
| 3286 | |
| 3287 | case OPC_OBJSETPROP: |
| 3288 | /* get the object */ |
| 3289 | obj = (vm_obj_id_t)get_op_uint32(&p); |
| 3290 | |
| 3291 | /* get the new value */ |
| 3292 | popval(vmg_ &val); |
| 3293 | |
| 3294 | /* set the property */ |
| 3295 | set_prop(vmg_ obj, get_op_uint16(&p), &val); |
| 3296 | continue; |
| 3297 | |
| 3298 | case OPC_PUSHSTRI: |
| 3299 | /* push inline string */ |
| 3300 | { |
| 3301 | /* get the length prefix */ |
| 3302 | uint cnt = get_op_uint16(&p); |
| 3303 | |
| 3304 | /* create the new string from the inline data */ |
| 3305 | obj = CVmObjString::create( |
| 3306 | vmg_ FALSE, (const char *)p, cnt); |
| 3307 | |
| 3308 | /* skip past the string's bytes */ |
| 3309 | p += cnt; |
| 3310 | |
| 3311 | /* push the new string */ |
| 3312 | push()->set_obj(obj); |
| 3313 | } |
| 3314 | continue; |
| 3315 | |
| 3316 | case OPC_PUSHBIFPTR: |
| 3317 | { |
| 3318 | /* push pointer to built-in function */ |
| 3319 | uint idx = get_op_uint16(&p); |
| 3320 | push()->set_bifptr(get_op_uint16(&p), (ushort)idx); |
| 3321 | validate_bifptr(vmg0_); |
| 3322 | } |
| 3323 | continue; |
| 3324 | |
| 3325 | case OPC_THROW: |
| 3326 | /* pop the exception object */ |
| 3327 | pop_obj(vmg_ &val); |
| 3328 | |
| 3329 | /* |
| 3330 | * Throw it. Note that we pass the start of the current |
| 3331 | * instruction as the program counter, since we want to |
| 3332 | * find the exception handler (if any) for the current |
| 3333 | * instruction, not for the next instruction. |
| 3334 | */ |
| 3335 | if ((p = do_throw(vmg_ p - 1, val.val.obj)) == 0) |
| 3336 | { |
| 3337 | /* remember the unhandled exception for re-throwing */ |
| 3338 | unhandled_exc = val.val.obj; |
| 3339 | |
| 3340 | /* terminate execution */ |
| 3341 | goto exit_loop; |
| 3342 | } |
| 3343 | continue; |
| 3344 | |
| 3345 | #ifdef VM_DEBUGGER |
| 3346 | |
| 3347 | case OPC_GETPROPDATA: |
| 3348 | /* get the object whose property we're fetching */ |
| 3349 | pop(&propev.self); |
| 3350 | |
| 3351 | /* |
| 3352 | * if the object is not an object, it's one of the native |
| 3353 | * types, in which case we'll definitely run native code to |
| 3354 | * evaluate the property, in which case it's not valid for |
| 3355 | * speculative evaluation |
| 3356 | */ |
| 3357 | if (propev.self.typ != VM_OBJ) |
| 3358 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3359 | |
| 3360 | /* get the property */ |
| 3361 | prop = (vm_prop_id_t)get_op_uint16(&p); |
| 3362 | |
| 3363 | /* check validity for speculative evaluation */ |
| 3364 | check_prop_spec_eval(vmg_ propev.self.val.obj, prop); |
| 3365 | |
| 3366 | /* evaluate the property given by the immediate data */ |
| 3367 | p = propev.get_prop(vmg_ p - entry_ptr_native_, prop); |
| 3368 | continue; |
| 3369 | |
| 3370 | case OPC_PTRGETPROPDATA: |
| 3371 | /* get the property and object to evaluate */ |
| 3372 | pop_prop(vmg_ &val); |
| 3373 | pop(&propev.self); |
| 3374 | |
| 3375 | /* |
| 3376 | * if the object is not an object, it's one of the native |
| 3377 | * types, in which case we'll definitely run native code to |
| 3378 | * evaluate the property, in which case it's not valid for |
| 3379 | * speculative evaluation |
| 3380 | */ |
| 3381 | if (propev.self.typ != VM_OBJ) |
| 3382 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3383 | |
| 3384 | /* check validity for speculative evaluation */ |
| 3385 | check_prop_spec_eval(vmg_ propev.self.val.obj, val.val.prop); |
| 3386 | |
| 3387 | /* evaluate it */ |
| 3388 | p = propev.get_prop(vmg_ p - entry_ptr_native_, val.val.prop); |
| 3389 | continue; |
| 3390 | |
| 3391 | case OPC_GETDBARGC: |
| 3392 | /* push the argument count from the selected frame */ |
| 3393 | push_int(vmg_ get_argc_at_level(vmg_ get_op_uint16(&p) + 1)); |
| 3394 | continue; |
| 3395 | |
| 3396 | case OPC_GETDBLCL: |
| 3397 | { |
| 3398 | /* get the local variable number and stack level */ |
| 3399 | uint idx = get_op_uint16(&p); |
| 3400 | int level = get_op_uint16(&p); |
| 3401 | |
| 3402 | /* push the value */ |
| 3403 | pushval(vmg_ get_local_at_level(vmg_ idx, level + 1)); |
| 3404 | } |
| 3405 | continue; |
| 3406 | |
| 3407 | case OPC_GETDBARG: |
| 3408 | { |
| 3409 | /* get the parameter variable number and stack level */ |
| 3410 | uint idx = get_op_uint16(&p); |
| 3411 | int level = get_op_uint16(&p); |
| 3412 | |
| 3413 | /* push the value */ |
| 3414 | pushval(vmg_ get_param_at_level(vmg_ idx, level + 1)); |
| 3415 | } |
| 3416 | continue; |
| 3417 | |
| 3418 | case OPC_SETDBLCL: |
| 3419 | { |
| 3420 | /* get the local variable number and stack level */ |
| 3421 | uint idx = get_op_uint16(&p); |
| 3422 | int level = get_op_uint16(&p); |
| 3423 | |
| 3424 | /* get the local pointer */ |
| 3425 | vm_val_t *valp = get_local_at_level(vmg_ idx, level + 1); |
| 3426 | |
| 3427 | /* pop the value into the local */ |
| 3428 | popval(vmg_ valp); |
| 3429 | } |
| 3430 | continue; |
| 3431 | |
| 3432 | case OPC_SETDBARG: |
| 3433 | { |
| 3434 | /* get the parameter variable number and stack level */ |
| 3435 | uint idx = get_op_uint16(&p); |
| 3436 | int level = get_op_uint16(&p); |
| 3437 | |
| 3438 | /* get the parameter pointer */ |
| 3439 | vm_val_t *valp = get_param_at_level(vmg_ idx, level + 1); |
| 3440 | |
| 3441 | /* pop the value into the local */ |
| 3442 | popval(vmg_ valp); |
| 3443 | } |
| 3444 | continue; |
| 3445 | |
| 3446 | case OPC_BP: |
| 3447 | /* step back to the breakpoint location itself */ |
| 3448 | --p; |
| 3449 | |
| 3450 | /* let the debugger take control */ |
| 3451 | G_debugger->step(vmg_ &p, entry_ptr_native_, TRUE, 0); |
| 3452 | |
| 3453 | /* if we're halting, get out of here */ |
| 3454 | if (halt_vm_) |
| 3455 | err_throw(VMERR_DBG_HALT); |
| 3456 | |
| 3457 | /* |
| 3458 | * go back and execute the current instruction - bypass |
| 3459 | * single-step tracing into the debugger in this case, |
| 3460 | * since the debugger expects when it returns that one |
| 3461 | * instruction will always be traced before the debugger is |
| 3462 | * re-entered |
| 3463 | */ |
| 3464 | goto exec_instruction; |
| 3465 | |
| 3466 | #else /* VM_DEBUGGER */ |
| 3467 | |
| 3468 | case OPC_GETDBARGC: |
| 3469 | case OPC_GETDBLCL: |
| 3470 | case OPC_GETDBARG: |
| 3471 | case OPC_SETDBLCL: |
| 3472 | case OPC_SETDBARG: |
| 3473 | case OPC_GETPROPDATA: |
| 3474 | case OPC_PTRGETPROPDATA: |
| 3475 | err_throw(VMERR_NO_DEBUGGER); |
| 3476 | continue; |
| 3477 | |
| 3478 | case OPC_BP: |
| 3479 | /* if there's no debugger, it's an error */ |
| 3480 | err_throw(VMERR_BREAKPOINT); |
| 3481 | continue; |
| 3482 | |
| 3483 | #endif /* VM_DEBUGGER */ |
| 3484 | |
| 3485 | case OPC_CALLEXT: |
| 3486 | //$$$ |
| 3487 | err_throw(VMERR_CALLEXT_NOT_IMPL); |
| 3488 | continue; |
| 3489 | |
| 3490 | #ifdef OS_FILL_OUT_CASE_TABLES |
| 3491 | /* |
| 3492 | * Since we this switch is the innermost inner loop of the VM, |
| 3493 | * we go to some extra lengths to optimize it where possible. |
| 3494 | * See tads2/osifc.h for information on how to use |
| 3495 | * OS_FILL_OUT_CASE_TABLES and OS_IMPOSSIBLE_DEFAULT_CASE. |
| 3496 | * |
| 3497 | * Our controlling expression is an unsigned character value, |
| 3498 | * so we know the range of possible values will be limited to |
| 3499 | * 0-255. Therefore, we simply need to provide a "case" |
| 3500 | * alternative for every invalid opcode. To further encourage |
| 3501 | * the compiler to favor speed here, we specifically put |
| 3502 | * different code in every one of these case alternatives, to |
| 3503 | * force the compiler to generate a separate jump location for |
| 3504 | * each one; some compilers will generate a two-level jump |
| 3505 | * table if many cases point to shared code, to reduce the size |
| 3506 | * of the table, but we don't want that here because this |
| 3507 | * switch is critical to VM performance so we want it as fast |
| 3508 | * as possible. |
| 3509 | */ |
| 3510 | case 0x00: val.val.intval = 0x00; |
| 3511 | case 0x11: val.val.intval = 0x11; |
| 3512 | case 0x12: val.val.intval = 0x12; |
| 3513 | case 0x13: val.val.intval = 0x13; |
| 3514 | case 0x14: val.val.intval = 0x14; |
| 3515 | case 0x15: val.val.intval = 0x15; |
| 3516 | case 0x16: val.val.intval = 0x16; |
| 3517 | case 0x17: val.val.intval = 0x17; |
| 3518 | case 0x18: val.val.intval = 0x18; |
| 3519 | case 0x19: val.val.intval = 0x19; |
| 3520 | case 0x1A: val.val.intval = 0x1A; |
| 3521 | case 0x1B: val.val.intval = 0x1B; |
| 3522 | case 0x1C: val.val.intval = 0x1C; |
| 3523 | case 0x1D: val.val.intval = 0x1D; |
| 3524 | case 0x1E: val.val.intval = 0x1E; |
| 3525 | case 0x1F: val.val.intval = 0x1F; |
| 3526 | case 0x31: val.val.intval = 0x31; |
| 3527 | case 0x32: val.val.intval = 0x32; |
| 3528 | case 0x33: val.val.intval = 0x33; |
| 3529 | case 0x34: val.val.intval = 0x34; |
| 3530 | case 0x35: val.val.intval = 0x35; |
| 3531 | case 0x36: val.val.intval = 0x36; |
| 3532 | case 0x37: val.val.intval = 0x37; |
| 3533 | case 0x38: val.val.intval = 0x38; |
| 3534 | case 0x39: val.val.intval = 0x39; |
| 3535 | case 0x3A: val.val.intval = 0x3A; |
| 3536 | case 0x3B: val.val.intval = 0x3B; |
| 3537 | case 0x3C: val.val.intval = 0x3C; |
| 3538 | case 0x3D: val.val.intval = 0x3D; |
| 3539 | case 0x3E: val.val.intval = 0x3E; |
| 3540 | case 0x3F: val.val.intval = 0x3F; |
| 3541 | case 0x46: val.val.intval = 0x46; |
| 3542 | case 0x47: val.val.intval = 0x47; |
| 3543 | case 0x48: val.val.intval = 0x48; |
| 3544 | case 0x49: val.val.intval = 0x49; |
| 3545 | case 0x4A: val.val.intval = 0x4A; |
| 3546 | case 0x4B: val.val.intval = 0x4B; |
| 3547 | case 0x4C: val.val.intval = 0x4C; |
| 3548 | case 0x4D: val.val.intval = 0x4D; |
| 3549 | case 0x4E: val.val.intval = 0x4E; |
| 3550 | case 0x4F: val.val.intval = 0x4F; |
| 3551 | case 0x53: val.val.intval = 0x53; |
| 3552 | case 0x55: val.val.intval = 0x55; |
| 3553 | case 0x5A: val.val.intval = 0x5A; |
| 3554 | case 0x5B: val.val.intval = 0x5B; |
| 3555 | case 0x5C: val.val.intval = 0x5C; |
| 3556 | case 0x5D: val.val.intval = 0x5D; |
| 3557 | case 0x5E: val.val.intval = 0x5E; |
| 3558 | case 0x5F: val.val.intval = 0x5F; |
| 3559 | case 0x6E: val.val.intval = 0x6E; |
| 3560 | case 0x6F: val.val.intval = 0x6F; |
| 3561 | case 0x70: val.val.intval = 0x70; |
| 3562 | case 0x71: val.val.intval = 0x71; |
| 3563 | case 0x79: val.val.intval = 0x7D; |
| 3564 | // case 0xA2: val.val.intval = 0xA2; |
| 3565 | // case 0xA3: val.val.intval = 0xA3; |
| 3566 | // case 0xA4: val.val.intval = 0xA4; |
| 3567 | // case 0xA5: val.val.intval = 0xA5; |
| 3568 | case 0xA7: val.val.intval = 0xA7; |
| 3569 | case 0xA8: val.val.intval = 0xA8; |
| 3570 | case 0xA9: val.val.intval = 0xA9; |
| 3571 | case 0xBD: val.val.intval = 0xBD; |
| 3572 | case 0xBE: val.val.intval = 0xBE; |
| 3573 | case 0xBF: val.val.intval = 0xBF; |
| 3574 | case 0xC4: val.val.intval = 0xC4; |
| 3575 | case 0xC5: val.val.intval = 0xC5; |
| 3576 | case 0xC6: val.val.intval = 0xC6; |
| 3577 | case 0xC7: val.val.intval = 0xC7; |
| 3578 | case 0xC8: val.val.intval = 0xC8; |
| 3579 | case 0xC9: val.val.intval = 0xC9; |
| 3580 | case 0xCA: val.val.intval = 0xCA; |
| 3581 | case 0xCB: val.val.intval = 0xCB; |
| 3582 | case 0xCC: val.val.intval = 0xCC; |
| 3583 | case 0xCD: val.val.intval = 0xCD; |
| 3584 | case 0xCE: val.val.intval = 0xCE; |
| 3585 | case 0xCF: val.val.intval = 0xCF; |
| 3586 | case 0xDC: val.val.intval = 0xDC; |
| 3587 | case 0xDD: val.val.intval = 0xDD; |
| 3588 | case 0xDE: val.val.intval = 0xDE; |
| 3589 | case 0xDF: val.val.intval = 0xDF; |
| 3590 | case 0xF0: val.val.intval = 0xF0; |
| 3591 | case 0xF3: val.val.intval = 0xF3; |
| 3592 | case 0xF4: val.val.intval = 0xF4; |
| 3593 | case 0xF5: val.val.intval = 0xF5; |
| 3594 | case 0xF6: val.val.intval = 0xF6; |
| 3595 | case 0xF7: val.val.intval = 0xF7; |
| 3596 | case 0xF8: val.val.intval = 0xF8; |
| 3597 | case 0xF9: val.val.intval = 0xF9; |
| 3598 | case 0xFA: val.val.intval = 0xFA; |
| 3599 | case 0xFB: val.val.intval = 0xFB; |
| 3600 | case 0xFC: val.val.intval = 0xFC; |
| 3601 | case 0xFD: val.val.intval = 0xFD; |
| 3602 | case 0xFE: val.val.intval = 0xFE; |
| 3603 | case 0xFF: val.val.intval = 0xFF; |
| 3604 | err_throw(VMERR_INVALID_OPCODE); |
| 3605 | |
| 3606 | OS_IMPOSSIBLE_DEFAULT_CASE |
| 3607 | |
| 3608 | #else /* OS_FILL_OUT_CASE_TABLES */ |
| 3609 | case 0: |
| 3610 | /* |
| 3611 | * Explicitly call out this invalid instruction case so |
| 3612 | * that we can avoid extra work in computing the switch. |
| 3613 | * Some compilers will be smart enough to observe that we |
| 3614 | * populate the full range of possible values (0-255) for |
| 3615 | * the datatype of the switch control expression, and thus |
| 3616 | * will build jump tables that can be jumped through |
| 3617 | * without range-checking the value. (No range checking |
| 3618 | * is necessary, because a uchar simply cannot hold any |
| 3619 | * values outside of the 0-255 range.) This doesn't |
| 3620 | * guarantee that the compiler will be smart, but it does |
| 3621 | * help with some compilers and shouldn't hurt performance |
| 3622 | * with those that don't make any use of the situation. |
| 3623 | */ |
| 3624 | err_throw(VMERR_INVALID_OPCODE); |
| 3625 | |
| 3626 | case 0xFF: |
| 3627 | /* |
| 3628 | * explicitly call out this invalid instruction for the |
| 3629 | * same reasons we call out case 0 above |
| 3630 | */ |
| 3631 | err_throw(VMERR_INVALID_OPCODE); |
| 3632 | |
| 3633 | default: |
| 3634 | /* unrecognized opcode */ |
| 3635 | err_throw(VMERR_INVALID_OPCODE); |
| 3636 | continue; |
| 3637 | |
| 3638 | #endif /* OS_FILL_OUT_CASE_TABLES */ |
| 3639 | } |
| 3640 | } |
| 3641 | |
| 3642 | /* |
| 3643 | * We jump to this label when it's time to terminate execution |
| 3644 | * and return to the host environment which called us. |
| 3645 | */ |
| 3646 | exit_loop: |
| 3647 | /* note that we're ready to return */ |
| 3648 | done = TRUE; |
| 3649 | } |
| 3650 | err_catch(err) |
| 3651 | { |
| 3652 | int i; |
| 3653 | volatile int released_reserve = FALSE; |
| 3654 | |
| 3655 | /* if the debugger has halted the VM, re-throw the error */ |
| 3656 | VM_IF_DEBUGGER(if (halt_vm_) err_rethrow();) |
| 3657 | |
| 3658 | err_try |
| 3659 | { |
| 3660 | /* |
| 3661 | * Return to the start of the most recent instruction - we've |
| 3662 | * already at least partially decoded the instruction, so we |
| 3663 | * won't be pointing to its first byte. Note that last_pc is |
| 3664 | * a non-register variable (because we take its address to |
| 3665 | * store in pc_ptr_), so it will correctly indicate the |
| 3666 | * current instruction even though we've jumped here via |
| 3667 | * longjmp. |
| 3668 | */ |
| 3669 | p = last_pc; |
| 3670 | |
| 3671 | /* |
| 3672 | * Create a new exception object to describe the error. The |
| 3673 | * arguments to the constructor are the error number and the |
| 3674 | * error parameters. |
| 3675 | * |
| 3676 | * If the error code is "unhandled exception," it means that |
| 3677 | * an exception occurred in a recursive interpreter |
| 3678 | * invocation, and the exception wasn't handled within the |
| 3679 | * code called recursively; in this case, we can simply |
| 3680 | * re-throw the original error, and perhaps handle it in the |
| 3681 | * context of the current code. |
| 3682 | */ |
| 3683 | if (err->get_error_code() == VMERR_UNHANDLED_EXC) |
| 3684 | { |
| 3685 | /* get the original exception object from the error stack */ |
| 3686 | obj = (vm_obj_id_t)err->get_param_ulong(0); |
| 3687 | } |
| 3688 | else |
| 3689 | { |
| 3690 | /* step into the debugger, if it's present */ |
| 3691 | VM_IF_DEBUGGER( |
| 3692 | { |
| 3693 | const uchar *dbgp; |
| 3694 | |
| 3695 | /* |
| 3696 | * If we're in the process of halting the VM, don't |
| 3697 | * bother stepping into the debugger. We'll check the |
| 3698 | * same thing in a moment, after we get back from |
| 3699 | * stepping into the debugger, but this check isn't |
| 3700 | * redundant: we could already be halting even before |
| 3701 | * we enter the debugger here, because we could be |
| 3702 | * unwinding the native (C++) error stack on our way |
| 3703 | * out from such a halt. |
| 3704 | */ |
| 3705 | if (halt_vm_) |
| 3706 | { |
| 3707 | done = TRUE; |
| 3708 | goto skip_throw; |
| 3709 | } |
| 3710 | |
| 3711 | /* make a copy of the PC for the debugger's use */ |
| 3712 | dbgp = p; |
| 3713 | |
| 3714 | /* step into the debugger */ |
| 3715 | G_debugger->step(vmg_ &dbgp, entry_ptr_native_, FALSE, |
| 3716 | err->get_error_code()); |
| 3717 | |
| 3718 | /* |
| 3719 | * If the VM was halted while in the debugger, throw |
| 3720 | * the DEBUG HALT error. (We formerly just exited the |
| 3721 | * byte-code loop, but that has the effect of returning |
| 3722 | * to a native method or function caller, and allowing |
| 3723 | * the native caller to continue to run. We don't want |
| 3724 | * that to happen because we can't process the normal |
| 3725 | * unwinding on the return - that would require VM ops, |
| 3726 | * and we've halted the VM. So, to explicitly unwind |
| 3727 | * any native callers, we have to throw an exception.) |
| 3728 | */ |
| 3729 | if (halt_vm_) |
| 3730 | err_throw(VMERR_DBG_HALT); |
| 3731 | |
| 3732 | /* |
| 3733 | * if they moved the execution pointer, resume |
| 3734 | * execution at the new point, discarding the |
| 3735 | * exception |
| 3736 | */ |
| 3737 | if (dbgp != p) |
| 3738 | { |
| 3739 | /* resume execution at the new location */ |
| 3740 | p = dbgp; |
| 3741 | |
| 3742 | /* discard the exception and resume execution */ |
| 3743 | goto skip_throw; |
| 3744 | } |
| 3745 | } |
| 3746 | ); |
| 3747 | |
| 3748 | /* |
| 3749 | * If this is a stack overflow exception, there's probably |
| 3750 | * not enough stack left to create the exception object. |
| 3751 | * Fortunately, we have an emergency stack reserve just for |
| 3752 | * such conditions, so release it now, hopefully giving us |
| 3753 | * enough room to work with to construct the exception. |
| 3754 | */ |
| 3755 | if (err->get_error_code() == VMERR_STACK_OVERFLOW) |
| 3756 | released_reserve = release_reserve(); |
| 3757 | |
| 3758 | /* push the error parameters (in reverse order) */ |
| 3759 | for (i = err->get_param_count() ; i > 0 ; ) |
| 3760 | { |
| 3761 | /* go to the next parameter */ |
| 3762 | --i; |
| 3763 | |
| 3764 | /* see what we have and push an appropriate value */ |
| 3765 | switch(err->get_param_type(i-1)) |
| 3766 | { |
| 3767 | case ERR_TYPE_INT: |
| 3768 | /* push the integer value */ |
| 3769 | push_int(vmg_ err->get_param_int(i)); |
| 3770 | break; |
| 3771 | |
| 3772 | case ERR_TYPE_ULONG: |
| 3773 | /* push the value */ |
| 3774 | push_int(vmg_ (int32_t)err->get_param_ulong(i)); |
| 3775 | break; |
| 3776 | |
| 3777 | case ERR_TYPE_TEXTCHAR: |
| 3778 | /* push a new string with the text */ |
| 3779 | push_obj(vmg_ CVmObjString::create(vmg_ FALSE, |
| 3780 | err->get_param_text(i), |
| 3781 | get_strlen(err->get_param_text(i)))); |
| 3782 | break; |
| 3783 | |
| 3784 | case ERR_TYPE_CHAR: |
| 3785 | /* push a new string with the text */ |
| 3786 | push_obj(vmg_ CVmObjString::create(vmg_ FALSE, |
| 3787 | err->get_param_char(i), |
| 3788 | strlen(err->get_param_char(i)))); |
| 3789 | break; |
| 3790 | |
| 3791 | default: |
| 3792 | /* unrecognized type - push nil for now */ |
| 3793 | push_nil(vmg0_); |
| 3794 | break; |
| 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | /* |
| 3799 | * if there's a RuntimeError base class defined, create an |
| 3800 | * instance; otherwise, create a simple instance of the |
| 3801 | * basic object type to throw as a placeholder, since the |
| 3802 | * program hasn't made any provision to catch run-time |
| 3803 | * errors |
| 3804 | */ |
| 3805 | if (G_predef->rterr != VM_INVALID_OBJ) |
| 3806 | { |
| 3807 | /* push the error number */ |
| 3808 | push_int(vmg_ err->get_error_code()); |
| 3809 | |
| 3810 | /* |
| 3811 | * Create the new RuntimeException instance. Run the |
| 3812 | * constructor in a recursive invocation of the |
| 3813 | * interpreter (by passing a null PC pointer). |
| 3814 | */ |
| 3815 | vm_objp(vmg_ G_predef->rterr) |
| 3816 | ->create_instance(vmg_ G_predef->rterr, 0, |
| 3817 | err->get_param_count() + 1); |
| 3818 | |
| 3819 | /* get the object from R0 */ |
| 3820 | if (r0_.typ != VM_OBJ) |
| 3821 | err_throw(VMERR_EXCEPTION_OBJ_REQD); |
| 3822 | obj = r0_.val.obj; |
| 3823 | } |
| 3824 | else |
| 3825 | { |
| 3826 | /* |
| 3827 | * There's no RuntimeError object defined by the image |
| 3828 | * file, so create a basic object to throw. This |
| 3829 | * won't convey any information to the program except |
| 3830 | * that it's not one of the errors they're expecting; |
| 3831 | * this is fine, since they have made no provisions to |
| 3832 | * catch VM errors, as demonstrated by their lack of a |
| 3833 | * RuntimeError definition. |
| 3834 | */ |
| 3835 | obj = CVmObjTads::create(vmg_ FALSE, 0, 1); |
| 3836 | } |
| 3837 | |
| 3838 | /* |
| 3839 | * if possible, set the exceptionMessage property in the |
| 3840 | * new exception object to the default error message for |
| 3841 | * the run-time error we're processing |
| 3842 | */ |
| 3843 | if (G_predef->rterrmsg_prop != VM_INVALID_PROP) |
| 3844 | { |
| 3845 | const char *msg; |
| 3846 | char buf[256]; |
| 3847 | vm_obj_id_t str_obj; |
| 3848 | |
| 3849 | /* format the message text */ |
| 3850 | msg = err_get_msg(vm_messages, vm_message_count, |
| 3851 | err->get_error_code(), FALSE); |
| 3852 | err_format_msg(buf, sizeof(buf), msg, err); |
| 3853 | |
| 3854 | /* |
| 3855 | * momentarily push the new exception object, so we |
| 3856 | * don't lose track of it if we run garbage collection |
| 3857 | * here |
| 3858 | */ |
| 3859 | push_obj(vmg_ obj); |
| 3860 | |
| 3861 | /* create a string object with the message text */ |
| 3862 | str_obj = |
| 3863 | CVmObjString::create(vmg_ FALSE, buf, strlen(buf)); |
| 3864 | |
| 3865 | /* |
| 3866 | * before we can build a stack trace, let the debugger |
| 3867 | * synchronize its current position information |
| 3868 | */ |
| 3869 | VM_IF_DEBUGGER( |
| 3870 | G_debugger->sync_exec_pos(vmg_ p, entry_ptr_native_)); |
| 3871 | |
| 3872 | /* set the property in the new object */ |
| 3873 | val.set_obj(str_obj); |
| 3874 | vm_objp(vmg_ obj) |
| 3875 | ->set_prop(vmg_ G_undo, obj, |
| 3876 | G_predef->rterrmsg_prop, &val); |
| 3877 | |
| 3878 | /* we don't need gc protection any more */ |
| 3879 | discard(); |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | /* |
| 3884 | * If we released the stack reserve, take it back. We've |
| 3885 | * finished creating the exception object, so we don't need the |
| 3886 | * emergency stack space any more. We want to put it back now |
| 3887 | * that we're done with it so that it'll be there for us if we |
| 3888 | * should run into another stack overflow in the future. |
| 3889 | */ |
| 3890 | if (released_reserve) |
| 3891 | recover_reserve(); |
| 3892 | |
| 3893 | /* throw the exception */ |
| 3894 | if ((p = do_throw(vmg_ p, obj)) == 0) |
| 3895 | { |
| 3896 | /* remember the unhandled exception for a moment */ |
| 3897 | unhandled_exc = obj; |
| 3898 | } |
| 3899 | |
| 3900 | /* come here to skip throwing the exception */ |
| 3901 | VM_IF_DEBUGGER(skip_throw: ); |
| 3902 | } |
| 3903 | err_catch_disc |
| 3904 | { |
| 3905 | /* |
| 3906 | * we got another exception trying to handle the first |
| 3907 | * exception - just throw the error again, but at least clean |
| 3908 | * up statics on the way out |
| 3909 | */ |
| 3910 | pc_ptr_ = old_pc_ptr; |
| 3911 | |
| 3912 | /* if we released the stack reserve, take it back */ |
| 3913 | if (released_reserve) |
| 3914 | recover_reserve(); |
| 3915 | |
| 3916 | /* re-throw the error */ |
| 3917 | err_rethrow(); |
| 3918 | } |
| 3919 | err_end; |
| 3920 | } |
| 3921 | err_end; |
| 3922 | |
| 3923 | /* |
| 3924 | * If an unhandled exception occurred, re-throw it. This will wrap our |
| 3925 | * exception object in a C++ object and throw it through our C++ |
| 3926 | * err_try/err_catch exception mechanism, so that the exception is |
| 3927 | * thrown out of the recursive native-code invoker. |
| 3928 | */ |
| 3929 | if (unhandled_exc != VM_INVALID_OBJ) |
| 3930 | { |
| 3931 | /* restore the enclosing PC pointer */ |
| 3932 | pc_ptr_ = old_pc_ptr; |
| 3933 | |
| 3934 | /* re-throw the unhandled exception */ |
| 3935 | err_throw_a(VMERR_UNHANDLED_EXC, 1, |
| 3936 | ERR_TYPE_ULONG, (unsigned long)unhandled_exc); |
| 3937 | } |
| 3938 | |
| 3939 | /* if we're not done, go back and resume execution */ |
| 3940 | if (!done) |
| 3941 | goto resume_execution; |
| 3942 | |
| 3943 | /* restore the enclosing PC pointer */ |
| 3944 | pc_ptr_ = old_pc_ptr; |
| 3945 | } |
| 3946 | |
| 3947 | |
| 3948 | /* ------------------------------------------------------------------------ */ |
| 3949 | /* |
| 3950 | * Validate the built-in function pointer at top of stack |
| 3951 | */ |
| 3952 | void CVmRun::validate_bifptr(VMG0_) |
| 3953 | { |
| 3954 | /* get the top of stack */ |
| 3955 | vm_val_t *bifp = G_stk->get(0); |
| 3956 | |
| 3957 | /* validate the entry in the bif table */ |
| 3958 | if (!G_bif_table->validate_entry( |
| 3959 | bifp->val.bifptr.set_idx, bifp->val.bifptr.func_idx)) |
| 3960 | { |
| 3961 | /* invalid entry - throw an error */ |
| 3962 | char buf[20]; |
| 3963 | sprintf(buf, "#%d", bifp->val.bifptr.set_idx); |
| 3964 | err_throw_a(VMERR_UNAVAIL_INTRINSIC, 2, |
| 3965 | ERR_TYPE_INT, bifp->val.bifptr.func_idx, |
| 3966 | ERR_TYPE_CHAR, buf); |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | /* ------------------------------------------------------------------------ */ |
| 3971 | /* |
| 3972 | * Create a method context object suitable for use with the STORECTX and |
| 3973 | * LOADCTX instructions. |
| 3974 | */ |
| 3975 | void CVmRun::create_loadctx_obj(VMG_ vm_val_t *result, |
| 3976 | vm_obj_id_t self, |
| 3977 | vm_obj_id_t defobj, |
| 3978 | vm_obj_id_t targobj, |
| 3979 | vm_prop_id_t targprop) |
| 3980 | { |
| 3981 | char buf[VMB_LEN + 4*VMB_DATAHOLDER]; |
| 3982 | |
| 3983 | /* initialize a list template for a four-element list */ |
| 3984 | vmb_put_len(buf, 4); |
| 3985 | |
| 3986 | /* |
| 3987 | * put the list elements: 'self', targetprop, original target object, |
| 3988 | * and defining object |
| 3989 | */ |
| 3990 | vmb_put_dh_obj(buf + VMB_LEN, self); |
| 3991 | vmb_put_dh_prop(buf + VMB_LEN + VMB_DATAHOLDER, targprop); |
| 3992 | vmb_put_dh_obj(buf + VMB_LEN + 2*VMB_DATAHOLDER, targobj); |
| 3993 | vmb_put_dh_obj(buf + VMB_LEN + 3*VMB_DATAHOLDER, defobj); |
| 3994 | |
| 3995 | /* create and return a new list copied from our prepared buffer */ |
| 3996 | result->set_obj(CVmObjList::create(vmg_ FALSE, buf)); |
| 3997 | } |
| 3998 | |
| 3999 | |
| 4000 | /* ------------------------------------------------------------------------ */ |
| 4001 | /* |
| 4002 | * Get a local variable, parameter, or context local from a stack frame. |
| 4003 | */ |
| 4004 | void CVmRun::get_local_from_frame(VMG_ vm_val_t *val, vm_val_t *fp, |
| 4005 | const CVmDbgFrameSymPtr *symp) |
| 4006 | { |
| 4007 | get_local_from_frame(vmg_ val, fp, symp->get_var_num(), symp->is_param(), |
| 4008 | symp->is_ctx_local(), symp->get_ctx_arr_idx()); |
| 4009 | } |
| 4010 | |
| 4011 | void CVmRun::get_local_from_frame(VMG_ vm_val_t *val, vm_val_t *fp, |
| 4012 | int varnum, int is_param, |
| 4013 | int is_ctx_local, int ctx_arr_idx) |
| 4014 | { |
| 4015 | /* check which kind of variable we have */ |
| 4016 | if (is_param) |
| 4017 | { |
| 4018 | /* parameter - get it from the frame */ |
| 4019 | *val = *get_param_from_frame(vmg_ fp, varnum); |
| 4020 | } |
| 4021 | else if (is_ctx_local) |
| 4022 | { |
| 4023 | /* context local - get the context local */ |
| 4024 | vm_val_t *cl = get_local_from_frame(vmg_ fp, varnum); |
| 4025 | |
| 4026 | /* index it to get the local */ |
| 4027 | vm_val_t idxval; |
| 4028 | idxval.set_int(ctx_arr_idx); |
| 4029 | apply_index(vmg_ val, cl, &idxval); |
| 4030 | } |
| 4031 | else |
| 4032 | { |
| 4033 | /* regular local - get it from the frame */ |
| 4034 | *val = *get_local_from_frame(vmg_ fp, varnum); |
| 4035 | } |
| 4036 | } |
| 4037 | |
| 4038 | /* ------------------------------------------------------------------------ */ |
| 4039 | /* |
| 4040 | * Set a local variable, parameter, or context local in a stack frame. |
| 4041 | */ |
| 4042 | void CVmRun::set_local_in_frame(VMG_ const vm_val_t *val, vm_val_t *fp, |
| 4043 | const CVmDbgFrameSymPtr *symp) |
| 4044 | { |
| 4045 | set_local_in_frame(vmg_ val, fp, symp->get_var_num(), symp->is_param(), |
| 4046 | symp->is_ctx_local(), symp->get_ctx_arr_idx()); |
| 4047 | } |
| 4048 | |
| 4049 | void CVmRun::set_local_in_frame(VMG_ const vm_val_t *val, vm_val_t *fp, |
| 4050 | int varnum, int is_param, |
| 4051 | int is_ctx_local, int ctx_arr_idx) |
| 4052 | { |
| 4053 | /* check which kind of variable we have */ |
| 4054 | if (is_param) |
| 4055 | { |
| 4056 | /* parameter - get it from the frame */ |
| 4057 | *get_param_from_frame(vmg_ fp, varnum) = *val; |
| 4058 | } |
| 4059 | else if (is_ctx_local) |
| 4060 | { |
| 4061 | /* context local - get the context local */ |
| 4062 | vm_val_t *cl = get_local_from_frame(vmg_ fp, varnum); |
| 4063 | |
| 4064 | /* index it to set the local */ |
| 4065 | vm_val_t idxval; |
| 4066 | idxval.set_int(ctx_arr_idx); |
| 4067 | set_index(vmg_ cl, &idxval, val); |
| 4068 | } |
| 4069 | else |
| 4070 | { |
| 4071 | /* regular local - set it in the frame */ |
| 4072 | *get_local_from_frame(vmg_ fp, varnum) = *val; |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | /* ------------------------------------------------------------------------ */ |
| 4077 | /* |
| 4078 | * Get a named argument table from a stack frame. Returns a pointer to the |
| 4079 | * table if we find one, null if not. Fills in arg0 with the first named |
| 4080 | * argument in the stack frame; the nth named argument is at (arg0+n). |
| 4081 | */ |
| 4082 | const uchar *CVmRun::get_named_args_from_frame( |
| 4083 | VMG_ vm_val_t *fp, vm_val_t **arg0) |
| 4084 | { |
| 4085 | /* get the return address */ |
| 4086 | pool_ofs_t ofs = G_interpreter->get_return_ofs_from_frame(vmg_ fp); |
| 4087 | const uchar *ep = |
| 4088 | G_interpreter->get_enclosing_entry_ptr_from_frame(vmg_ fp); |
| 4089 | const vm_rcdesc *rc = G_interpreter->get_rcdesc_from_frame(vmg_ fp); |
| 4090 | |
| 4091 | /* check for a recursive native code caller */ |
| 4092 | if (ofs == 0 && rc != 0 && rc->has_return_addr()) |
| 4093 | { |
| 4094 | /* get the return address from the descriptor */ |
| 4095 | ofs = rc->get_return_addr() - ep; |
| 4096 | } |
| 4097 | |
| 4098 | /* if there's no return address, there's no table */ |
| 4099 | if (ep == 0 || ofs == 0) |
| 4100 | return 0; |
| 4101 | |
| 4102 | /* get the return instruction pointer */ |
| 4103 | const uchar *ip = ep + ofs; |
| 4104 | |
| 4105 | /* if it's a NamedArgPtr instruction, follows the pointer */ |
| 4106 | if (*ip == OPC_NAMEDARGPTR) |
| 4107 | ip += 2 + osrp2(ip+2); |
| 4108 | |
| 4109 | /* make sure we're at a NamedArgTab instruction */ |
| 4110 | if (*ip == OPC_NAMEDARGTAB) |
| 4111 | { |
| 4112 | /* |
| 4113 | * We have a named argument table. Fill in *arg0 with a pointer to |
| 4114 | * the first named argument value in the stack frame. |
| 4115 | * |
| 4116 | * If there's a native caller at this level, get the arguments to |
| 4117 | * the native caller instead of the arguments to the bytecode |
| 4118 | * callee. Native callers never send named arguments, so we can |
| 4119 | * ignore the callee's arguments and look only at the native |
| 4120 | * caller's arguments. |
| 4121 | */ |
| 4122 | int argc; |
| 4123 | if (rc != 0) |
| 4124 | { |
| 4125 | /* native caller - get the native code's arguments */ |
| 4126 | argc = rc->argc; |
| 4127 | *arg0 = rc->argp - argc; |
| 4128 | } |
| 4129 | else |
| 4130 | { |
| 4131 | /* bytecode caller - get the current level's arguments */ |
| 4132 | argc = G_interpreter->get_argc_from_frame(vmg_ fp); |
| 4133 | *arg0 = G_interpreter->get_param_from_frame(vmg_ fp, argc); |
| 4134 | } |
| 4135 | |
| 4136 | /* skip the NamedArgTab opcode and table length prefix */ |
| 4137 | ip += 1 + 2; |
| 4138 | |
| 4139 | /* |
| 4140 | * Adjust arg0 for the number of arguments. The compiler generates |
| 4141 | * the names in the same order as the pushes, so the first name is |
| 4142 | * the first value pushed. |
| 4143 | */ |
| 4144 | *arg0 -= (osrp2(ip) - 1); |
| 4145 | |
| 4146 | /* |
| 4147 | * return the table pointer - it starts after the NamedArgTab |
| 4148 | * instruction and table byte length prefix |
| 4149 | */ |
| 4150 | return ip; |
| 4151 | } |
| 4152 | |
| 4153 | /* there's no named argument table in this return frame */ |
| 4154 | return 0; |
| 4155 | } |
| 4156 | |
| 4157 | /* ------------------------------------------------------------------------ */ |
| 4158 | /* |
| 4159 | * Throw an exception of the given class, with the constructor arguments |
| 4160 | * on the stack. |
| 4161 | */ |
| 4162 | void CVmRun::throw_new_class(VMG_ vm_obj_id_t cls, uint argc, |
| 4163 | const char *fallback_msg) |
| 4164 | { |
| 4165 | /* if the class is defined, create an instance and throw it */ |
| 4166 | if (cls != VM_INVALID_OBJ) |
| 4167 | { |
| 4168 | /* create the object */ |
| 4169 | vm_objp(vmg_ cls)->create_instance(vmg_ cls, 0, argc); |
| 4170 | |
| 4171 | /* make sure we created an object */ |
| 4172 | if (r0_.typ == VM_OBJ) |
| 4173 | { |
| 4174 | vm_obj_id_t exc_obj; |
| 4175 | |
| 4176 | /* get the object from R0 */ |
| 4177 | exc_obj = r0_.val.obj; |
| 4178 | |
| 4179 | /* |
| 4180 | * throw an 'unhandled exception' with this object as the |
| 4181 | * parameter; the execution loop will catch it and dispatch it |
| 4182 | * properly |
| 4183 | */ |
| 4184 | err_throw_a(VMERR_UNHANDLED_EXC, 1, |
| 4185 | ERR_TYPE_ULONG, (unsigned long)exc_obj); |
| 4186 | } |
| 4187 | else |
| 4188 | err_throw(VMERR_EXCEPTION_OBJ_REQD); |
| 4189 | } |
| 4190 | |
| 4191 | /* |
| 4192 | * the imported exception class isn't defined, or we failed to create |
| 4193 | * it; throw a generic intrinsic class exception with the fallback |
| 4194 | * message string |
| 4195 | */ |
| 4196 | err_throw_a(VMERR_INTCLS_GENERAL_ERROR, 1, ERR_TYPE_CHAR, fallback_msg); |
| 4197 | } |
| 4198 | |
| 4199 | /* |
| 4200 | * Throw a new RuntimeError subclass. |
| 4201 | */ |
| 4202 | void CVmRun::throw_new_rtesub(VMG_ vm_obj_id_t cls, uint argc, int errnum) |
| 4203 | { |
| 4204 | /* if the subclass is defined, create an instance and throw it */ |
| 4205 | if (cls != VM_INVALID_OBJ) |
| 4206 | { |
| 4207 | /* push the error number as the first argument */ |
| 4208 | push_int(vmg_ errnum); |
| 4209 | |
| 4210 | /* throw the subclass error */ |
| 4211 | throw_new_class(vmg_ cls, argc + 1, 0); |
| 4212 | } |
| 4213 | |
| 4214 | /* the subclass isn't defined, so throw a generic RuntimeError */ |
| 4215 | err_throw(errnum); |
| 4216 | } |
| 4217 | |
| 4218 | |
| 4219 | /* ------------------------------------------------------------------------ */ |
| 4220 | /* |
| 4221 | * Throw an exception. Returns true if an exception handler was found, |
| 4222 | * which means that execution can proceed; returns false if no handler |
| 4223 | * was found, in which case the execution loop must throw the exception |
| 4224 | * to its caller. |
| 4225 | */ |
| 4226 | const uchar *CVmRun::do_throw(VMG_ const uchar *pc, vm_obj_id_t exception_obj) |
| 4227 | { |
| 4228 | /* |
| 4229 | * Search the stack for a handler for this exception class. Start |
| 4230 | * at the current stack frame; if we find a handler here, use it; |
| 4231 | * otherwise, unwind the stack to the enclosing frame and search for |
| 4232 | * a handler there; repeat until we exhaust the stack. |
| 4233 | */ |
| 4234 | for (;;) |
| 4235 | { |
| 4236 | CVmExcTablePtr tab; |
| 4237 | const uchar *func_start; |
| 4238 | uint ofs; |
| 4239 | |
| 4240 | /* get a pointer to the start of the current function */ |
| 4241 | func_start = entry_ptr_native_; |
| 4242 | |
| 4243 | /* set up a pointer to the current exception table */ |
| 4244 | if (func_start != 0 && tab.set(func_start)) |
| 4245 | { |
| 4246 | size_t cnt; |
| 4247 | size_t i; |
| 4248 | CVmExcEntryPtr entry; |
| 4249 | |
| 4250 | /* calculate our offset in the current function */ |
| 4251 | ofs = pc - func_start; |
| 4252 | |
| 4253 | /* set up a pointer to the first table entry */ |
| 4254 | tab.set_entry_ptr(vmg_ &entry, 0); |
| 4255 | |
| 4256 | /* loop through the entries */ |
| 4257 | for (i = 0, cnt = tab.get_count() ; i < cnt ; |
| 4258 | ++i, entry.inc(vmg0_)) |
| 4259 | { |
| 4260 | /* |
| 4261 | * Check to see if we're in the range for this entry. |
| 4262 | * If this entry covers the appropriate range, and the |
| 4263 | * exception we're handling is of the class handled by |
| 4264 | * this exception (or derives from that class), this |
| 4265 | * handler handles this exception. |
| 4266 | */ |
| 4267 | if (ofs >= entry.get_start_ofs() |
| 4268 | && ofs <= entry.get_end_ofs() |
| 4269 | && (entry.get_exception() == VM_INVALID_OBJ |
| 4270 | || exception_obj == entry.get_exception() |
| 4271 | || (vm_objp(vmg_ exception_obj) |
| 4272 | ->is_instance_of(vmg_ entry.get_exception())))) |
| 4273 | { |
| 4274 | /* |
| 4275 | * this is it - move the program counter to the |
| 4276 | * first byte of the handler's code |
| 4277 | */ |
| 4278 | pc = func_start + entry.get_handler_ofs(); |
| 4279 | |
| 4280 | /* push the exception so that the handler can get at it */ |
| 4281 | push_obj(vmg_ exception_obj); |
| 4282 | |
| 4283 | /* return the new program counter at which to resume */ |
| 4284 | return pc; |
| 4285 | } |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | /* |
| 4290 | * We didn't find a handler in the current function - unwind the |
| 4291 | * stack one level, using an ordinary RETURN operation (we're not |
| 4292 | * really returning, though, so we don't need to provide a return |
| 4293 | * value). First, though, check to make sure there is an enclosing |
| 4294 | * frame at all - if there's not, we can simply return immediately. |
| 4295 | */ |
| 4296 | if (frame_ptr_ == 0) |
| 4297 | { |
| 4298 | /* there's no enclosing frame, so there's nowhere to go */ |
| 4299 | return 0; |
| 4300 | } |
| 4301 | |
| 4302 | /* try unwinding the stack a level */ |
| 4303 | if ((pc = do_return(vmg0_)) == 0) |
| 4304 | { |
| 4305 | /* |
| 4306 | * The enclosing frame is a recursive invocation, so we cannot |
| 4307 | * unwind any further at this point. Return null to indicate |
| 4308 | * that the exception was not handled and should be thrown out |
| 4309 | * of the current recursive VM invocation. |
| 4310 | */ |
| 4311 | return 0; |
| 4312 | } |
| 4313 | } |
| 4314 | } |
| 4315 | |
| 4316 | |
| 4317 | /* ------------------------------------------------------------------------ */ |
| 4318 | /* |
| 4319 | * Call a built-in function |
| 4320 | */ |
| 4321 | void CVmRun::call_bif(VMG_ uint set_index, uint func_index, uint argc) |
| 4322 | { |
| 4323 | /* |
| 4324 | * Call the function -- presume the compiler has ensured that the |
| 4325 | * function set index is valid for the load image, and that the |
| 4326 | * function index is valid for the function set; all of this can be |
| 4327 | * determined at compile time, since function sets are statically |
| 4328 | * defined. |
| 4329 | */ |
| 4330 | G_bif_table->call_func(vmg_ set_index, func_index, argc); |
| 4331 | } |
| 4332 | |
| 4333 | |
| 4334 | /* ------------------------------------------------------------------------ */ |
| 4335 | /* |
| 4336 | * Call a function pointer |
| 4337 | */ |
| 4338 | const uchar *CVmRun::call_func_ptr(VMG_ const vm_val_t *funcptr, uint argc, |
| 4339 | const vm_rcdesc *recurse_ctx, |
| 4340 | uint caller_ofs) |
| 4341 | { |
| 4342 | /* |
| 4343 | * Prepare the invocation frame. Even though we're calling this as a |
| 4344 | * function, if the value is an object, pass it as the 'self' value, |
| 4345 | * for compatibility with the old anonymous function invocation |
| 4346 | * protocol. |
| 4347 | */ |
| 4348 | vm_val_t *fp = G_stk->push(5); |
| 4349 | (fp++)->set_propid(VM_INVALID_PROP); |
| 4350 | (fp++)->set_nil(); |
| 4351 | (fp++)->set_nil(); |
| 4352 | if (funcptr->typ == VM_OBJ) |
| 4353 | (fp++)->set_obj(funcptr->val.obj); |
| 4354 | else |
| 4355 | (fp++)->set_nil_obj(); |
| 4356 | *(fp++) = *funcptr; |
| 4357 | |
| 4358 | /* call the function */ |
| 4359 | return call_func_ptr_fr(vmg_ funcptr, argc, recurse_ctx, caller_ofs); |
| 4360 | } |
| 4361 | |
| 4362 | /* |
| 4363 | * Call a function pointer, with the invocation frame already set up |
| 4364 | */ |
| 4365 | const uchar *CVmRun::call_func_ptr_fr(VMG_ const vm_val_t *funcptr, uint argc, |
| 4366 | const vm_rcdesc *recurse_ctx, |
| 4367 | uint caller_ofs) |
| 4368 | { |
| 4369 | vm_val_t invoker; |
| 4370 | CVmObject *io; |
| 4371 | |
| 4372 | /* check what we have */ |
| 4373 | switch (funcptr->typ) |
| 4374 | { |
| 4375 | case VM_OBJ: |
| 4376 | /* it's an object - check to see if it's invokable */ |
| 4377 | if ((io = vm_objp(vmg_ funcptr->val.obj))->get_invoker(vmg_ &invoker)) |
| 4378 | { |
| 4379 | /* get the invocation address, according to the invoker type */ |
| 4380 | const void *invoke_addr; |
| 4381 | switch (invoker.typ) |
| 4382 | { |
| 4383 | case VM_FUNCPTR: |
| 4384 | invoke_addr = G_code_pool->get_ptr(invoker.val.ofs); |
| 4385 | break; |
| 4386 | |
| 4387 | case VM_CODEPTR: |
| 4388 | invoke_addr = invoker.val.ptr; |
| 4389 | break; |
| 4390 | |
| 4391 | default: |
| 4392 | invoke_addr = 0; |
| 4393 | break; |
| 4394 | } |
| 4395 | |
| 4396 | /* if we got a valid address, invoke the code */ |
| 4397 | if (invoke_addr != 0) |
| 4398 | { |
| 4399 | /* call the function and return the new program counter */ |
| 4400 | return do_call( |
| 4401 | vmg_ caller_ofs, (const uchar *)invoke_addr, argc, |
| 4402 | recurse_ctx); |
| 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | /* it's not invokable - throw an error */ |
| 4407 | err_throw(VMERR_FUNCPTR_VAL_REQD); |
| 4408 | AFTER_ERR_THROW(return 0;) |
| 4409 | |
| 4410 | case VM_FUNCPTR: |
| 4411 | /* call the function */ |
| 4412 | return do_call(vmg_ caller_ofs, |
| 4413 | (const uchar *)G_code_pool->get_ptr(funcptr->val.ofs), |
| 4414 | argc, recurse_ctx); |
| 4415 | |
| 4416 | case VM_BIFPTR: |
| 4417 | /* |
| 4418 | * Built-in function. First, discard the invocation frame the |
| 4419 | * caller set up for a regular method/function call, as builtins |
| 4420 | * don't use those. Then, decode the SetIndex:FuncIndex value and |
| 4421 | * call the function. |
| 4422 | */ |
| 4423 | discard(5); |
| 4424 | call_bif(vmg_ funcptr->val.bifptr.set_idx, |
| 4425 | funcptr->val.bifptr.func_idx, argc); |
| 4426 | |
| 4427 | /* proceed from the caller's address */ |
| 4428 | return entry_ptr_native_ + caller_ofs; |
| 4429 | |
| 4430 | default: |
| 4431 | /* invalid type */ |
| 4432 | err_throw(VMERR_FUNCPTR_VAL_REQD); |
| 4433 | AFTER_ERR_THROW(return 0;) |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | /* ------------------------------------------------------------------------ */ |
| 4438 | /* |
| 4439 | * Call a function, non-recursively. |
| 4440 | * |
| 4441 | * This is a separate form of do_call(), but simplified for cases where we |
| 4442 | * know in advance that we won't need to check for recursion and when we |
| 4443 | * know in advance that we're calling a function and thus have no 'self' |
| 4444 | * or other method context objects. These simplifications reduce the |
| 4445 | * amount of work we have to do, so that ordinary function calls run a |
| 4446 | * little faster than they would if we used the full do_call() routine. |
| 4447 | */ |
| 4448 | const uchar *CVmRun::do_call_func_nr(VMG_ uint caller_ofs, |
| 4449 | pool_ofs_t target_ofs, uint argc) |
| 4450 | { |
| 4451 | const uchar *target_ofs_ptr; |
| 4452 | CVmFuncPtr hdr_ptr; |
| 4453 | uint i; |
| 4454 | vm_val_t *fp; |
| 4455 | int lcl_cnt; |
| 4456 | |
| 4457 | /* store nil in R0 */ |
| 4458 | r0_.set_nil(); |
| 4459 | |
| 4460 | /* translate the target address */ |
| 4461 | target_ofs_ptr = (const uchar *)G_code_pool->get_ptr(target_ofs); |
| 4462 | |
| 4463 | /* set up a pointer to the new function header */ |
| 4464 | hdr_ptr.set(target_ofs_ptr); |
| 4465 | |
| 4466 | /* get the number of locals from the header */ |
| 4467 | lcl_cnt = hdr_ptr.get_local_cnt(); |
| 4468 | |
| 4469 | /* get the target's stack space needs and check for stack overflow */ |
| 4470 | if (!check_space(hdr_ptr.get_stack_depth() + 11)) |
| 4471 | err_throw(VMERR_STACK_OVERFLOW); |
| 4472 | |
| 4473 | /* allocate the stack frame */ |
| 4474 | fp = push(11 + lcl_cnt); |
| 4475 | |
| 4476 | /* there's no target property, target object, defining object, or self */ |
| 4477 | (fp++)->set_propid(VM_INVALID_PROP); |
| 4478 | (fp++)->set_nil(); |
| 4479 | (fp++)->set_nil(); |
| 4480 | (fp++)->set_nil_obj(); |
| 4481 | |
| 4482 | /* push the invokee */ |
| 4483 | (fp++)->set_fnptr(target_ofs); |
| 4484 | |
| 4485 | /* push the frame reference slot */ |
| 4486 | (fp++)->set_nil(); |
| 4487 | |
| 4488 | /* push the native code descriptor (none for a non-recursive call) */ |
| 4489 | (fp++)->set_codeptr(0); |
| 4490 | |
| 4491 | /* push the caller's code offset */ |
| 4492 | (fp++)->set_codeofs(caller_ofs); |
| 4493 | |
| 4494 | /* push the current entrypoint code offset */ |
| 4495 | (fp++)->set_codeptr(entry_ptr_native_); |
| 4496 | |
| 4497 | /* push the actual parameter count */ |
| 4498 | (fp++)->set_int((int32_t)argc); |
| 4499 | |
| 4500 | /* push the current frame pointer */ |
| 4501 | (fp++)->set_stack(frame_ptr_); |
| 4502 | |
| 4503 | /* verify the argument count */ |
| 4504 | if (!hdr_ptr.argc_ok(argc)) |
| 4505 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 4506 | |
| 4507 | /* set up the new stack frame */ |
| 4508 | frame_ptr_ = fp; |
| 4509 | |
| 4510 | /* load EP with the new code offset */ |
| 4511 | entry_ptr_native_ = target_ofs_ptr; |
| 4512 | |
| 4513 | /* push nil for each local */ |
| 4514 | for (i = lcl_cnt ; i != 0 ; --i) |
| 4515 | (fp++)->set_nil(); |
| 4516 | |
| 4517 | /* create and activate the new function's profiler frame */ |
| 4518 | VM_IF_PROFILER(if (profiling_) |
| 4519 | prof_enter(vmg_ target_ofs_ptr)); |
| 4520 | |
| 4521 | /* return the new program counter */ |
| 4522 | return target_ofs_ptr + get_funchdr_size(); |
| 4523 | } |
| 4524 | |
| 4525 | /* ------------------------------------------------------------------------ */ |
| 4526 | /* |
| 4527 | * Recursive call descriptor |
| 4528 | */ |
| 4529 | |
| 4530 | /* set up a descriptor for a system caller */ |
| 4531 | void vm_rcdesc::init_ret(VMG_ const char *name) |
| 4532 | { |
| 4533 | this->name = name; |
| 4534 | bifptr.set_nil(); |
| 4535 | self.set_nil(); |
| 4536 | method_idx = 0; |
| 4537 | argc = 0; |
| 4538 | argp = 0; |
| 4539 | caller_addr = G_interpreter->get_last_pc(); |
| 4540 | } |
| 4541 | |
| 4542 | /* set up a descriptor for a built-in function */ |
| 4543 | void vm_rcdesc::init(VMG_ const char *name, |
| 4544 | const vm_bif_desc *funcset, int idx, |
| 4545 | vm_val_t *argp, int argc) |
| 4546 | { |
| 4547 | this->name = name; |
| 4548 | this->self.set_nil(); |
| 4549 | this->method_idx = 0; |
| 4550 | this->bifptr = funcset[idx].bifptr; |
| 4551 | this->argp = argp; |
| 4552 | this->argc = argc; |
| 4553 | this->caller_addr = G_interpreter->get_last_pc(); |
| 4554 | } |
| 4555 | |
| 4556 | /* initialize for an intrinsic class method */ |
| 4557 | void vm_rcdesc::init(VMG_ const char *name, |
| 4558 | vm_obj_id_t self, unsigned short method_idx, |
| 4559 | vm_val_t *argp, int argc) |
| 4560 | { |
| 4561 | this->name = name; |
| 4562 | this->bifptr.set_nil(); |
| 4563 | this->self.set_obj(self); |
| 4564 | this->method_idx = method_idx; |
| 4565 | this->argp = argp; |
| 4566 | this->argc = argc; |
| 4567 | this->caller_addr = G_interpreter->get_last_pc(); |
| 4568 | } |
| 4569 | |
| 4570 | /* initialize for an intrinsic class method */ |
| 4571 | void vm_rcdesc::init(VMG_ const char *name, |
| 4572 | const vm_val_t *self, unsigned short method_idx, |
| 4573 | vm_val_t *argp, int argc) |
| 4574 | { |
| 4575 | this->name = name; |
| 4576 | this->bifptr.set_nil(); |
| 4577 | this->self = *self; |
| 4578 | this->method_idx = method_idx; |
| 4579 | this->argp = argp; |
| 4580 | this->argc = argc; |
| 4581 | this->caller_addr = G_interpreter->get_last_pc(); |
| 4582 | } |
| 4583 | |
| 4584 | /* |
| 4585 | * Calculate the return address in the calling frame |
| 4586 | */ |
| 4587 | const uchar *vm_rcdesc::get_return_addr() const |
| 4588 | { |
| 4589 | /* if there's no caller address, there's no return address */ |
| 4590 | if (caller_addr == 0) |
| 4591 | return 0; |
| 4592 | |
| 4593 | /* |
| 4594 | * caller_addr is a pointer to the instruction that triggered the call |
| 4595 | * to the native code that in turn made the recursive call represented |
| 4596 | * by 'this'. The return address is the next instruction, so we just |
| 4597 | * need to calculate the size of the instruction at caller_addr and add |
| 4598 | * it to the caller address. Start with one byte for the opcode. |
| 4599 | */ |
| 4600 | const uchar *p = caller_addr; |
| 4601 | |
| 4602 | /* check for VARARGC, which can prefix many call instructions */ |
| 4603 | if (*p == OPC_VARARGC) |
| 4604 | ++p; |
| 4605 | |
| 4606 | /* advance past the actual call instruction */ |
| 4607 | p += CVmOpcodes::get_op_size(p); |
| 4608 | |
| 4609 | /* return the final address */ |
| 4610 | return p; |
| 4611 | } |
| 4612 | |
| 4613 | |
| 4614 | /* ------------------------------------------------------------------------ */ |
| 4615 | /* |
| 4616 | * Call a function or method |
| 4617 | */ |
| 4618 | const uchar *CVmRun::do_call(VMG_ uint caller_ofs, |
| 4619 | const uchar *target_ptr, uint argc, |
| 4620 | const vm_rcdesc *recurse_ctx) |
| 4621 | { |
| 4622 | CVmFuncPtr hdr_ptr; |
| 4623 | uint i; |
| 4624 | vm_val_t *fp; |
| 4625 | int lcl_cnt; |
| 4626 | |
| 4627 | /* store nil in R0 */ |
| 4628 | r0_.set_nil(); |
| 4629 | |
| 4630 | /* set up a pointer to the new function header */ |
| 4631 | hdr_ptr.set(target_ptr); |
| 4632 | |
| 4633 | /* get the number of locals from the header */ |
| 4634 | lcl_cnt = hdr_ptr.get_local_cnt(); |
| 4635 | |
| 4636 | /* |
| 4637 | * Get the space needs of the new function, and ensure we have enough |
| 4638 | * stack space available. Include the size of the frame that we store |
| 4639 | * (the original target object, the target property, the defining |
| 4640 | * object, the 'self' object, the caller's code offset, the caller's |
| 4641 | * entrypoint offset, the actual parameter count, and the enclosing |
| 4642 | * frame pointer) in our space needs. |
| 4643 | */ |
| 4644 | if (!check_space(hdr_ptr.get_stack_depth() + 11)) |
| 4645 | err_throw(VMERR_STACK_OVERFLOW); |
| 4646 | |
| 4647 | /* |
| 4648 | * Allocate the stack frame. Note that the caller has already pushed |
| 4649 | * targetprop, targetobj, definingobj, and self. |
| 4650 | */ |
| 4651 | fp = push(6 + lcl_cnt); |
| 4652 | |
| 4653 | /* push the frame reference slot */ |
| 4654 | (fp++)->set_nil(); |
| 4655 | |
| 4656 | /* push the native code descriptor */ |
| 4657 | (fp++)->set_codeptr(recurse_ctx); |
| 4658 | |
| 4659 | /* |
| 4660 | * Push the caller's code offset. Note that if the caller's offset is |
| 4661 | * zero, it indicates that the caller is not the byte-code interpreter |
| 4662 | * and that this is a recursive invocation; we represent recursive |
| 4663 | * frames using a zero caller offset, so we can just use the zero |
| 4664 | * value as given in this case. |
| 4665 | */ |
| 4666 | (fp++)->set_codeofs(caller_ofs); |
| 4667 | |
| 4668 | /* push the current entrypoint code offset */ |
| 4669 | (fp++)->set_codeptr(entry_ptr_native_); |
| 4670 | |
| 4671 | /* push the actual parameter count */ |
| 4672 | (fp++)->set_int((int32_t)argc); |
| 4673 | |
| 4674 | /* push the current frame pointer */ |
| 4675 | (fp++)->set_stack(frame_ptr_); |
| 4676 | |
| 4677 | /* |
| 4678 | * check the argument count - do this before establishing the new |
| 4679 | * frame and entry pointers, so that if we report a stack traceback in |
| 4680 | * the debugger, we'll report the error in the calling frame, which is |
| 4681 | * where it really belongs |
| 4682 | */ |
| 4683 | if (!hdr_ptr.argc_ok(argc)) |
| 4684 | { |
| 4685 | /* |
| 4686 | * if we're making a recursive call, throw an error indicating |
| 4687 | * what kind of recursive call we're making |
| 4688 | */ |
| 4689 | if (recurse_ctx != 0) |
| 4690 | { |
| 4691 | /* throw the named generic argument mismatch error */ |
| 4692 | err_throw_a(VMERR_WRONG_NUM_OF_ARGS_CALLING, 1, |
| 4693 | ERR_TYPE_CHAR, recurse_ctx->name); |
| 4694 | } |
| 4695 | else |
| 4696 | { |
| 4697 | /* throw the generic argument mismatch error */ |
| 4698 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 4699 | } |
| 4700 | } |
| 4701 | |
| 4702 | /* |
| 4703 | * set up the new frame so that the frame pointer points to the old |
| 4704 | * frame pointer stored in the stack |
| 4705 | */ |
| 4706 | frame_ptr_ = fp; |
| 4707 | |
| 4708 | /* load EP with the new code offset */ |
| 4709 | entry_ptr_native_ = target_ptr; |
| 4710 | |
| 4711 | /* push nil for each local */ |
| 4712 | for (i = lcl_cnt ; i != 0 ; --i) |
| 4713 | (fp++)->set_nil(); |
| 4714 | |
| 4715 | /* create and activate the new function's profiler frame */ |
| 4716 | VM_IF_PROFILER(if (profiling_) |
| 4717 | prof_enter(vmg_ target_ptr)); |
| 4718 | |
| 4719 | /* if desired, make a recursive call into the byte code interpreter */ |
| 4720 | if (caller_ofs != 0) |
| 4721 | { |
| 4722 | /* |
| 4723 | * return the new program counter at the first byte of code in the |
| 4724 | * new function, which immediately follows the header |
| 4725 | */ |
| 4726 | return target_ptr + get_funchdr_size(); |
| 4727 | } |
| 4728 | else |
| 4729 | { |
| 4730 | /* recursively call the interpreter loop */ |
| 4731 | run(vmg_ target_ptr + get_funchdr_size()); |
| 4732 | |
| 4733 | /* |
| 4734 | * this was a recursive call, so there's no program counter to |
| 4735 | * return - just return null |
| 4736 | */ |
| 4737 | return 0; |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | /* |
| 4742 | * Return from the current function. Returns true if execution can |
| 4743 | * proceed, false if this returns us out of the outermost function, in |
| 4744 | * which case the execution loop must terminate and return control to |
| 4745 | * the host environment. |
| 4746 | */ |
| 4747 | const uchar *CVmRun::do_return(VMG0_) |
| 4748 | { |
| 4749 | /* invalidate the frame reference object, if present */ |
| 4750 | vm_val_t *fro = get_frameref_slot(vmg_ frame_ptr_); |
| 4751 | if (fro->typ == VM_OBJ |
| 4752 | && CVmObjFrameRef::is_frameref_obj(vmg_ fro->val.obj)) |
| 4753 | ((CVmObjFrameRef *)vm_objp(vmg_ fro->val.obj))->inval_frame(vmg0_); |
| 4754 | |
| 4755 | /* |
| 4756 | * The frame pointer always points to the location on the stack |
| 4757 | * where we pushed the enclosing frame pointer. Reset the stack |
| 4758 | * pointer to the current frame pointer, then pop the enclosing |
| 4759 | * frame pointer. |
| 4760 | */ |
| 4761 | set_sp(frame_ptr_); |
| 4762 | frame_ptr_ = (vm_val_t *)get(0)->val.ptr; |
| 4763 | |
| 4764 | /* get the enclosing argument count */ |
| 4765 | int argc = get(1)->val.intval; |
| 4766 | |
| 4767 | /* restore the enclosing entry pointer */ |
| 4768 | entry_ptr_native_ = (const uchar *)get(2)->val.ptr; |
| 4769 | |
| 4770 | /* restore the enclosing code offset */ |
| 4771 | pool_ofs_t caller_ofs = get(3)->val.ofs; |
| 4772 | |
| 4773 | /* |
| 4774 | * Discard the actual parameters, plus the 'self', defining object, |
| 4775 | * original target object, and target property values. While we're at |
| 4776 | * it, also discard the enclosing frame pointer, enclosing argument |
| 4777 | * count, enclosing entry pointer, and enclosing code offset, which |
| 4778 | * we've already restored. |
| 4779 | */ |
| 4780 | discard(argc + 11); |
| 4781 | |
| 4782 | /* leave the profiler stack level */ |
| 4783 | VM_IF_PROFILER(if (profiling_) |
| 4784 | prof_leave()); |
| 4785 | |
| 4786 | /* if we're debugging, notify the debugger of the return */ |
| 4787 | VM_IF_DEBUGGER(G_debugger->step_return(vmg0_)); |
| 4788 | |
| 4789 | /* |
| 4790 | * if this is a special return address code, execute the appropraite |
| 4791 | * local subroutine |
| 4792 | */ |
| 4793 | if (vmrun_is_special_return(caller_ofs)) |
| 4794 | { |
| 4795 | vm_val_t v; |
| 4796 | |
| 4797 | switch (caller_ofs) |
| 4798 | { |
| 4799 | case VMRUN_RET_RECURSIVE: |
| 4800 | /* |
| 4801 | * return from recursive VM invocation - just return 0 as the |
| 4802 | * finishing offset to tell the caller to exit the bytecode |
| 4803 | * execution loop |
| 4804 | */ |
| 4805 | return 0; |
| 4806 | |
| 4807 | case VMRUN_RET_OP: |
| 4808 | /* |
| 4809 | * returning from operator evaluation - push R0 as the operand |
| 4810 | * result, and return to the address currently at TOS |
| 4811 | */ |
| 4812 | caller_ofs = get(0)->val.intval; |
| 4813 | *get(0) = r0_; |
| 4814 | break; |
| 4815 | |
| 4816 | case VMRUN_RET_OP_ASILCL: |
| 4817 | /* |
| 4818 | * Returning from an operator evaluation, with assignment to a |
| 4819 | * local. The local variable number is at TOS; assign it, then |
| 4820 | * return to the address next on the stack. |
| 4821 | */ |
| 4822 | pop(&v); |
| 4823 | *get_local(vmg_ v.val.intval) = r0_; |
| 4824 | pop(&v); |
| 4825 | caller_ofs = v.val.intval; |
| 4826 | break; |
| 4827 | |
| 4828 | default: |
| 4829 | /* |
| 4830 | * invalid address; we're going to dump into random code if we |
| 4831 | * continue, so nip it in the bud and throw an invalid op now |
| 4832 | */ |
| 4833 | err_throw(VMERR_INVALID_OPCODE); |
| 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | /* |
| 4838 | * Empirically, about half the time, the caller will push the return |
| 4839 | * value onto the stack. It's a little faster to test for that case |
| 4840 | * here and handle it specially. |
| 4841 | */ |
| 4842 | if (*(entry_ptr_native_ + caller_ofs) == OPC_GETR0) |
| 4843 | { |
| 4844 | push(&r0_); |
| 4845 | caller_ofs += 1; |
| 4846 | } |
| 4847 | |
| 4848 | /* |
| 4849 | * return the new program counter - calculate the PC offset by adding |
| 4850 | * the offset within the method to the entry pointer |
| 4851 | */ |
| 4852 | return entry_ptr_native_ + caller_ofs; |
| 4853 | } |
| 4854 | |
| 4855 | |
| 4856 | /* ------------------------------------------------------------------------ */ |
| 4857 | /* |
| 4858 | * Execution context save/restore - these are for the debugger's use, so |
| 4859 | * that it can save the machine registers while executing a debugger |
| 4860 | * expression. |
| 4861 | */ |
| 4862 | |
| 4863 | #ifdef VM_DEBUGGER |
| 4864 | |
| 4865 | /* |
| 4866 | * save the execution context |
| 4867 | */ |
| 4868 | void CVmRun::save_context(VMG_ vmrun_save_ctx *ctx) |
| 4869 | { |
| 4870 | /* save our registers */ |
| 4871 | ctx->entry_ptr_native_ = entry_ptr_native_; |
| 4872 | ctx->frame_ptr_ = frame_ptr_; |
| 4873 | ctx->pc_ptr_ = pc_ptr_; |
| 4874 | |
| 4875 | /* save the stack depth */ |
| 4876 | ctx->old_stack_depth_ = get_depth(); |
| 4877 | } |
| 4878 | |
| 4879 | /* |
| 4880 | * restore the execution context |
| 4881 | */ |
| 4882 | void CVmRun::restore_context(VMG_ vmrun_save_ctx *ctx) |
| 4883 | { |
| 4884 | /* restore our registers */ |
| 4885 | entry_ptr_native_ = ctx->entry_ptr_native_; |
| 4886 | frame_ptr_ = ctx->frame_ptr_; |
| 4887 | pc_ptr_ = ctx->pc_ptr_; |
| 4888 | |
| 4889 | /* if there's anything extra left on the stack, discard it */ |
| 4890 | if (get_depth() > ctx->old_stack_depth_) |
| 4891 | discard(get_depth() - ctx->old_stack_depth_); |
| 4892 | } |
| 4893 | |
| 4894 | #endif /* VM_DEBUGGER */ |
| 4895 | |
| 4896 | /* ------------------------------------------------------------------------ */ |
| 4897 | /* |
| 4898 | * Append a stack trace to a string. This is only meaningful in a |
| 4899 | * debugger-equipped version. |
| 4900 | */ |
| 4901 | #if VM_DEBUGGER |
| 4902 | |
| 4903 | /* |
| 4904 | * callback context for stack trace appender |
| 4905 | */ |
| 4906 | struct append_stack_ctx |
| 4907 | { |
| 4908 | /* the string so far */ |
| 4909 | vm_obj_id_t str_obj; |
| 4910 | |
| 4911 | /* globals */ |
| 4912 | vm_globals *vmg; |
| 4913 | |
| 4914 | /* frame pointer where we pushed our string for gc protection */ |
| 4915 | vm_val_t *gc_fp; |
| 4916 | }; |
| 4917 | |
| 4918 | /* |
| 4919 | * stack trace callback |
| 4920 | */ |
| 4921 | static void append_stack_cb(void *ctx0, const char *str, int strl) |
| 4922 | { |
| 4923 | append_stack_ctx *ctx = (append_stack_ctx *)ctx0; |
| 4924 | size_t new_len; |
| 4925 | size_t old_len; |
| 4926 | const char *old_str; |
| 4927 | char *new_str; |
| 4928 | |
| 4929 | /* set up access to globals */ |
| 4930 | VMGLOB_PTR(ctx->vmg); |
| 4931 | |
| 4932 | /* get the original string text */ |
| 4933 | old_str = vm_objp(vmg_ ctx->str_obj)->get_as_string(vmg0_); |
| 4934 | old_len = vmb_get_len(old_str); |
| 4935 | old_str += VMB_LEN; |
| 4936 | |
| 4937 | /* |
| 4938 | * allocate a new string, big enough for the old string plus the new |
| 4939 | * text, plus a newline |
| 4940 | */ |
| 4941 | new_len = old_len + strl + 1; |
| 4942 | ctx->str_obj = CVmObjString::create(vmg_ FALSE, new_len); |
| 4943 | |
| 4944 | /* get the new string buffer */ |
| 4945 | new_str = ((CVmObjString *)vm_objp(vmg_ ctx->str_obj))->cons_get_buf(); |
| 4946 | |
| 4947 | /* build the new string */ |
| 4948 | memcpy(new_str, old_str, old_len); |
| 4949 | new_str[old_len] = '\n'; |
| 4950 | memcpy(new_str + old_len + 1, str, strl); |
| 4951 | |
| 4952 | /* |
| 4953 | * replace our gc-protective stack reference to the old string with |
| 4954 | * the new string - we're done with the old string now, so it's okay |
| 4955 | * if it gets collected, but we obviously want to keep the new one |
| 4956 | * around |
| 4957 | */ |
| 4958 | G_interpreter->get_from_frame(ctx->gc_fp, 0)->set_obj(ctx->str_obj); |
| 4959 | } |
| 4960 | |
| 4961 | /* |
| 4962 | * append a stack trace to the given string |
| 4963 | */ |
| 4964 | vm_obj_id_t CVmRun::append_stack_trace(VMG_ vm_obj_id_t str_obj) |
| 4965 | { |
| 4966 | append_stack_ctx ctx; |
| 4967 | |
| 4968 | /* push the string for protection from gc */ |
| 4969 | push_obj(vmg_ str_obj); |
| 4970 | |
| 4971 | /* call the debugger to set up the stack traceback */ |
| 4972 | ctx.str_obj = str_obj; |
| 4973 | ctx.vmg = VMGLOB_ADDR; |
| 4974 | ctx.gc_fp = get_sp(); |
| 4975 | G_debugger->build_stack_listing(vmg_ &append_stack_cb, &ctx, TRUE); |
| 4976 | |
| 4977 | /* discard the gc protection */ |
| 4978 | discard(); |
| 4979 | |
| 4980 | /* return the result string */ |
| 4981 | return ctx.str_obj; |
| 4982 | } |
| 4983 | |
| 4984 | #endif /* VM_DEBUGGER */ |
| 4985 | |
| 4986 | /* ------------------------------------------------------------------------ */ |
| 4987 | /* |
| 4988 | * Set a property of an object |
| 4989 | */ |
| 4990 | void CVmRun::set_prop(VMG_ vm_obj_id_t obj, vm_prop_id_t prop, |
| 4991 | const vm_val_t *new_val) |
| 4992 | { |
| 4993 | /* set the property */ |
| 4994 | vm_objp(vmg_ obj)->set_prop(vmg_ G_undo, obj, prop, new_val); |
| 4995 | } |
| 4996 | |
| 4997 | /* ------------------------------------------------------------------------ */ |
| 4998 | /* |
| 4999 | * Evaluate an operator overload. 'obj' is the operand of a unary op, or |
| 5000 | * the left operand of a binary op; 'prop' is the operator overload |
| 5001 | * property ID; 'argc' is the number of additional operands (0 for a unary |
| 5002 | * op, 1 for a binary op); 'err' is the VMERR_xxx error number for applying |
| 5003 | * the operator to a value that doesn't implement it. |
| 5004 | * |
| 5005 | * If the object doesn't provide the property, we'll throw the error, since |
| 5006 | * we're attempting to apply an operator to a value that doesn't implement |
| 5007 | * it. This is in contrast to ordinary property evaluations, which just |
| 5008 | * return nil for an unimplemented property. |
| 5009 | * |
| 5010 | * The 'prop' usually comes from G_predef, since it's an imported name from |
| 5011 | * the image file. If this is undefined (VM_INVALID_PROP), we certainly |
| 5012 | * can't have an overload for the operator, so we'll simply throw the |
| 5013 | * error. |
| 5014 | * |
| 5015 | * The 'obj' value doesn't have to be an object, since the program could |
| 5016 | * attempt to apply the operator to any type. If the value is of a type |
| 5017 | * that doesn't support property evaluations, we'll throw the same error. |
| 5018 | * |
| 5019 | * If 'asi_lcl' is 0 or greater, it's the local variable number to assign |
| 5020 | * the value to on return from the operator overload. If this is negative, |
| 5021 | * the return value is to be pushed onto the stack on return. |
| 5022 | */ |
| 5023 | const uchar *CVmRun::op_overload(VMG_ uint caller_ofs, int asi_lcl, |
| 5024 | const vm_val_t *obj, vm_prop_id_t prop, |
| 5025 | uint argc, int err) |
| 5026 | { |
| 5027 | vm_val_t val; |
| 5028 | vm_obj_id_t srcobj; |
| 5029 | vm_val_t new_self; |
| 5030 | |
| 5031 | /* |
| 5032 | * if the program defines any overloads for the operator, it must |
| 5033 | * define the export property; so if the export property isn't defined, |
| 5034 | * we can't have any overloading for the operator |
| 5035 | */ |
| 5036 | if (prop == VM_INVALID_PROP) |
| 5037 | err_throw(err); |
| 5038 | |
| 5039 | /* |
| 5040 | * if the value isn't an object, list, or string, there's no property, |
| 5041 | * so throw the specified error |
| 5042 | */ |
| 5043 | switch (obj->typ) |
| 5044 | { |
| 5045 | case VM_OBJ: |
| 5046 | case VM_SSTRING: |
| 5047 | case VM_LIST: |
| 5048 | /* these can have property values - look it up */ |
| 5049 | if (get_prop_no_eval(vmg_ &obj, prop, &argc, &srcobj, &val, |
| 5050 | &obj, &new_self)) |
| 5051 | { |
| 5052 | /* presume this is not actually a procedure call */ |
| 5053 | int is_call = FALSE; |
| 5054 | |
| 5055 | /* |
| 5056 | * Got it. Set up the appropriate special local subroutine for |
| 5057 | * processing the return value. |
| 5058 | * |
| 5059 | * - For a recursive call, the caller will provide the special |
| 5060 | * handling on return, so there's nothing special to do. |
| 5061 | * |
| 5062 | * - If there's no local to assign, we simply push the value |
| 5063 | * onto the stack on return, then do a local return to the |
| 5064 | * actual caller. Push our return offset as the local return |
| 5065 | * address. |
| 5066 | * |
| 5067 | * - If there's a local, we assign the value to the local on |
| 5068 | * return. Push the return offset as the local return address, |
| 5069 | * and push the local number after that. |
| 5070 | */ |
| 5071 | if (val.typ == VM_CODEOFS |
| 5072 | || val.typ == VM_DSTRING |
| 5073 | || val.typ == VM_OBJX) |
| 5074 | { |
| 5075 | /* |
| 5076 | * These will all result in subroutine calls. Set up the |
| 5077 | * return frame patch accordingly. |
| 5078 | */ |
| 5079 | is_call = TRUE; |
| 5080 | if (caller_ofs == 0) |
| 5081 | { |
| 5082 | /* |
| 5083 | * this is a recursive call - no return processing |
| 5084 | * patch is required |
| 5085 | */ |
| 5086 | } |
| 5087 | else if (asi_lcl < 0) |
| 5088 | { |
| 5089 | /* open one stack slot above the arguments */ |
| 5090 | insert(argc, 1); |
| 5091 | |
| 5092 | /* on return, push the value onto the stack */ |
| 5093 | get(argc)->set_int(caller_ofs); |
| 5094 | caller_ofs = VMRUN_RET_OP; |
| 5095 | } |
| 5096 | else |
| 5097 | { |
| 5098 | /* open two stack slots above the arguments */ |
| 5099 | insert(argc, 2); |
| 5100 | |
| 5101 | /* on return, store the value in locl #asi_lcl */ |
| 5102 | get(argc + 1)->set_int(caller_ofs); |
| 5103 | get(argc)->set_int(asi_lcl); |
| 5104 | caller_ofs = VMRUN_RET_OP_ASILCL; |
| 5105 | } |
| 5106 | } |
| 5107 | |
| 5108 | /* evaluate the property */ |
| 5109 | const uchar *ret = eval_prop_val( |
| 5110 | vmg_ TRUE, caller_ofs, &val, obj->val.obj, prop, |
| 5111 | obj, srcobj, argc, 0); |
| 5112 | |
| 5113 | /* if this is a direct evaluation, do the return patch now */ |
| 5114 | if (!is_call) |
| 5115 | { |
| 5116 | if (caller_ofs == 0) |
| 5117 | { |
| 5118 | /* recursive call - the caller will take care of it */ |
| 5119 | } |
| 5120 | else if (asi_lcl < 0) |
| 5121 | { |
| 5122 | /* normal operator eval - push the result onto the stack */ |
| 5123 | push(get_r0()); |
| 5124 | } |
| 5125 | else |
| 5126 | { |
| 5127 | /* local assignment on return */ |
| 5128 | *get_local(vmg_ asi_lcl) = *get_r0(); |
| 5129 | } |
| 5130 | } |
| 5131 | |
| 5132 | /* return the result */ |
| 5133 | return ret; |
| 5134 | } |
| 5135 | |
| 5136 | /* the property isn't defined - throw the caller's error */ |
| 5137 | err_throw(err); |
| 5138 | AFTER_ERR_THROW(return 0;) |
| 5139 | |
| 5140 | default: |
| 5141 | /* other types don't have properties - throw the caller's error */ |
| 5142 | err_throw(err); |
| 5143 | AFTER_ERR_THROW(return 0;) |
| 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | /* ------------------------------------------------------------------------ */ |
| 5148 | /* |
| 5149 | * Evaluate a property of an object |
| 5150 | */ |
| 5151 | const uchar *CVmRun::get_prop(VMG_ uint caller_ofs, |
| 5152 | const vm_val_t *target_obj, |
| 5153 | vm_prop_id_t target_prop, |
| 5154 | const vm_val_t *self, uint argc, |
| 5155 | const vm_rcdesc *rc) |
| 5156 | { |
| 5157 | vm_val_t val; |
| 5158 | vm_obj_id_t srcobj; |
| 5159 | int found; |
| 5160 | vm_val_t new_self; |
| 5161 | |
| 5162 | /* find the property without evaluating it */ |
| 5163 | found = get_prop_no_eval(vmg_ &target_obj, target_prop, |
| 5164 | &argc, &srcobj, &val, &self, &new_self); |
| 5165 | |
| 5166 | /* if we didn't find it, try propNotDefined */ |
| 5167 | if (!found && G_predef->prop_not_defined_prop != VM_INVALID_PROP) |
| 5168 | { |
| 5169 | /* look up propNotDefined */ |
| 5170 | found = get_prop_no_eval(vmg_ &target_obj, |
| 5171 | G_predef->prop_not_defined_prop, |
| 5172 | &argc, &srcobj, &val, &self, &new_self); |
| 5173 | |
| 5174 | /* if we found a method, set up to call it */ |
| 5175 | if (found && (val.typ == VM_CODEOFS |
| 5176 | || val.typ == VM_OBJX |
| 5177 | || val.typ == VM_BIFPTRX)) |
| 5178 | { |
| 5179 | /* |
| 5180 | * add the target property as the additional first argument to |
| 5181 | * propNotDefined (we push backwards, so this will conveniently |
| 5182 | * become the new first argument) |
| 5183 | */ |
| 5184 | push_prop(vmg_ target_prop); |
| 5185 | |
| 5186 | /* count the additional argument */ |
| 5187 | ++argc; |
| 5188 | |
| 5189 | /* the property we're actually calling now is propNotDefined */ |
| 5190 | target_prop = G_predef->prop_not_defined_prop; |
| 5191 | } |
| 5192 | } |
| 5193 | |
| 5194 | /* evaluate whatever we found or didn't find */ |
| 5195 | return eval_prop_val(vmg_ found, caller_ofs, &val, self->val.obj, |
| 5196 | target_prop, target_obj, srcobj, argc, rc); |
| 5197 | } |
| 5198 | |
| 5199 | /* |
| 5200 | * Simplified property evaluator |
| 5201 | */ |
| 5202 | void CVmRun::get_prop(VMG_ vm_val_t *result, |
| 5203 | const vm_val_t *obj, vm_prop_id_t prop, uint argc, |
| 5204 | const vm_rcdesc *rc) |
| 5205 | { |
| 5206 | /* use nil as the default in case we don't find a value */ |
| 5207 | result->set_nil(); |
| 5208 | |
| 5209 | /* if the property is invalid, we definitely can't evaluate it */ |
| 5210 | if (prop == VM_INVALID_PROP) |
| 5211 | return; |
| 5212 | |
| 5213 | /* evaluate the property into R0 */ |
| 5214 | get_prop(vmg_ 0, obj, prop, obj, argc, rc); |
| 5215 | |
| 5216 | /* copy the result to the caller's parameter */ |
| 5217 | *result = r0_; |
| 5218 | } |
| 5219 | |
| 5220 | /* |
| 5221 | * Look up a property without evaluating it. |
| 5222 | */ |
| 5223 | inline int CVmRun::get_prop_no_eval(VMG_ const vm_val_t **target_obj, |
| 5224 | vm_prop_id_t target_prop, |
| 5225 | uint *argc, vm_obj_id_t *srcobj, |
| 5226 | vm_val_t *val, |
| 5227 | const vm_val_t **self, |
| 5228 | vm_val_t *new_self) |
| 5229 | { |
| 5230 | int found; |
| 5231 | const char *target_ptr; |
| 5232 | |
| 5233 | /* |
| 5234 | * we can evaluate properties of regular objects, as well as string |
| 5235 | * and list constants - see what we have |
| 5236 | */ |
| 5237 | switch((*target_obj)->typ) |
| 5238 | { |
| 5239 | case VM_LIST: |
| 5240 | /* 'self' must be the same as the target for a constant list */ |
| 5241 | if ((*self)->typ != (*target_obj)->typ |
| 5242 | || (*self)->val.ofs != (*target_obj)->val.ofs) |
| 5243 | err_throw(VMERR_OBJ_VAL_REQD); |
| 5244 | |
| 5245 | /* translate the list offset to a physical pointer */ |
| 5246 | target_ptr = G_const_pool->get_ptr((*target_obj)->val.ofs); |
| 5247 | |
| 5248 | /* evaluate the constant list property */ |
| 5249 | found = CVmObjList::const_get_prop(vmg_ val, *target_obj, |
| 5250 | target_ptr, target_prop, |
| 5251 | srcobj, argc); |
| 5252 | |
| 5253 | /* |
| 5254 | * If the result is a method to run, we need an actual object for |
| 5255 | * 'self'. In this case, create a dynamic list object with the |
| 5256 | * same contents as the constant list value. |
| 5257 | */ |
| 5258 | if (found |
| 5259 | && (val->typ == VM_CODEOFS |
| 5260 | || val->typ == VM_OBJX |
| 5261 | || val->typ == VM_BIFPTRX)) |
| 5262 | { |
| 5263 | /* create the list */ |
| 5264 | new_self->set_obj(CVmObjListConst::create(vmg_ target_ptr)); |
| 5265 | |
| 5266 | /* use it as the new 'self' and the new effective target */ |
| 5267 | *self = new_self; |
| 5268 | *target_obj = new_self; |
| 5269 | } |
| 5270 | |
| 5271 | /* go evaluate the result as normal */ |
| 5272 | break; |
| 5273 | |
| 5274 | case VM_SSTRING: |
| 5275 | /* 'self' must be the same as the target for a constant string */ |
| 5276 | if ((*self)->typ != (*target_obj)->typ |
| 5277 | || (*self)->val.ofs != (*target_obj)->val.ofs) |
| 5278 | err_throw(VMERR_OBJ_VAL_REQD); |
| 5279 | |
| 5280 | /* translate the string offset to a physical pointer */ |
| 5281 | target_ptr = G_const_pool->get_ptr((*target_obj)->val.ofs); |
| 5282 | |
| 5283 | /* evaluate the constant string property */ |
| 5284 | found = CVmObjString::const_get_prop(vmg_ val, *target_obj, |
| 5285 | target_ptr, target_prop, |
| 5286 | srcobj, argc); |
| 5287 | |
| 5288 | /* |
| 5289 | * If the result is a method to run, we need an actual object for |
| 5290 | * 'self'. In this case, create a dynamic string object with the |
| 5291 | * same contents as the constant string value. |
| 5292 | */ |
| 5293 | if (found |
| 5294 | && (val->typ == VM_CODEOFS |
| 5295 | || val->typ == VM_OBJX |
| 5296 | || val->typ == VM_BIFPTRX)) |
| 5297 | { |
| 5298 | /* create the string */ |
| 5299 | new_self->set_obj(CVmObjStringConst::create(vmg_ target_ptr)); |
| 5300 | |
| 5301 | /* it's the new 'self' and the new effective target object */ |
| 5302 | *self = new_self; |
| 5303 | *target_obj = new_self; |
| 5304 | } |
| 5305 | |
| 5306 | /* go evaluate the result as normal */ |
| 5307 | break; |
| 5308 | |
| 5309 | case VM_OBJ: |
| 5310 | /* get the property value from the target object */ |
| 5311 | found = vm_objp(vmg_ (*target_obj)->val.obj) |
| 5312 | ->get_prop(vmg_ target_prop, val, (*target_obj)->val.obj, |
| 5313 | srcobj, argc); |
| 5314 | |
| 5315 | /* 'self' must be an object as well */ |
| 5316 | if ((*self)->typ != VM_OBJ) |
| 5317 | err_throw(VMERR_OBJ_VAL_REQD); |
| 5318 | break; |
| 5319 | |
| 5320 | case VM_NIL: |
| 5321 | /* nil pointer dereferenced */ |
| 5322 | err_throw(VMERR_NIL_DEREF); |
| 5323 | |
| 5324 | default: |
| 5325 | /* we can't evaluate properties of anything else */ |
| 5326 | err_throw(VMERR_OBJ_VAL_REQD); |
| 5327 | } |
| 5328 | |
| 5329 | /* return the 'found' indication */ |
| 5330 | return found; |
| 5331 | } |
| 5332 | |
| 5333 | /* ------------------------------------------------------------------------ */ |
| 5334 | /* |
| 5335 | * Given a value that has been retrieved from an object property, |
| 5336 | * evaluate the value. If the value contains code, we'll execute the |
| 5337 | * code; if it contains a self-printing string, we'll display the |
| 5338 | * string; otherwise, we'll just store the value in R0. |
| 5339 | * |
| 5340 | * 'found' indicates whether or not the property value is defined. |
| 5341 | * False indicates that the property value is not defined by the object; |
| 5342 | * true indicates that it is. |
| 5343 | */ |
| 5344 | const uchar *CVmRun::eval_prop_val(VMG_ int found, uint caller_ofs, |
| 5345 | const vm_val_t *val, |
| 5346 | vm_obj_id_t self, |
| 5347 | vm_prop_id_t target_prop, |
| 5348 | const vm_val_t *orig_target_obj, |
| 5349 | vm_obj_id_t defining_obj, |
| 5350 | uint argc, const vm_rcdesc *rc) |
| 5351 | { |
| 5352 | /* check whether or not the property is defined */ |
| 5353 | if (found) |
| 5354 | { |
| 5355 | /* take appropriate action based on the datatype of the result */ |
| 5356 | switch(val->typ) |
| 5357 | { |
| 5358 | case VM_CODEOFS: |
| 5359 | /* |
| 5360 | * It's a method - invoke the method. This will set us up |
| 5361 | * to start executing this new code, so there's nothing more |
| 5362 | * we need to do here. |
| 5363 | */ |
| 5364 | |
| 5365 | /* push targetprop, targetobj, definingobj, self, and invokee */ |
| 5366 | { |
| 5367 | vm_val_t *fp = push(5); |
| 5368 | (fp++)->set_propid(target_prop); |
| 5369 | (fp++)->set_obj(orig_target_obj->val.obj); |
| 5370 | (fp++)->set_obj(defining_obj); |
| 5371 | (fp++)->set_obj(self); |
| 5372 | (fp++)->set_fnptr(val->val.ofs); |
| 5373 | } |
| 5374 | |
| 5375 | /* call the function */ |
| 5376 | return do_call(vmg_ caller_ofs, |
| 5377 | (const uchar *)G_code_pool->get_ptr(val->val.ofs), |
| 5378 | argc, rc); |
| 5379 | |
| 5380 | case VM_DSTRING: |
| 5381 | /* no arguments are allowed */ |
| 5382 | if (argc != 0) |
| 5383 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 5384 | |
| 5385 | /* |
| 5386 | * it's a self-printing string - invoke the default string |
| 5387 | * output function (this is effectively a do_call()) |
| 5388 | */ |
| 5389 | return disp_dstring(vmg_ val->val.ofs, caller_ofs, self); |
| 5390 | |
| 5391 | case VM_OBJX: |
| 5392 | /* |
| 5393 | * Execute-on-eval object. If the value is an anonymous |
| 5394 | * function or other invokable object, call it. If it's a |
| 5395 | * string, print it. |
| 5396 | */ |
| 5397 | if (vm_objp(vmg_ val->val.obj)->get_invoker(vmg_ 0)) |
| 5398 | { |
| 5399 | /* push targetprop, targetobj, definingobj, self, invokee */ |
| 5400 | vm_val_t *fp = push(5); |
| 5401 | (fp++)->set_propid(target_prop); |
| 5402 | (fp++)->set_obj(orig_target_obj->val.obj); |
| 5403 | (fp++)->set_obj(defining_obj); |
| 5404 | (fp++)->set_obj(self); |
| 5405 | (fp++)->set_obj(val->val.obj); |
| 5406 | |
| 5407 | /* convert to an ordinary anonymous function object */ |
| 5408 | vm_val_t funcptr; |
| 5409 | funcptr.set_obj(val->val.obj); |
| 5410 | |
| 5411 | /* invoke it */ |
| 5412 | return call_func_ptr_fr(vmg_ &funcptr, argc, 0, caller_ofs); |
| 5413 | } |
| 5414 | else if (CVmObjString::is_string_obj(vmg_ val->val.obj)) |
| 5415 | { |
| 5416 | /* print the string */ |
| 5417 | push_obj(vmg_ val->val.obj); |
| 5418 | return disp_string_val(vmg_ caller_ofs, self); |
| 5419 | } |
| 5420 | err_throw(VMERR_BAD_TYPE_CALL); |
| 5421 | |
| 5422 | case VM_BIFPTRX: |
| 5423 | /* Execute-on-eval built-in function. Call the function. */ |
| 5424 | call_bif(vmg_ val->val.bifptr.set_idx, |
| 5425 | val->val.bifptr.func_idx, argc); |
| 5426 | |
| 5427 | /* resume execution where we left off */ |
| 5428 | return entry_ptr_native_ + caller_ofs; |
| 5429 | |
| 5430 | default: |
| 5431 | /* for any other value, no arguments are allowed */ |
| 5432 | if (argc != 0) |
| 5433 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 5434 | |
| 5435 | /* store the result in R0 */ |
| 5436 | r0_ = *val; |
| 5437 | |
| 5438 | /* resume execution where we left off */ |
| 5439 | return entry_ptr_native_ + caller_ofs; |
| 5440 | } |
| 5441 | } |
| 5442 | else |
| 5443 | { |
| 5444 | /* |
| 5445 | * the property or method is not defined - discard arguments and |
| 5446 | * set R0 to nil |
| 5447 | */ |
| 5448 | discard(argc); |
| 5449 | r0_.set_nil(); |
| 5450 | |
| 5451 | /* resume execution where we left off */ |
| 5452 | return entry_ptr_native_ + caller_ofs; |
| 5453 | } |
| 5454 | } |
| 5455 | |
| 5456 | /* ------------------------------------------------------------------------ */ |
| 5457 | /* |
| 5458 | * Inherit a property or method from the appropriate superclass of the |
| 5459 | * object that defines currently executing code. |
| 5460 | */ |
| 5461 | const uchar *CVmRun::inh_prop(VMG_ uint caller_ofs, |
| 5462 | vm_prop_id_t prop, uint argc) |
| 5463 | { |
| 5464 | vm_val_t orig_target_obj; |
| 5465 | vm_obj_id_t defining_obj; |
| 5466 | vm_val_t val; |
| 5467 | vm_obj_id_t srcobj; |
| 5468 | int found; |
| 5469 | vm_obj_id_t self; |
| 5470 | |
| 5471 | /* get the defining object from the stack frame */ |
| 5472 | defining_obj = get_defining_obj(vmg0_); |
| 5473 | |
| 5474 | /* get the original target object from the stack frame */ |
| 5475 | orig_target_obj.set_obj(get_orig_target_obj(vmg0_)); |
| 5476 | |
| 5477 | /* get the 'self' object */ |
| 5478 | self = get_self(vmg0_); |
| 5479 | |
| 5480 | /* get the inherited property value */ |
| 5481 | found = vm_objp(vmg_ self)->inh_prop(vmg_ prop, &val, self, |
| 5482 | orig_target_obj.val.obj, |
| 5483 | defining_obj, &srcobj, &argc); |
| 5484 | |
| 5485 | /* if we didn't find it, try inheriting propNotDefined */ |
| 5486 | if (!found && G_predef->prop_not_defined_prop != VM_INVALID_PROP) |
| 5487 | { |
| 5488 | /* |
| 5489 | * Look up propNotDefined using the same search conditions we used |
| 5490 | * to find the original inherited property. This lets us look up |
| 5491 | * the "inherited" propNotDefined. |
| 5492 | */ |
| 5493 | found = vm_objp(vmg_ self)->inh_prop(vmg_ |
| 5494 | G_predef->prop_not_defined_prop, |
| 5495 | &val, self, |
| 5496 | orig_target_obj.val.obj, |
| 5497 | defining_obj, &srcobj, &argc); |
| 5498 | |
| 5499 | /* |
| 5500 | * if we found it, and it's code, push the original property ID we |
| 5501 | * were attempting to inherit - this becomes the new first |
| 5502 | * parameter to the propNotDefined method |
| 5503 | */ |
| 5504 | if (found |
| 5505 | && (val.typ == VM_CODEOFS |
| 5506 | || val.typ == VM_OBJX |
| 5507 | || val.typ == VM_BIFPTRX)) |
| 5508 | { |
| 5509 | /* add the original property pointer argument */ |
| 5510 | push_prop(vmg_ prop); |
| 5511 | |
| 5512 | /* count the additional argument */ |
| 5513 | ++argc; |
| 5514 | |
| 5515 | /* the target property changes to propNotDefined */ |
| 5516 | prop = G_predef->prop_not_defined_prop; |
| 5517 | } |
| 5518 | } |
| 5519 | |
| 5520 | /* |
| 5521 | * evaluate and store the result - note that "self" remains the |
| 5522 | * current "self" object, since we're inheriting within the context |
| 5523 | * of the original method call |
| 5524 | */ |
| 5525 | return eval_prop_val(vmg_ found, caller_ofs, &val, self, prop, |
| 5526 | &orig_target_obj, srcobj, argc, 0); |
| 5527 | } |
| 5528 | |
| 5529 | |
| 5530 | /* ------------------------------------------------------------------------ */ |
| 5531 | /* |
| 5532 | * Display a dstring via the default string display mechanism |
| 5533 | */ |
| 5534 | const uchar *CVmRun::disp_dstring(VMG_ pool_ofs_t ofs, uint caller_ofs, |
| 5535 | vm_obj_id_t self) |
| 5536 | { |
| 5537 | /* push the string */ |
| 5538 | push()->set_sstring(ofs); |
| 5539 | |
| 5540 | /* invoke the default "say" function */ |
| 5541 | return disp_string_val(vmg_ caller_ofs, self); |
| 5542 | } |
| 5543 | |
| 5544 | /* |
| 5545 | * Display the value at top of stack via the default string display |
| 5546 | * mechanism |
| 5547 | */ |
| 5548 | const uchar *CVmRun::disp_string_val(VMG_ uint caller_ofs, vm_obj_id_t self) |
| 5549 | { |
| 5550 | /* |
| 5551 | * if there's a valid 'self' object, and there's a default display |
| 5552 | * method defined, and 'self' defines or inherits that method, |
| 5553 | * invoke the method |
| 5554 | */ |
| 5555 | if (say_method_ != VM_INVALID_PROP && self != VM_INVALID_OBJ) |
| 5556 | { |
| 5557 | vm_obj_id_t src_obj; |
| 5558 | vm_val_t val; |
| 5559 | |
| 5560 | /* |
| 5561 | * look up the property - if we find it, and it's a regular |
| 5562 | * method, invoke it |
| 5563 | */ |
| 5564 | if (vm_objp(vmg_ self)->get_prop(vmg_ say_method_, &val, self, |
| 5565 | &src_obj, 0) |
| 5566 | && (val.typ == VM_CODEOFS || val.typ == VM_OBJX |
| 5567 | || val.typ == VM_BIFPTRX)) |
| 5568 | { |
| 5569 | /* set up a 'self' value - this is the target object */ |
| 5570 | vm_val_t self_val; |
| 5571 | self_val.set_obj(self); |
| 5572 | |
| 5573 | /* there's a default display method - invoke it */ |
| 5574 | return eval_prop_val(vmg_ TRUE, caller_ofs, &val, self, |
| 5575 | say_method_, &self_val, src_obj, 1, 0); |
| 5576 | } |
| 5577 | } |
| 5578 | |
| 5579 | /* if the "say" function isn't initialized, it's an error */ |
| 5580 | if (say_func_ == 0 || say_func_->val.typ == VM_NIL) |
| 5581 | err_throw(VMERR_SAY_IS_NOT_DEFINED); |
| 5582 | |
| 5583 | /* call the "say" function with the argument at top of stack */ |
| 5584 | return call_func_ptr(vmg_ &say_func_->val, 1, 0, caller_ofs); |
| 5585 | } |
| 5586 | |
| 5587 | /* |
| 5588 | * Set the "say" function. |
| 5589 | */ |
| 5590 | void CVmRun::set_say_func(VMG_ const vm_val_t *val) |
| 5591 | { |
| 5592 | /* |
| 5593 | * if we haven't yet allocated a global to hold the 'say' function, |
| 5594 | * allocate one now |
| 5595 | */ |
| 5596 | if (say_func_ == 0) |
| 5597 | say_func_ = G_obj_table->create_global_var(); |
| 5598 | |
| 5599 | /* remember the new function */ |
| 5600 | say_func_->val = *val; |
| 5601 | } |
| 5602 | |
| 5603 | /* |
| 5604 | * Get the current "say" function |
| 5605 | */ |
| 5606 | void CVmRun::get_say_func(vm_val_t *val) const |
| 5607 | { |
| 5608 | /* |
| 5609 | * if we ever allocated a global to hold the 'say' function, return its |
| 5610 | * value; otherwise, there's no 'say' function, so the result is nil |
| 5611 | */ |
| 5612 | if (say_func_ != 0) |
| 5613 | *val = say_func_->val; |
| 5614 | else |
| 5615 | val->set_nil(); |
| 5616 | } |
| 5617 | |
| 5618 | /* ------------------------------------------------------------------------ */ |
| 5619 | /* |
| 5620 | * Push a string value |
| 5621 | */ |
| 5622 | void CVmRun::push_string(VMG_ const char *str, size_t len) |
| 5623 | { |
| 5624 | if (str != 0) |
| 5625 | push()->set_obj(CVmObjString::create(vmg_ FALSE, str, len)); |
| 5626 | else |
| 5627 | push()->set_nil(); |
| 5628 | } |
| 5629 | |
| 5630 | /* |
| 5631 | * Push a printf-formatted string |
| 5632 | */ |
| 5633 | void CVmRun::push_stringf(VMG_ const char *fmt, ...) |
| 5634 | { |
| 5635 | /* package the arguments as a va_list and invoke our va_list version */ |
| 5636 | va_list args; |
| 5637 | va_start(args, fmt); |
| 5638 | push_stringvf(vmg_ fmt, args); |
| 5639 | va_end(args); |
| 5640 | } |
| 5641 | |
| 5642 | /* |
| 5643 | * Push a vprintf-formatted string |
| 5644 | */ |
| 5645 | void CVmRun::push_stringvf(VMG_ const char *fmt, va_list args) |
| 5646 | { |
| 5647 | /* allocate and format the string */ |
| 5648 | char *str = t3vsprintf_alloc(fmt, args); |
| 5649 | |
| 5650 | /* push it */ |
| 5651 | push_string(vmg_ str); |
| 5652 | |
| 5653 | /* free the allocated string buffer */ |
| 5654 | t3free(str); |
| 5655 | } |
| 5656 | |
| 5657 | /* ------------------------------------------------------------------------ */ |
| 5658 | /* |
| 5659 | * Check a property for speculative evaluation |
| 5660 | */ |
| 5661 | void CVmRun::check_prop_spec_eval(VMG_ vm_obj_id_t obj, vm_prop_id_t prop) |
| 5662 | { |
| 5663 | vm_val_t val; |
| 5664 | vm_obj_id_t srcobj; |
| 5665 | |
| 5666 | /* get the property value */ |
| 5667 | if (vm_objp(vmg_ obj)->get_prop(vmg_ prop, &val, obj, &srcobj, 0)) |
| 5668 | { |
| 5669 | /* check the type of the value */ |
| 5670 | switch(val.typ) |
| 5671 | { |
| 5672 | case VM_CODEOFS: |
| 5673 | case VM_OBJX: |
| 5674 | case VM_BIFPTRX: |
| 5675 | case VM_DSTRING: |
| 5676 | case VM_NATIVE_CODE: |
| 5677 | /* |
| 5678 | * evaulating these types could result in side effects, so |
| 5679 | * this property cannot be evaulated during a speculative |
| 5680 | * evaluation |
| 5681 | */ |
| 5682 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 5683 | break; |
| 5684 | |
| 5685 | default: |
| 5686 | /* evaluating other types causes no side effects, so proceed */ |
| 5687 | break; |
| 5688 | } |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | /* ------------------------------------------------------------------------ */ |
| 5693 | /* |
| 5694 | * Set up a function header pointer for the current function |
| 5695 | */ |
| 5696 | void CVmRun::set_current_func_ptr(VMG_ CVmFuncPtr *func_ptr) |
| 5697 | { |
| 5698 | /* set up the pointer based on the current Entry Pointer register */ |
| 5699 | func_ptr->set(entry_ptr_native_); |
| 5700 | } |
| 5701 | |
| 5702 | /* |
| 5703 | * Set up a function header pointer for the return address of the given |
| 5704 | * stack frame |
| 5705 | */ |
| 5706 | void CVmRun::set_return_funcptr_from_frame(VMG_ CVmFuncPtr *func_ptr, |
| 5707 | vm_val_t *frame_ptr) |
| 5708 | { |
| 5709 | /* set up the function pointer for the entry pointer */ |
| 5710 | func_ptr->set(get_enclosing_entry_ptr_from_frame(vmg_ frame_ptr)); |
| 5711 | } |
| 5712 | |
| 5713 | /* ------------------------------------------------------------------------ */ |
| 5714 | /* |
| 5715 | * Get the frame pointer at a given stack level |
| 5716 | */ |
| 5717 | vm_val_t *CVmRun::get_fp_at_level(VMG_ int level) VM_REG_CONST |
| 5718 | { |
| 5719 | vm_val_t *fp; |
| 5720 | const vm_rcdesc *rc; |
| 5721 | |
| 5722 | /* walk up the stack to the desired level */ |
| 5723 | for (fp = frame_ptr_, rc = 0 ; fp != 0 && level != 0 ; --level) |
| 5724 | { |
| 5725 | /* if this is a recursive level, count it */ |
| 5726 | if (rc != 0 && rc->has_return_addr()) |
| 5727 | { |
| 5728 | /* move to the bytecode caller at this level */ |
| 5729 | rc = 0; |
| 5730 | |
| 5731 | /* count the level */ |
| 5732 | if (--level == 0) |
| 5733 | break; |
| 5734 | } |
| 5735 | |
| 5736 | /* move to the enclosing level */ |
| 5737 | rc = get_rcdesc_from_frame(vmg_ fp); |
| 5738 | fp = get_enclosing_frame_ptr(vmg_ fp); |
| 5739 | } |
| 5740 | |
| 5741 | /* |
| 5742 | * if we ran out of frames before we reached the desired level, or we |
| 5743 | * stopped at a native caller, the requested frame doesn't exist |
| 5744 | */ |
| 5745 | if (fp == 0 || (rc != 0 && rc->has_return_addr())) |
| 5746 | err_throw(VMERR_BAD_FRAME); |
| 5747 | |
| 5748 | /* return the frame */ |
| 5749 | return fp; |
| 5750 | } |
| 5751 | |
| 5752 | /* ------------------------------------------------------------------------ */ |
| 5753 | /* |
| 5754 | * Get the message from an exception object |
| 5755 | */ |
| 5756 | void CVmRun::get_exc_message(VMG_ const CVmException *exc, |
| 5757 | char *buf, size_t buflen, int add_unh_prefix) |
| 5758 | { |
| 5759 | CVmException tmpexc; |
| 5760 | const char *tmpmsg; |
| 5761 | const char *msg; |
| 5762 | |
| 5763 | /* set up our temporary exception object with no parameters by default */ |
| 5764 | tmpexc.param_count_ = 0; |
| 5765 | |
| 5766 | /* check for unhandled program exceptions */ |
| 5767 | if (exc->get_error_code() == VMERR_UNHANDLED_EXC) |
| 5768 | { |
| 5769 | size_t msg_len; |
| 5770 | |
| 5771 | /* |
| 5772 | * This is not a VM error, but is simply an exception that the |
| 5773 | * program itself threw but did not handle. We might be able to |
| 5774 | * find an informational message in the exception object itself. |
| 5775 | */ |
| 5776 | |
| 5777 | /* get the exception's message, if available */ |
| 5778 | msg = get_exc_message(vmg_ exc, &msg_len); |
| 5779 | if (msg != 0) |
| 5780 | { |
| 5781 | /* |
| 5782 | * we got a message from the exception object - use it |
| 5783 | */ |
| 5784 | |
| 5785 | /* set up our parameters for the formatting */ |
| 5786 | tmpexc.param_count_ = 1; |
| 5787 | tmpexc.set_param_str(0, msg, msg_len); |
| 5788 | |
| 5789 | /* |
| 5790 | * If they want an "unhandled exception" prefix, get the |
| 5791 | * message for the prefix; otherwise, just use the message |
| 5792 | * from the exception without further adornment. |
| 5793 | */ |
| 5794 | if (add_unh_prefix) |
| 5795 | { |
| 5796 | /* they want a prefix - get the prefix message */ |
| 5797 | tmpmsg = err_get_msg(vm_messages, vm_message_count, |
| 5798 | VMERR_UNHANDLED_EXC_PARAM, FALSE); |
| 5799 | } |
| 5800 | else |
| 5801 | { |
| 5802 | /* no prefix desired - just use the message as we got it */ |
| 5803 | tmpmsg = "%s"; |
| 5804 | } |
| 5805 | |
| 5806 | /* format the message */ |
| 5807 | err_format_msg(buf, buflen, tmpmsg, &tmpexc); |
| 5808 | } |
| 5809 | else |
| 5810 | { |
| 5811 | /* no message - use a generic exception message */ |
| 5812 | tmpmsg = err_get_msg(vm_messages, vm_message_count, |
| 5813 | VMERR_UNHANDLED_EXC, FALSE); |
| 5814 | err_format_msg(buf, buflen, tmpmsg, &tmpexc); |
| 5815 | } |
| 5816 | } |
| 5817 | else |
| 5818 | { |
| 5819 | /* |
| 5820 | * It's a VM exception, so we can determine the error's meaning |
| 5821 | * from the error code. Look up the message for the error code |
| 5822 | * in our error message list. |
| 5823 | */ |
| 5824 | msg = err_get_msg(vm_messages, vm_message_count, |
| 5825 | exc->get_error_code(), FALSE); |
| 5826 | |
| 5827 | /* if that failed, just show the error number */ |
| 5828 | if (msg == 0) |
| 5829 | { |
| 5830 | /* no message - just show the error code */ |
| 5831 | tmpmsg = err_get_msg(vm_messages, vm_message_count, |
| 5832 | VMERR_VM_EXC_CODE, FALSE); |
| 5833 | |
| 5834 | /* set up our parameters for formatting */ |
| 5835 | tmpexc.param_count_ = 1; |
| 5836 | tmpexc.set_param_int(0, exc->get_error_code()); |
| 5837 | |
| 5838 | /* format the message */ |
| 5839 | err_format_msg(buf, buflen, tmpmsg, &tmpexc); |
| 5840 | } |
| 5841 | else |
| 5842 | { |
| 5843 | char tmpbuf[256]; |
| 5844 | |
| 5845 | /* format the message from the exception parameters */ |
| 5846 | err_format_msg(tmpbuf, sizeof(tmpbuf), msg, exc); |
| 5847 | |
| 5848 | /* get the prefix message */ |
| 5849 | tmpmsg = err_get_msg(vm_messages, vm_message_count, |
| 5850 | VMERR_VM_EXC_PARAM, FALSE); |
| 5851 | |
| 5852 | /* set up our parameters for the formatting */ |
| 5853 | tmpexc.param_count_ = 1; |
| 5854 | tmpexc.set_param_str(0, tmpbuf); |
| 5855 | |
| 5856 | /* format the message */ |
| 5857 | err_format_msg(buf, buflen, tmpmsg, &tmpexc); |
| 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | /* |
| 5863 | * Get the message from an "unhandled exception" error object |
| 5864 | */ |
| 5865 | const char *CVmRun::get_exc_message(VMG_ const CVmException *exc, |
| 5866 | size_t *msg_len) |
| 5867 | { |
| 5868 | vm_obj_id_t exc_obj; |
| 5869 | |
| 5870 | /* |
| 5871 | * if the error isn't "unhandled exception," there's not a stored |
| 5872 | * exception object; likewise, if there's no object parameter in the |
| 5873 | * exception, there's nothing to use to obtain the message |
| 5874 | */ |
| 5875 | if (exc->get_error_code() != VMERR_UNHANDLED_EXC |
| 5876 | || exc->get_param_count() < 1) |
| 5877 | return 0; |
| 5878 | |
| 5879 | /* get the exception object */ |
| 5880 | exc_obj = (vm_obj_id_t)exc->get_param_ulong(0); |
| 5881 | |
| 5882 | /* get the message from the object */ |
| 5883 | return get_exc_message(vmg_ exc_obj, msg_len); |
| 5884 | } |
| 5885 | |
| 5886 | /* |
| 5887 | * Get the message from an exception object |
| 5888 | */ |
| 5889 | const char *CVmRun::get_exc_message(VMG_ vm_obj_id_t exc_obj, size_t *msg_len) |
| 5890 | { |
| 5891 | vm_val_t val; |
| 5892 | vm_obj_id_t src_obj; |
| 5893 | const char *str; |
| 5894 | uint argc; |
| 5895 | |
| 5896 | /* if there's no object, there's no message */ |
| 5897 | if (exc_obj == VM_INVALID_OBJ) |
| 5898 | return 0; |
| 5899 | |
| 5900 | /* |
| 5901 | * get the exceptionMessage property value from the object; if |
| 5902 | * there's not a valid exceptionMessage property defined, or the |
| 5903 | * object doesn't have a value for the property, there's no message |
| 5904 | */ |
| 5905 | argc = 0; |
| 5906 | if (G_predef->rterrmsg_prop == VM_INVALID_PROP |
| 5907 | || (!vm_objp(vmg_ exc_obj)->get_prop(vmg_ G_predef->rterrmsg_prop, |
| 5908 | &val, exc_obj, &src_obj, |
| 5909 | &argc))) |
| 5910 | return 0; |
| 5911 | |
| 5912 | /* |
| 5913 | * We got the property. If it's a string or an object containing a |
| 5914 | * string, retrieve the string. |
| 5915 | */ |
| 5916 | switch(val.typ) |
| 5917 | { |
| 5918 | case VM_SSTRING: |
| 5919 | /* get the constant string */ |
| 5920 | str = G_const_pool->get_ptr(val.val.ofs); |
| 5921 | break; |
| 5922 | |
| 5923 | case VM_OBJ: |
| 5924 | /* get the string value of the object, if possible */ |
| 5925 | str = vm_objp(vmg_ val.val.obj)->get_as_string(vmg0_); |
| 5926 | break; |
| 5927 | |
| 5928 | default: |
| 5929 | /* it's not a string - we can't use it */ |
| 5930 | str = 0; |
| 5931 | break; |
| 5932 | } |
| 5933 | |
| 5934 | /* check to see if we got a string */ |
| 5935 | if (str != 0) |
| 5936 | { |
| 5937 | /* |
| 5938 | * The string is in the standard VM internal format, which means |
| 5939 | * it has a 2-byte length prefix followed by the bytes of the |
| 5940 | * string (with no null termination). Read the length prefix, |
| 5941 | * then skip past it so the caller doesn't have to. |
| 5942 | */ |
| 5943 | *msg_len = osrp2(str); |
| 5944 | str += VMB_LEN; |
| 5945 | } |
| 5946 | |
| 5947 | /* return the string pointer */ |
| 5948 | return str; |
| 5949 | } |
| 5950 | |
| 5951 | /* ------------------------------------------------------------------------ */ |
| 5952 | /* |
| 5953 | * Get the boundaries of the current statement, based on debugging |
| 5954 | * information. Returns true if valid debugging information was found for |
| 5955 | * the given code location, false if not. |
| 5956 | */ |
| 5957 | int CVmRun::get_stm_bounds(VMG_ const CVmFuncPtr *func_ptr, |
| 5958 | ulong method_ofs, |
| 5959 | CVmDbgLinePtr *caller_line_ptr, |
| 5960 | const uchar **stm_start, const uchar **stm_end) |
| 5961 | { |
| 5962 | CVmDbgTablePtr dbg_ptr; |
| 5963 | int lo; |
| 5964 | int hi; |
| 5965 | int cur; |
| 5966 | |
| 5967 | /* presume we won't find anything */ |
| 5968 | *stm_start = *stm_end = 0; |
| 5969 | |
| 5970 | /* |
| 5971 | * if the current method has no line records, we can't find the |
| 5972 | * boundaries |
| 5973 | */ |
| 5974 | if (!func_ptr->set_dbg_ptr(&dbg_ptr) |
| 5975 | || dbg_ptr.get_line_count(vmg0_) == 0) |
| 5976 | { |
| 5977 | /* indicate that we didn't find debug information */ |
| 5978 | return FALSE; |
| 5979 | } |
| 5980 | |
| 5981 | /* |
| 5982 | * We must perform a binary search of the line records for the line |
| 5983 | * that contains this program counter offset. |
| 5984 | */ |
| 5985 | lo = 0; |
| 5986 | hi = dbg_ptr.get_line_count(vmg0_) - 1; |
| 5987 | while (lo <= hi) |
| 5988 | { |
| 5989 | ulong start_ofs; |
| 5990 | ulong end_ofs; |
| 5991 | CVmDbgLinePtr line_ptr; |
| 5992 | |
| 5993 | /* split the difference and get the current entry */ |
| 5994 | cur = lo + (hi - lo)/2; |
| 5995 | dbg_ptr.set_line_ptr(vmg_ &line_ptr, cur); |
| 5996 | |
| 5997 | /* get the current statement's start relative to the method header */ |
| 5998 | start_ofs = line_ptr.get_start_ofs(); |
| 5999 | |
| 6000 | /* |
| 6001 | * Get the next statement's start offset, which gives us the end |
| 6002 | * of this statement. If this is the last statement in the table, |
| 6003 | * it runs to the end of the function; use the debug records table |
| 6004 | * offset as the upper bound in this case. |
| 6005 | */ |
| 6006 | if (cur == (int)dbg_ptr.get_line_count(vmg0_) - 1) |
| 6007 | { |
| 6008 | /* |
| 6009 | * it's the last record - use the debug table offset as an |
| 6010 | * upper bound, since we know the function can't have any |
| 6011 | * executable code past this point |
| 6012 | */ |
| 6013 | end_ofs = func_ptr->get_debug_ofs(); |
| 6014 | } |
| 6015 | else |
| 6016 | { |
| 6017 | CVmDbgLinePtr next_line_ptr; |
| 6018 | |
| 6019 | /* another record follows this one - use it */ |
| 6020 | next_line_ptr.copy_from(&line_ptr); |
| 6021 | next_line_ptr.inc(vmg0_); |
| 6022 | end_ofs = next_line_ptr.get_start_ofs(); |
| 6023 | } |
| 6024 | |
| 6025 | /* see where we are relative to this line record */ |
| 6026 | if (method_ofs >= end_ofs) |
| 6027 | { |
| 6028 | /* we need to go higher */ |
| 6029 | lo = (cur == lo ? cur + 1 : cur); |
| 6030 | } |
| 6031 | else if (method_ofs < start_ofs) |
| 6032 | { |
| 6033 | /* we need to go lower */ |
| 6034 | hi = (cur == hi ? hi - 1 : cur); |
| 6035 | } |
| 6036 | else |
| 6037 | { |
| 6038 | /* found it - set the bounds to this record's limits */ |
| 6039 | *stm_start = func_ptr->get() + start_ofs; |
| 6040 | *stm_end = func_ptr->get() + end_ofs; |
| 6041 | |
| 6042 | /* fill in the caller's line pointer if desired */ |
| 6043 | if (caller_line_ptr != 0) |
| 6044 | caller_line_ptr->copy_from(&line_ptr); |
| 6045 | |
| 6046 | /* indicate that we found the line boundaries successfully */ |
| 6047 | return TRUE; |
| 6048 | } |
| 6049 | } |
| 6050 | |
| 6051 | /* return failure */ |
| 6052 | return FALSE; |
| 6053 | } |
| 6054 | |
| 6055 | /* ------------------------------------------------------------------------ */ |
| 6056 | /* |
| 6057 | * Profiler functions |
| 6058 | */ |
| 6059 | #ifdef VM_PROFILER |
| 6060 | |
| 6061 | /* |
| 6062 | * Profiler master hash table entry |
| 6063 | */ |
| 6064 | class CVmHashEntryProfiler: public CVmHashEntryCI |
| 6065 | { |
| 6066 | public: |
| 6067 | CVmHashEntryProfiler(const char *str, size_t len, |
| 6068 | const vm_profiler_rec *rec) |
| 6069 | : CVmHashEntryCI(str, len, TRUE) |
| 6070 | { |
| 6071 | /* copy the profiler record's identifying portion */ |
| 6072 | rec_.func = rec->func; |
| 6073 | rec_.obj = rec->obj; |
| 6074 | rec_.prop = rec->prop; |
| 6075 | |
| 6076 | /* initialize the timers and counters to zero */ |
| 6077 | rec_.sum_direct.hi = rec_.sum_direct.lo = 0; |
| 6078 | rec_.sum_chi.hi = rec_.sum_chi.lo = 0; |
| 6079 | rec_.call_cnt = 0; |
| 6080 | } |
| 6081 | |
| 6082 | /* our profiler record */ |
| 6083 | vm_profiler_rec rec_; |
| 6084 | }; |
| 6085 | |
| 6086 | /* |
| 6087 | * Begin profiling |
| 6088 | */ |
| 6089 | void CVmRun::start_profiling() |
| 6090 | { |
| 6091 | /* clear any old profiler data from the master hash table */ |
| 6092 | prof_master_table_->delete_all_entries(); |
| 6093 | |
| 6094 | /* reset the profiler stack */ |
| 6095 | prof_stack_idx_ = 0; |
| 6096 | |
| 6097 | /* turn on profiling */ |
| 6098 | profiling_ = TRUE; |
| 6099 | } |
| 6100 | |
| 6101 | /* |
| 6102 | * End profiling |
| 6103 | */ |
| 6104 | void CVmRun::end_profiling() |
| 6105 | { |
| 6106 | /* turn off profiling */ |
| 6107 | profiling_ = FALSE; |
| 6108 | |
| 6109 | /* leave all active profiler stack levels */ |
| 6110 | while (prof_stack_idx_ != 0) |
| 6111 | prof_leave(); |
| 6112 | } |
| 6113 | |
| 6114 | /* context for our profiling callback */ |
| 6115 | struct vmrun_prof_enum |
| 6116 | { |
| 6117 | /* globals object */ |
| 6118 | vm_globals *globals; |
| 6119 | |
| 6120 | /* interpreter object */ |
| 6121 | CVmRun *terp; |
| 6122 | |
| 6123 | /* debugger object */ |
| 6124 | CVmDebug *dbg; |
| 6125 | |
| 6126 | /* client callback and its context */ |
| 6127 | void (*cb)(void *, const char *, unsigned long, unsigned long, |
| 6128 | unsigned long); |
| 6129 | void *cb_ctx; |
| 6130 | }; |
| 6131 | |
| 6132 | /* |
| 6133 | * Get the profiling data |
| 6134 | */ |
| 6135 | void CVmRun::get_profiling_data(VMG_ |
| 6136 | void (*cb)(void *, |
| 6137 | const char *, |
| 6138 | unsigned long, |
| 6139 | unsigned long, |
| 6140 | unsigned long), |
| 6141 | void *cb_ctx) |
| 6142 | { |
| 6143 | vmrun_prof_enum our_ctx; |
| 6144 | |
| 6145 | /* if there's no debugger, we can't get symbols, so we can't proceed */ |
| 6146 | if (G_debugger == 0) |
| 6147 | return; |
| 6148 | |
| 6149 | /* set up our callback context */ |
| 6150 | our_ctx.globals = VMGLOB_ADDR; |
| 6151 | our_ctx.terp = this; |
| 6152 | our_ctx.dbg = G_debugger; |
| 6153 | our_ctx.cb = cb; |
| 6154 | our_ctx.cb_ctx = cb_ctx; |
| 6155 | |
| 6156 | /* enumerate the master table entries through our callback */ |
| 6157 | prof_master_table_->enum_entries(&prof_enum_cb, &our_ctx); |
| 6158 | } |
| 6159 | |
| 6160 | /* |
| 6161 | * Callback for enumerating the profiling data |
| 6162 | */ |
| 6163 | void CVmRun::prof_enum_cb(void *ctx0, CVmHashEntry *entry0) |
| 6164 | { |
| 6165 | vmrun_prof_enum *ctx = (vmrun_prof_enum *)ctx0; |
| 6166 | VMGLOB_PTR(ctx->globals); |
| 6167 | CVmHashEntryProfiler *entry = (CVmHashEntryProfiler *)entry0; |
| 6168 | char namebuf[128]; |
| 6169 | const char *p; |
| 6170 | |
| 6171 | /* generate the name of the function or method */ |
| 6172 | if (entry->rec_.obj != VM_INVALID_OBJ) |
| 6173 | { |
| 6174 | char *dst; |
| 6175 | |
| 6176 | /* look up the object name */ |
| 6177 | p = ctx->dbg->objid_to_sym(entry->rec_.obj); |
| 6178 | |
| 6179 | /* get the original name, if this is a synthetic 'modify' object */ |
| 6180 | p = ctx->dbg->get_modifying_sym(p); |
| 6181 | |
| 6182 | /* |
| 6183 | * if we got an object name, use it; otherwise, synthesize a name |
| 6184 | * using the object number |
| 6185 | */ |
| 6186 | if (p != 0) |
| 6187 | strcpy(namebuf, p); |
| 6188 | else |
| 6189 | sprintf(namebuf, "obj#%lx", (long)entry->rec_.obj); |
| 6190 | |
| 6191 | /* add a period */ |
| 6192 | dst = namebuf + strlen(namebuf); |
| 6193 | *dst++ = '.'; |
| 6194 | |
| 6195 | /* look up the property name */ |
| 6196 | p = ctx->dbg->propid_to_sym(entry->rec_.prop); |
| 6197 | if (p != 0) |
| 6198 | strcpy(dst, p); |
| 6199 | else |
| 6200 | sprintf(dst, "prop#%x", (int)entry->rec_.prop); |
| 6201 | } |
| 6202 | else if (entry->rec_.func != 0) |
| 6203 | { |
| 6204 | /* look up the function at the code offset */ |
| 6205 | char buf[256]; |
| 6206 | p = ctx->dbg->funchdr_to_sym(vmg_ entry->rec_.func, buf); |
| 6207 | if (p != 0) |
| 6208 | strcpy(namebuf, p); |
| 6209 | else |
| 6210 | sprintf(namebuf, "func#%lx", (long)entry->rec_.func); |
| 6211 | } |
| 6212 | else |
| 6213 | { |
| 6214 | /* it must be system code */ |
| 6215 | strcpy(namebuf, "<System>"); |
| 6216 | } |
| 6217 | |
| 6218 | /* invoke the callback with the data */ |
| 6219 | (*ctx->cb)(ctx->cb_ctx, namebuf, |
| 6220 | os_prof_time_to_ms(&entry->rec_.sum_direct), |
| 6221 | os_prof_time_to_ms(&entry->rec_.sum_chi), |
| 6222 | entry->rec_.call_cnt); |
| 6223 | } |
| 6224 | |
| 6225 | |
| 6226 | /* |
| 6227 | * Profile entry into a new function or method |
| 6228 | */ |
| 6229 | void CVmRun::prof_enter(VMG_ const uchar *fptr) |
| 6230 | { |
| 6231 | vm_prof_time cur; |
| 6232 | |
| 6233 | /* pull the target property and defining object from the frame */ |
| 6234 | vm_obj_id_t obj = get_defining_obj(vmg0_); |
| 6235 | vm_prop_id_t prop = get_target_prop(vmg0_); |
| 6236 | |
| 6237 | /* get the current time */ |
| 6238 | os_prof_curtime(&cur); |
| 6239 | |
| 6240 | /* if we have a valid previous entry, suspend it */ |
| 6241 | if (prof_stack_idx_ > 0 && prof_stack_idx_ - 1 < prof_stack_max_) |
| 6242 | { |
| 6243 | vm_profiler_rec *p; |
| 6244 | vm_prof_time delta; |
| 6245 | |
| 6246 | /* get a pointer to the outgoing entry */ |
| 6247 | p = &prof_stack_[prof_stack_idx_ - 1]; |
| 6248 | |
| 6249 | /* |
| 6250 | * add the time since the last start to the cumulative time spent |
| 6251 | * in this function |
| 6252 | */ |
| 6253 | prof_calc_elapsed(&delta, &cur, &prof_start_); |
| 6254 | prof_add_elapsed(&p->sum_direct, &delta); |
| 6255 | } |
| 6256 | |
| 6257 | /* if we have room on the profiler stack, add a new level */ |
| 6258 | if (prof_stack_idx_ < prof_stack_max_) |
| 6259 | { |
| 6260 | vm_profiler_rec *p; |
| 6261 | |
| 6262 | /* get a pointer to the new entry */ |
| 6263 | p = &prof_stack_[prof_stack_idx_]; |
| 6264 | |
| 6265 | /* remember the identifying data for the method or function */ |
| 6266 | p->func = fptr; |
| 6267 | p->obj = obj; |
| 6268 | p->prop = prop; |
| 6269 | |
| 6270 | /* we have no cumulative time yet */ |
| 6271 | p->sum_direct.hi = p->sum_direct.lo = 0; |
| 6272 | p->sum_chi.hi = p->sum_chi.lo = 0; |
| 6273 | } |
| 6274 | |
| 6275 | /* count the level */ |
| 6276 | ++prof_stack_idx_; |
| 6277 | |
| 6278 | /* remember the start time in the new current function */ |
| 6279 | os_prof_curtime(&prof_start_); |
| 6280 | } |
| 6281 | |
| 6282 | /* |
| 6283 | * Profile returning from a function or method |
| 6284 | */ |
| 6285 | void CVmRun::prof_leave() |
| 6286 | { |
| 6287 | vm_prof_time delta; |
| 6288 | vm_prof_time cur; |
| 6289 | vm_prof_time chi; |
| 6290 | |
| 6291 | /* get the current time */ |
| 6292 | os_prof_curtime(&cur); |
| 6293 | |
| 6294 | /* move to the last level */ |
| 6295 | --prof_stack_idx_; |
| 6296 | |
| 6297 | /* presume we won't know the child time */ |
| 6298 | chi.hi = chi.lo = 0; |
| 6299 | |
| 6300 | /* if we're on a valid level, finish the call */ |
| 6301 | if (prof_stack_idx_ < prof_stack_max_) |
| 6302 | { |
| 6303 | vm_profiler_rec *p; |
| 6304 | CVmHashEntryProfiler *entry; |
| 6305 | |
| 6306 | /* get a pointer to the outgoing entry */ |
| 6307 | p = &prof_stack_[prof_stack_idx_]; |
| 6308 | |
| 6309 | /* |
| 6310 | * add the time since the last start to the cumulative time spent |
| 6311 | * in this function |
| 6312 | */ |
| 6313 | prof_calc_elapsed(&delta, &cur, &prof_start_); |
| 6314 | prof_add_elapsed(&p->sum_direct, &delta); |
| 6315 | |
| 6316 | /* |
| 6317 | * Find or create the master record for the terminating function or |
| 6318 | * method, and add the cumulative times from this call to the |
| 6319 | * master record's cumulative times. Also count the invocation in |
| 6320 | * the master record. |
| 6321 | */ |
| 6322 | entry = prof_find_master_rec(p); |
| 6323 | prof_add_elapsed(&entry->rec_.sum_direct, &p->sum_direct); |
| 6324 | prof_add_elapsed(&entry->rec_.sum_chi, &p->sum_chi); |
| 6325 | ++(entry->rec_.call_cnt); |
| 6326 | |
| 6327 | /* |
| 6328 | * Calculate the cumulative time in the outgoing function - this is |
| 6329 | * the total time directly in the function plus the cumulative time |
| 6330 | * in all of its children. We must add this to the caller's |
| 6331 | * cumulative child time, since this function and all of its |
| 6332 | * children are children of the caller and thus must count in the |
| 6333 | * caller's total child time. |
| 6334 | */ |
| 6335 | chi = p->sum_direct; |
| 6336 | prof_add_elapsed(&chi, &p->sum_chi); |
| 6337 | } |
| 6338 | |
| 6339 | /* if we're leaving to a valid level, re-activate it */ |
| 6340 | if (prof_stack_idx_ > 0 && prof_stack_idx_ < prof_stack_max_) |
| 6341 | { |
| 6342 | vm_profiler_rec *p; |
| 6343 | |
| 6344 | /* get a pointer to the resuming entry */ |
| 6345 | p = &prof_stack_[prof_stack_idx_ - 1]; |
| 6346 | |
| 6347 | /* |
| 6348 | * add the time spent in the child and its children to our |
| 6349 | * cumulative child time |
| 6350 | */ |
| 6351 | prof_add_elapsed(&p->sum_chi, &chi); |
| 6352 | } |
| 6353 | |
| 6354 | /* |
| 6355 | * remember the new start time for the function we're resuming - we |
| 6356 | * must reset this to the current time, since we measure deltas from |
| 6357 | * the last call or return on each call or return |
| 6358 | */ |
| 6359 | os_prof_curtime(&prof_start_); |
| 6360 | } |
| 6361 | |
| 6362 | /* |
| 6363 | * Calculate an elapsed 64-bit time value |
| 6364 | */ |
| 6365 | void CVmRun::prof_calc_elapsed(vm_prof_time *diff, const vm_prof_time *a, |
| 6366 | const vm_prof_time *b) |
| 6367 | { |
| 6368 | /* calculate the differences of the low and high parts */ |
| 6369 | diff->lo = a->lo - b->lo; |
| 6370 | diff->hi = a->hi - b->hi; |
| 6371 | |
| 6372 | /* |
| 6373 | * if the low part ended up higher than it started, then we |
| 6374 | * underflowed, and hence must borrow from the high part |
| 6375 | */ |
| 6376 | if (diff->lo > a->lo) |
| 6377 | --(diff->hi); |
| 6378 | } |
| 6379 | |
| 6380 | /* |
| 6381 | * Add one elapsed time value to another |
| 6382 | */ |
| 6383 | void CVmRun::prof_add_elapsed(vm_prof_time *sum, const vm_prof_time *val) |
| 6384 | { |
| 6385 | unsigned long orig_lo; |
| 6386 | |
| 6387 | /* remember the original low part */ |
| 6388 | orig_lo = sum->lo; |
| 6389 | |
| 6390 | /* add the low parts and high parts */ |
| 6391 | sum->lo += val->lo; |
| 6392 | sum->hi += val->hi; |
| 6393 | |
| 6394 | /* |
| 6395 | * if the low part of the sum is less than where it started, then it |
| 6396 | * overflowed, and we must hence carry to the high part |
| 6397 | */ |
| 6398 | if (sum->lo < orig_lo) |
| 6399 | ++(sum->hi); |
| 6400 | } |
| 6401 | |
| 6402 | /* |
| 6403 | * Find or create a hash table entry for a profiler record |
| 6404 | */ |
| 6405 | CVmHashEntryProfiler *CVmRun::prof_find_master_rec(const vm_profiler_rec *p) |
| 6406 | { |
| 6407 | const size_t id_siz = sizeof(p->func) + sizeof(p->obj) + sizeof(p->prop); |
| 6408 | char id[id_siz]; |
| 6409 | CVmHashEntryProfiler *entry; |
| 6410 | |
| 6411 | /* |
| 6412 | * Build the ID string, which we'll use as our hash key. We never have |
| 6413 | * to serialize this, so it doesn't matter that it's dependent on byte |
| 6414 | * order and word size. |
| 6415 | */ |
| 6416 | memcpy(id, &p->func, sizeof(p->func)); |
| 6417 | memcpy(id + sizeof(p->func), &p->obj, sizeof(p->obj)); |
| 6418 | memcpy(id + sizeof(p->func) + sizeof(p->obj), &p->prop, sizeof(p->prop)); |
| 6419 | |
| 6420 | /* try to find an existing entry */ |
| 6421 | entry = (CVmHashEntryProfiler *)prof_master_table_->find(id, id_siz); |
| 6422 | |
| 6423 | /* if we didn't find an entry, create one */ |
| 6424 | if (entry == 0) |
| 6425 | { |
| 6426 | /* create a new entry */ |
| 6427 | entry = new CVmHashEntryProfiler(id, id_siz, p); |
| 6428 | |
| 6429 | /* add it to the table */ |
| 6430 | prof_master_table_->add(entry); |
| 6431 | } |
| 6432 | |
| 6433 | /* return the entry */ |
| 6434 | return entry; |
| 6435 | } |
| 6436 | |
| 6437 | #endif /* VM_PROFILER */ |
| 6438 | |
| 6439 | /* ------------------------------------------------------------------------ */ |
| 6440 | /* |
| 6441 | * Footnote - for the referring code, search the code above for |
| 6442 | * [REGISTER_P_FOOTNOTE]. |
| 6443 | * |
| 6444 | * This footnote pertains to a 'register' declaration that causes gcc (and |
| 6445 | * probably some other compilers) to generate a warning message. The |
| 6446 | * 'register' declaration is useful on some compilers and will be retained. |
| 6447 | * Here's a note I sent to Nikos Chantziaras (who asked about the warning) |
| 6448 | * explaining why I'm choosing to leave the 'register' declaration in, and |
| 6449 | * why I think this 'register' declaration is actually correct and useful |
| 6450 | * despite the warning it generates on some compilers. |
| 6451 | * |
| 6452 | * The basic issue is that the code takes the address of the variable in |
| 6453 | * question in expressions passed as parameters to certain function calls. |
| 6454 | * These function calls all happen to be in-linable functions, and it |
| 6455 | * happens that in each function, the address operator is always canceled |
| 6456 | * out by a '*' dereference operator - in other words, we have '*&p', which |
| 6457 | * the compiler can turn into just plain 'p' when the calls are in-lined, |
| 6458 | * eliminating the need to actually take the address of 'p'. |
| 6459 | * |
| 6460 | * Nikos: |
| 6461 | *. >I'm no expert, but I think GCC barks at this because it isn't possible |
| 6462 | *. >at all to store the variable in a register if the code wants its |
| 6463 | *. >address, therefore the 'register' in the declaration does nothing. |
| 6464 | * |
| 6465 | * That's correct, but a compiler is always free to ignore 'register' |
| 6466 | * declarations *anyway*, even if enregistration is possible. Therefore a |
| 6467 | * warning that it's not possible to obey 'register' is unnecessary, |
| 6468 | * because it's explicit in the language definition that 'register' is not |
| 6469 | * binding. It simply is not possible for an ignored 'register' attribute |
| 6470 | * to cause unexpected behavior. Warnings really should only be generated |
| 6471 | * for situations where it is likely that the programmer expects different |
| 6472 | * behavior than the compiler will deliver; in the case of an ignored |
| 6473 | * 'register' attribute, the programmer is *required* to expect that the |
| 6474 | * attribute might be ignored, so a warning to this effect is superfluous. |
| 6475 | * |
| 6476 | * Now, I understand why they generate the warning - it's because the |
| 6477 | * compiler believes that the program code itself makes enregistration |
| 6478 | * impossible, not because the compiler has chosen for optimization |
| 6479 | * purposes to ignore the 'register' request. However, as we'll see |
| 6480 | * shortly, the program code doesn't truly make enregistration impossible; |
| 6481 | * it is merely impossible in some interpretations of the code. Therefore |
| 6482 | * we really are back to the compiler choosing to ignore the 'register' |
| 6483 | * request due to its own optimization decisions; the 'register' request is |
| 6484 | * made impossible far downstream of the actual decisions that the compiler |
| 6485 | * makes (which have to do with in-line vs out-of-line calls), but it |
| 6486 | * really is compiler decisions that make it impossible, not the inherent |
| 6487 | * structure of the code. |
| 6488 | * |
| 6489 | *. >Furthermore, I'm not sure I understand the relationship |
| 6490 | *. >between 'register' and inlining; why should "*(&p)" do something |
| 6491 | *. >else "in calls to inlines" than its obvious meaning? |
| 6492 | * |
| 6493 | * When a function is in-lined, the compiler is not required to generate |
| 6494 | * the same code it would generate for the most general case of the same |
| 6495 | * function call, as long as the meaning is the same. |
| 6496 | * |
| 6497 | * For example, suppose we have some code that contains a call to a |
| 6498 | * function like so: |
| 6499 | * |
| 6500 | * a = myFunc(a + 7, 3); |
| 6501 | * |
| 6502 | * In the general out-of-line case, the compiler must generate some |
| 6503 | * machine-code instructions like this: |
| 6504 | * |
| 6505 | *. push #3 |
| 6506 | *. mov [a], d0 |
| 6507 | *. add #7, d0 |
| 6508 | *. push d0 |
| 6509 | *. call #myFunc |
| 6510 | *. mov d0, [a] |
| 6511 | * |
| 6512 | * The compiler doesn't have access to the inner workings of myFunc, so it |
| 6513 | * must generate the appropriate code for the generic interface to an |
| 6514 | * external function. |
| 6515 | * |
| 6516 | * Now, suppose the function is defined like so: |
| 6517 | * |
| 6518 | * int myFunc(int a, int b) { return a - 6; } |
| 6519 | * |
| 6520 | * and further suppose that the compiler decides to in-line this function. |
| 6521 | * In-lining means the compiler will generate the code that implements the |
| 6522 | * function directly in the caller; there will be no call to an external |
| 6523 | * linkage point. This means the compiler can implement the linkage to the |
| 6524 | * function with a custom one-off interface for this particular invocation |
| 6525 | * - every in-line invocation can be customized to the exact context where |
| 6526 | * it appears. So, for example, if we call myFunc right now and registers |
| 6527 | * d1 and d2 happens to be available, we can put the parameters in d1 and |
| 6528 | * d2, and the generated function will refer to those registers for the |
| 6529 | * parameters rather than having to look in the stack. Later on, if we |
| 6530 | * generate a separate call to the same function, but registers d3 and d7 |
| 6531 | * are the ones available, we can use those instead. Each generated copy |
| 6532 | * of the function can fit its exact context. |
| 6533 | * |
| 6534 | * Furthermore, looking at this function and at the arguments passed, we |
| 6535 | * can see that the formal parameter 'b' has no effect on the function's |
| 6536 | * results, and the actual parameter '3' passed for 'b' has no side |
| 6537 | * effects. Therefore, the compiler is free to completely ignore this |
| 6538 | * parameter - there's no need to generate any code for it at all, since we |
| 6539 | * have sufficient knowledge to see that it has no effect on the meaning of |
| 6540 | * the code. |
| 6541 | * |
| 6542 | * Further still, we can globally optimize the entire function. So, we can |
| 6543 | * see that myFunc(a+7, 3) is going to turn into the expression (a+7-6). |
| 6544 | * We can fold constants to arrive at (a+1) as the result of the function. |
| 6545 | * We can therefore generate the entire code for the function's invocation |
| 6546 | * like so: |
| 6547 | * |
| 6548 | * inc [a] |
| 6549 | * |
| 6550 | * Okay, now let's look at the &p case. In the specific examples in |
| 6551 | * vmrun.cpp, we have a bunch of function invocations like this: |
| 6552 | * |
| 6553 | * register const char *p; |
| 6554 | *. int x = myfunc(&p); |
| 6555 | * |
| 6556 | * In the most general case, we have to generate code like this: |
| 6557 | * |
| 6558 | *. lea [p], d0 ; load effective address |
| 6559 | *. push d0 |
| 6560 | *. call #myfunc |
| 6561 | *. mov d0, [x] |
| 6562 | * |
| 6563 | * So, in the most general case of a call with external linkage, we need |
| 6564 | * 'p' to have a main memory address so that we can push it on the stack as |
| 6565 | * the parameter to this call. Registers don't have main memory addresses, |
| 6566 | * so 'p' can't go in a register. |
| 6567 | * |
| 6568 | * However, we know what myfunc() looks like: |
| 6569 | * |
| 6570 | *. char myfunc(const char **p) |
| 6571 | *. { |
| 6572 | *. char c = **p; |
| 6573 | *. *p += 1; |
| 6574 | *. return c; |
| 6575 | *. } |
| 6576 | * |
| 6577 | * If the compiler chooses to in-line this function, it can globally |
| 6578 | * optimize its linkage and implementation as we saw earlier. So, the |
| 6579 | * compiler can rewrite the code like so: |
| 6580 | * |
| 6581 | * register const char *p; |
| 6582 | *. int x = **(&p); |
| 6583 | *. *(&p) += 1; |
| 6584 | * |
| 6585 | * which can be further rewritten to: |
| 6586 | * |
| 6587 | *. register const char *p; |
| 6588 | *. int x = *p; |
| 6589 | *. p += 1; |
| 6590 | * |
| 6591 | * Now we can generate the machine code for the final optimized form: |
| 6592 | * |
| 6593 | *. mov [p], a0 ; get the *value* of p into index register 0 |
| 6594 | *. mov.byte [a0+0], d0 ; get the value index register 0 points to |
| 6595 | *. mov.byte d0, [x] ; store it in x |
| 6596 | *. inc [p] ; inc the value of p |
| 6597 | * |
| 6598 | * Nowhere do we need a main memory address for p. This means the compiler |
| 6599 | * can keep p in a register, say d5: |
| 6600 | * |
| 6601 | *. mov d5, a0 |
| 6602 | *. mov.byte [a0+0], d0 |
| 6603 | *. mov.byte d0, [x] |
| 6604 | *. inc d5 |
| 6605 | * |
| 6606 | * And this is indeed exactly what the code that comes out of vc++ looks |
| 6607 | * like (changed from my abstract machine to 32-bit x86, of course). |
| 6608 | * |
| 6609 | * So: if the compiler chooses to in-line the functions that are called |
| 6610 | * with '&p' as a parameter, and the compiler performs the available |
| 6611 | * optimizations on those calls once they're in-lined, then a memory |
| 6612 | * address for 'p' is never needed. Thus there is a valid interpretation |
| 6613 | * of the code where 'register p' can be obeyed. If the compiler doesn't |
| 6614 | * choose to in-line the functions or make those optimizations, then the |
| 6615 | * compiler will be unable to satisfy the 'register p' request and will be |
| 6616 | * forced to put 'p' in addressable main memory. But it really is entirely |
| 6617 | * up to the compiler whether to obey the 'register p' request; the |
| 6618 | * program's structure does not make the request impossible to satisfy. |
| 6619 | * Therefore there is no reason for the compiler to warn about this, any |
| 6620 | * more than there would be if the compiler chose not to obey the 'register |
| 6621 | * p' simply because it thought it could make more optimal use of the |
| 6622 | * available registers. That gcc warns is understandable, in that a |
| 6623 | * superficial reading of the code would not reveal the optimization |
| 6624 | * opportunity; but the warning is nonetheless unnecessary, and the |
| 6625 | * 'register' does provide useful optimization hinting to at least vc++, so |
| 6626 | * I think it's best to leave it in and ignore the warning. |
| 6627 | */ |