Debian Codegraph @

terps/tads/tads3/vmrun.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/vmrun.h,v 1.4 1999/07/11 00:46:59 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmrun.h - VM Execution
12 Function
13
14 Notes
15
16 Modified
17 11/12/98 MJRoberts - Creation
18 */
19
20 #ifndef VMRUN_H
21 #define VMRUN_H
22
23 #include "vmglob.h"
24 #include "vmtype.h"
25 #include "vmstack.h"
26 #include "vmpool.h"
27 #include "vmobj.h"
28 #include "vmerr.h"
29 #include "vmerrnum.h"
30 #include "vmprofty.h"
31 #include "vmfunc.h"
32
33
34 /* ------------------------------------------------------------------------ */
35 /*
36 * for debugger use - interpreter context save structure
37 */
38 struct vmrun_save_ctx
39 {
40 const uchar *entry_ptr_native_;
41 vm_val_t *frame_ptr_;
42 size_t old_stack_depth_;
43 const uchar **pc_ptr_;
44 };
45
46 /* ------------------------------------------------------------------------ */
47 /*
48 * Recursive VM call descriptor. When making a recursive call into the VM,
49 * a caller can set up one of these structures to describe the caller.
50 * We'll store this in the stack in the recursive context slot.
51 */
52 struct vm_rcdesc
53 {
54 /* no-op construction */
55 vm_rcdesc() { }
56
57 /* construct for a system caller */
58 vm_rcdesc(const char *name) { init(name); }
59
60 /* initialize for a system caller */
61 void init(const char *name)
62 {
63 this->name = name;
64 bifptr.set_nil();
65 self.set_nil();
66 method_idx = 0;
67 argc = 0;
68 argp = 0;
69 caller_addr = 0;
70 }
71
72 /* initialize for a system caller, including a return address */
73 void init_ret(VMG_ const char *name);
74
75 /* construct for a built-in function */
76 vm_rcdesc(VMG_ const char *name,
77 const struct vm_bif_desc *funcset, int idx,
78 vm_val_t *argp, int argc)
79 {
80 init(vmg_ name, funcset, idx, argp, argc);
81 }
82
83 /* initialize for a built-in function */
84 void init(VMG_ const char *name,
85 const struct vm_bif_desc *funcset, int idx,
86 vm_val_t *argp, int argc);
87
88 /* construct for an intrinsic class method */
89 vm_rcdesc(VMG_ const char *name,
90 vm_obj_id_t self, unsigned short method_idx,
91 vm_val_t *argp, int argc)
92 {
93 init(vmg_ name, self, method_idx, argp, argc);
94 }
95
96 /* initialize for an intrinsic class method */
97 void init(VMG_ const char *name,
98 vm_obj_id_t self, unsigned short method_idx,
99 vm_val_t *argp, int argc);
100
101 /* construct for an intrinsic class method */
102 vm_rcdesc(VMG_ const char *name,
103 vm_obj_id_t self, unsigned short method_idx,
104 vm_val_t *argp, uint *argc)
105 {
106 init(vmg_ name, self, method_idx, argp, argc != 0 ? *argc : 0);
107 }
108
109 /* initialize for an intrinsic class method */
110 void init(VMG_ const char *name,
111 const vm_val_t *self, unsigned short method_idx,
112 vm_val_t *argp, int argc);
113
114 /* construct for an intrinsic class method */
115 vm_rcdesc(VMG_ const char *name,
116 const vm_val_t *self, unsigned short method_idx,
117 vm_val_t *argp, int argc)
118 {
119 init(vmg_ name, self, method_idx, argp, argc);
120 }
121
122 /* construct for an intrinsic class method */
123 vm_rcdesc(VMG_ const char *name,
124 const vm_val_t *self, unsigned short method_idx,
125 vm_val_t *argp, uint *argc)
126 {
127 init(vmg_ name, self, method_idx, argp, argc != 0 ? *argc : 0);
128 }
129
130 /* is there a return address available in the calling frame? */
131 int has_return_addr() const { return caller_addr != 0; }
132
133 /*
134 * Compute the return address in the calling frame. This requires
135 * figuring the size of the instruction at caller_addr (which is
136 * relatively quick, but not quick enough that we want to precompute it
137 * on every call).
138 */
139 const uchar *get_return_addr() const;
140
141 /* pointer to first argument in stack */
142 vm_val_t *argp;
143
144 /* number of arguments */
145 int argc;
146
147 /* descriptive name, for error messages */
148 const char *name;
149
150 /* built-in function pointer, if the caller is a bif */
151 vm_val_t bifptr;
152
153 /* 'self', if the caller is an intrinsic class */
154 vm_val_t self;
155
156 /* for an intrinsic class caller, the method index of the caller */
157 unsigned short method_idx;
158
159 /*
160 * Byte-code address of the instruction in the calling frame that
161 * triggered the call. The return address can be computed by adding
162 * the length of that instruction to this address.
163 */
164 const uchar *caller_addr;
165 };
166
167
168 /* ------------------------------------------------------------------------ */
169 /*
170 * Pseudo subroutine addresses.
171 *
172 * There are some situations where the bytecode engine needs to patch in
173 * what's effectively a local subroutine to invoke on return from a
174 * function or method call. (A local subroutine is one that doesn't
175 * involve a method header change - it's just a matter of pushing a return
176 * address and jumping, similar to the way LJSR and LRET work.)
177 *
178 * In a real machine, we'd just load the subroutine's instructions into
179 * some reserved area of system memory, and use the address of the
180 * subroutine code as the return address when making the call. We'd also
181 * push the *real* return address before this, so that the subroutine would
182 * be able to do the local return to the actual calling address upon
183 * completion.
184 *
185 * Due to the way we manage memory, though, we can't do this. Return
186 * addresses are always offsets from a method header, since the only
187 * addresses we're allowed to resolve are method header addresses. (This
188 * is largely because memory can be discontiguous outside of a single
189 * method block.)
190 *
191 * So what we do instead is define a range of return addresses that are
192 * impossible, and use those to signal these special internal subroutines.
193 * A return address is always an offset from the start of a method header,
194 * so, conveniently, any offset less than the size of a header is
195 * inherently invalid as an actual return address. This gives us a range
196 * of addresses that are safe to use to signal special meanings. The
197 * original T3 method header is 10 bytes, so any return offset from 0 to 9
198 * is invalid and thus can have a special meaning.
199 */
200
201 /* is the given offset a special return address? */
202 inline int vmrun_is_special_return(uint ofs) { return ofs < 10; }
203
204 /*
205 * Special return address: Recursive Call Return. This value for the
206 * return offset indicates that this is a recursive call into the VM, so
207 * the VM bytecode execution loop should simply return when this frame
208 * exits.
209 */
210 const uint VMRUN_RET_RECURSIVE = 0;
211
212 /*
213 * Special return address: Return from Operator Overload. This value for
214 * the return offset indicates that this is a call to evaluate an
215 * overloaded operator. On return from this frame, execute the following
216 * local subroutine:
217 *
218 *. GETR0 ; push return value from R0 onto the stack
219 *. SWAP ; arrange stack: [sp]=return offset, [sp+1]=return value
220 *. LRET [sp+] ; pop stack -> X and return to offset X in current method
221 *
222 * The stack at entry is arranged as follows: [sp]=return offset
223 */
224 const uint VMRUN_RET_OP = 1;
225
226 /*
227 * Special return address: Return from Operator Overload And Assign Local.
228 * This value for the return address indicates that this is a call to
229 * evaluate an overloaded operator, and on return, we're to assign the
230 * result to a local variable. On return from this frame, execute the
231 * following local subroutine:
232 *
233 *. SETLCLR0 [sp+] ; pop stack -> V and assign R0 -> local variable #V
234 *. LRET [sp+] ; pop stack -> X and return to offset X in current method
235 *
236 * The stack is arranged at entry as follows: [sp]=local variable number to
237 * assign, [sp+1]=return offset
238 */
239 const uint VMRUN_RET_OP_ASILCL = 2;
240
241
242
243 /* ------------------------------------------------------------------------ */
244 /*
245 * Procedure activation frame. The activation frame is arranged as
246 * follows (the stack index increases reading down the list):
247 *
248 *. argument N
249 *. argument N-1
250 *. ...
251 *. argument 2
252 *. argument 1
253 *. target property
254 *. original target object
255 *. defining object
256 *. self
257 *. offset in calling method of next instruction to execute
258 *. caller's entry pointer (EP) register value
259 *. actual parameter count
260 *. caller's frame pointer <<<--- CURRENT FRAME POINTER
261 *. local variable 1
262 *. local variable 2
263 *. local variable 3
264 *
265 * So, local variable 1 is at (FP+1), local variable 2 is at (FP+2), and
266 * so on; the argument count is at (FP-1); 'self' is at (FP-4); argument 1
267 * is at (FP-5), argument 2 is at (FP-6), and so on.
268 */
269
270 /* offset from FP of first argument */
271 const int VMRUN_FPOFS_ARG1 = -11;
272
273 /* offset from FP of target property */
274 const int VMRUN_FPOFS_PROP = -10;
275
276 /* offset from FP of original target object */
277 const int VMRUN_FPOFS_ORIGTARG = -9;
278
279 /* offset from FP of defining object (definer of current method) */
280 const int VMRUN_FPOFS_DEFOBJ = -8;
281
282 /* offset from FP of 'self' */
283 const int VMRUN_FPOFS_SELF = -7;
284
285 /* invokee (this is the FuncPtr, DynamicFunc, AnonFunc, etc being invoked) */
286 const int VMRUN_FPOFS_INVOKEE = -6;
287
288 /* frame reference (for reflection access to the frame contents) */
289 const int VMRUN_FPOFS_FRAMEREF = -5;
290
291 /* recursive VM invocation native caller context */
292 const int VMRUN_FPOFS_RCDESC = -4;
293
294 /* offset from FP of return address */
295 const int VMRUN_FPOFS_RET = -3;
296
297 /* offset from FP of enclosing entry pointer */
298 const int VMRUN_FPOFS_ENC_EP = -2;
299
300 /* offset from FP of argument count */
301 const int VMRUN_FPOFS_ARGC = -1;
302
303 /* offset from FP of enclosing frame pointer */
304 const int VMRUN_FPOFS_ENC_FP = 0;
305
306 /* offset from FP of first local variable */
307 const int VMRUN_FPOFS_LCL1 = 1;
308
309
310 /* ------------------------------------------------------------------------ */
311 /*
312 * Define certain of the VM CPU registers in global variables, if
313 * applicable.
314 */
315 VM_IF_REGS_IN_GLOBALS(extern vm_val_t r0_;)
316 VM_IF_REGS_IN_GLOBALS(extern const uchar *entry_ptr_native_;)
317 VM_IF_REGS_IN_GLOBALS(extern vm_val_t *frame_ptr_;)
318 VM_IF_REGS_IN_GLOBALS(extern const uchar **pc_ptr_;)
319
320
321 /* ------------------------------------------------------------------------ */
322 /*
323 * VM Execution Engine class. This class handles execution of byte
324 * code.
325 */
326 class CVmRun: public CVmStack
327 {
328 friend class CVmDebug;
329 friend class vmrun_prop_eval;
330
331 public:
332 CVmRun(size_t max_depth, size_t reserve_depth);
333 ~CVmRun();
334
335 /* initialize */
336 void init();
337
338 /* terminate */
339 void terminate();
340
341 /*
342 * Get/set the method header size. This size is stored in the image
343 * file; the image loader sets this at load time to the value
344 * retrieved from the image file. All method headers in an image
345 * file use the same size.
346 */
347 void set_funchdr_size(size_t siz);
348 size_t get_funchdr_size() const { return funchdr_size_; }
349
350 /*
351 * Call a function or method. 'target_ptr' is the byte pointer to the
352 * code to invoke, and 'argc' is the number of arguments that the
353 * caller has pushed onto the stack.
354 *
355 * Before calling this, the caller must push onto the stack targetprop,
356 * targetobj, definingobj, and self, in that order. For a function,
357 * targetprop is VM_INVALID_PROP and all of the others are nil.
358 *
359 * 'caller_ofs' is the method offset (the byte code offset from the
360 * current entry pointer) in the caller. If 'caller_ofs' is non-zero,
361 * we'll set up to begin execution in the target code and return the
362 * new program counter. If 'caller_ofs' is zero, we'll invoke the VM
363 * byte code interpreter recursively, so this function will return only
364 * after the called code returns. When calling recursively, set
365 * 'recurse_calling' to a descriptive string that can be used to show
366 * the system code calling the recursive code in case of error.
367 *
368 * When calling a function, 'self' should be VM_INVALID_OBJ.
369 * Otherwise, this value gives the object whose method is being
370 * invoked.
371 *
372 * The return value is the new program counter. For recursive
373 * invocations, this will simply return null.
374 */
375 const uchar *do_call(VMG_ uint caller_ofs,
376 const uchar *target_ptr, uint argc,
377 const vm_rcdesc *recurse_ctx);
378
379 /* call a function, non-recursively */
380 const uchar *do_call_func_nr(VMG_ uint caller_ofs, pool_ofs_t ofs,
381 uint argc);
382
383 /*
384 * Call a function pointer value. If 'funcptr' contains a function
385 * pointer, we'll simply call the function; if it contains an
386 * anonymous function object, we'll call the anonymous function.
387 */
388 const uchar *call_func_ptr(VMG_ const vm_val_t *funcptr, uint argc,
389 const vm_rcdesc *recurse_ctx, uint caller_ofs);
390
391 /* call a function pointer, with the invocation frame already set up */
392 const uchar *call_func_ptr_fr(VMG_ const vm_val_t *funcptr, uint argc,
393 const vm_rcdesc *recurse_ctx,
394 uint caller_ofs);
395
396 /*
397 * Get the descriptive message, if any, from an exception object.
398 * The returned string will not be null-terminated, but the length
399 * will be stored in *msg_len. The returned string might point to
400 * constant pool data or data in an object, so it might not remain
401 * valid after a constant pool translation or garbage collection
402 * operation. If the exception has no message, we will return a
403 * null pointer.
404 */
405 static const char *get_exc_message(VMG_ const CVmException *exc,
406 size_t *msg_len);
407 static const char *get_exc_message(VMG_ vm_obj_id_t obj, size_t *msg_len);
408
409 /*
410 * Get the descriptive message from an exception. If the exception
411 * has a program-generated exception object, we'll try to get the
412 * message from that object; if it's a VM exception with no
413 * underlying object, we'll retrieve the VM message.
414 *
415 * If add_unh_prefix is true, we'll add an "unhandled exception:"
416 * prefix to the message if we retrieve the message from a
417 * program-defined exception. Otherwise, if it's a program
418 * exception, we won't add any prefix at all.
419 */
420 static void get_exc_message(VMG_ const CVmException *exc,
421 char *buf, size_t buflen, int add_unh_prefix);
422
423 /*
424 * Evaluate a property of an object. 'target_obj' is the object whose
425 * property is to be evaluated, 'target_prop' is the ID of the
426 * property to evaluate, and 'argc' is the number of arguments to the
427 * method.
428 *
429 * 'caller_ofs' is the current method offset (the offset from the
430 * current entry pointer to the current program counter) in the
431 * caller. If this is zero, we'll make a recursive call to the
432 * interpreter loop to execute any method code; thus, any method code
433 * will have run to completion by the time we return in this case.
434 *
435 * 'self' is the object in whose context we're to perform the code
436 * execution, if the property is a method. Note that 'self' may
437 * differ from 'target_obj' in some cases, particularly when
438 * inheriting.
439 *
440 * The return value is the new program counter from which execution
441 * should resume. This will be null (and can be ignored) for
442 * recursive invocations.
443 */
444 const uchar *get_prop(VMG_ uint caller_ofs,
445 const vm_val_t *target_obj,
446 vm_prop_id_t target_prop,
447 const vm_val_t *self, uint argc,
448 const vm_rcdesc *rc);
449
450 /*
451 * Simplified get_prop, for cases where the caller just wants to
452 * evaluate a property of a value (with recursive VM entry).
453 */
454 void get_prop(VMG_ vm_val_t *result,
455 const vm_val_t *obj, vm_prop_id_t prop, uint argc,
456 const vm_rcdesc *rc);
457
458 /*
459 * Evaluate a property for operator overloading.
460 */
461 const uchar *op_overload(VMG_ uint caller_ofs, int asi_lcl,
462 const vm_val_t *obj, vm_prop_id_t prop,
463 uint argc, int err);
464
465 /*
466 * Set a property of an object
467 */
468 void set_prop(VMG_ vm_obj_id_t obj, vm_prop_id_t prop,
469 const vm_val_t *new_val);
470
471 /* get data register 0 (R0) */
472 VM_REG_ACCESS vm_val_t *get_r0() { return &r0_; }
473
474 /* set the default "say" function */
475 void set_say_func(VMG_ const vm_val_t *val);
476
477 /* get the current default "say" function */
478 void get_say_func(vm_val_t *val) const;
479
480 /* set the default "say" method */
481 void set_say_method(vm_prop_id_t prop)
482 {
483 /* remember the property */
484 say_method_ = prop;
485 }
486
487 /* get the current "say" method */
488 vm_prop_id_t get_say_method() const { return say_method_; }
489
490 /* pop an integer value; throws an error if the value is not an integer */
491 void pop_int(VMG_ vm_val_t *val)
492 {
493 pop(val);
494 if (val->typ != VM_INT)
495 err_throw(VMERR_INT_VAL_REQD);
496 }
497
498 /* pop an object value */
499 void pop_obj(VMG_ vm_val_t *val)
500 {
501 pop(val);
502 if (val->typ != VM_OBJ)
503 err_throw(VMERR_OBJ_VAL_REQD);
504 }
505
506 /* pop a property pointer value */
507 void pop_prop(VMG_ vm_val_t *val)
508 {
509 pop(val);
510 if (val->typ != VM_PROP)
511 err_throw(VMERR_PROPPTR_VAL_REQD);
512 }
513
514 /* pop a function pointer value */
515 void pop_funcptr(VMG_ vm_val_t *val)
516 {
517 pop(val);
518 if (val->typ != VM_FUNCPTR)
519 err_throw(VMERR_FUNCPTR_VAL_REQD);
520 }
521
522 /*
523 * Pop two values from the stack. The values are popped in reverse
524 * order, so val2 has the value at the top of the stack. If the
525 * left operand was pushed first, this results in placing the left
526 * operand in val1 and the right operand in val2.
527 */
528 void popval_2(VMG_ vm_val_t *val1, vm_val_t *val2)
529 {
530 popval(vmg_ val2);
531 popval(vmg_ val1);
532 }
533
534 /*
535 * Pop the left-hand operand of a two-operand operator, leaving the
536 * right-hand operand on the stack. For binary operators, the
537 * left-hand value is pushed first, then the right-hand value; this
538 * means that the right value is at top-of-stack. So we basically have
539 * to pull the left-hand value out of the stack, then move the right
540 * value up one slot to close the gap.
541 */
542 void pop_left_op(VMG_ vm_val_t *left)
543 {
544 *left = *get(1);
545 *get(1) = *get(0);
546 discard();
547 }
548
549 /*
550 * Pop two integers, throwing an error if either value is not an
551 * integer. Pops the item at the top of the stack into val2, and
552 * the next value into val1.
553 */
554 void pop_int_2(VMG_ vm_val_t *val1, vm_val_t *val2)
555 {
556 popval(vmg_ val2);
557 popval(vmg_ val1);
558 if (val1->typ != VM_INT || val2->typ != VM_INT)
559 err_throw(VMERR_INT_VAL_REQD);
560 }
561
562 /*
563 * get the active function's argument count - we read the value from
564 * the first item below the frame pointer in the current frame
565 */
566 VM_REG_ACCESS int get_cur_argc(VMG0_) VM_REG_CONST
567 {
568 return get_from_frame(frame_ptr_, VMRUN_FPOFS_ARGC)->val.intval;
569 }
570
571 /*
572 * Get a parameter value; 0 is the first parameter, 1 is the second,
573 * and so on.
574 */
575 VM_REG_ACCESS vm_val_t *get_param(VMG_ int idx) VM_REG_CONST
576 { return get_param_from_frame(vmg_ frame_ptr_, idx); }
577
578 /* get a parameter from a given frame */
579 VM_REG_ACCESS vm_val_t *get_param_from_frame(
580 VMG_ vm_val_t *fp, int idx) VM_REG_CONST
581 { return get_from_frame(fp, VMRUN_FPOFS_ARG1 - idx); }
582
583 /* get a parameter at the given stack level */
584 VM_REG_ACCESS vm_val_t *get_param_at_level(
585 VMG_ int idx, int level) VM_REG_CONST
586 {
587 return get_param_from_frame(vmg_ get_fp_at_level(vmg_ level), idx);
588 }
589
590 /* get the named parameter table, if any, from a frame */
591 static const uchar *get_named_args_from_frame(
592 VMG_ vm_val_t *fp, vm_val_t **arg0);
593
594 /*
595 * get a local variable's value; 0 is the first local variable, 1 is
596 * the second, and so on
597 */
598 VM_REG_ACCESS vm_val_t *get_local(VMG_ int idx) VM_REG_CONST
599 { return get_local_from_frame(vmg_ frame_ptr_, idx); }
600
601 /* get a local from a given frame */
602 VM_REG_ACCESS vm_val_t *get_local_from_frame(
603 VMG_ vm_val_t *fp, int idx) VM_REG_CONST
604 { return get_from_frame(fp, VMRUN_FPOFS_LCL1 + idx); }
605
606 /* get a local at the given stack level */
607 VM_REG_ACCESS vm_val_t *get_local_at_level(
608 VMG_ int idx, int level) VM_REG_CONST
609 {
610 return get_local_from_frame(vmg_ get_fp_at_level(vmg_ level), idx);
611 }
612
613 /* get a local, parameter, or context local at a given stack level */
614 VM_REG_ACCESS void get_local_from_frame(
615 VMG_ vm_val_t *val, vm_val_t *fp, const class CVmDbgFrameSymPtr *symp);
616 VM_REG_ACCESS void get_local_from_frame(
617 VMG_ vm_val_t *val, vm_val_t *fp, int varnum, int is_param,
618 int is_ctx_local, int ctx_arr_idx);
619
620 /* set a local, parameter, or context local at a given stack level */
621 VM_REG_ACCESS void set_local_in_frame(
622 VMG_ const vm_val_t *val, vm_val_t *fp,
623 const class CVmDbgFrameSymPtr *symp);
624 VM_REG_ACCESS void set_local_in_frame(
625 VMG_ const vm_val_t *val, vm_val_t *fp,
626 int varnum, int is_param, int is_ctx_local, int ctx_arr_idx);
627
628 /*
629 * Get the frame pointer at the given stack level. Level 0 is the
630 * currently active frame, 1 is the first enclosing level, and so
631 * on. Throws an error if the enclosing frame is invalid.
632 */
633 VM_REG_ACCESS vm_val_t *get_fp_at_level(VMG_ int level) VM_REG_CONST;
634
635 /*
636 * Get the current frame depth. This is the stack depth of the
637 * current frame pointer. This can be used to compare two frame
638 * pointers to determine if one encloses the other - the pointer
639 * with the smaller depth value encloses the larger one.
640 */
641 size_t get_frame_depth(VMG0_) const
642 { return ptr_to_index(frame_ptr_); }
643
644 /* get the current frame pointer */
645 VM_REG_ACCESS vm_val_t *get_frame_ptr() VM_REG_CONST { return frame_ptr_; }
646
647 /* given a frame pointer, get the enclosing frame pointer */
648 static vm_val_t *get_enclosing_frame_ptr(VMG_ vm_val_t *fp)
649 {
650 return (vm_val_t *)get_from_frame(fp, VMRUN_FPOFS_ENC_FP)->val.ptr;
651 }
652
653 /* get the number of arguments from a given frame */
654 VM_REG_ACCESS int get_argc_from_frame(VMG_ vm_val_t *fp) VM_REG_CONST
655 { return get_from_frame(fp, VMRUN_FPOFS_ARGC)->val.intval; }
656
657 /* get the argument counter from a given stack level */
658 VM_REG_ACCESS int get_argc_at_level(VMG_ int level) VM_REG_CONST
659 { return get_argc_from_frame(vmg_ get_fp_at_level(vmg_ level)); }
660
661 /* given a frame pointer, get the 'self' object for the frame */
662 static vm_obj_id_t get_self_from_frame(VMG_ vm_val_t *fp)
663 {
664 /* get the 'self' slot on the stack */
665 vm_val_t *self_val = get_from_frame(fp, VMRUN_FPOFS_SELF);
666
667 /* return the appropriate value */
668 return (self_val->typ == VM_NIL ? VM_INVALID_OBJ : self_val->val.obj);
669 }
670
671 /* get the 'self' object at a given stack level */
672 VM_REG_ACCESS vm_obj_id_t get_self_at_level(VMG_ int level) VM_REG_CONST
673 { return get_self_from_frame(vmg_ get_fp_at_level(vmg_ level)); }
674
675 /* given a frame pointer, get the target property for the frame */
676 static vm_prop_id_t get_target_prop_from_frame(VMG_ vm_val_t *fp)
677 {
678 vm_val_t *val;
679
680 /* get the 'self' slot on the stack */
681 val = get_from_frame(fp, VMRUN_FPOFS_PROP);
682
683 /* return the appropriate value */
684 return (val->typ == VM_NIL ? VM_INVALID_PROP : val->val.prop);
685 }
686
687 /* get the target property at a given stack level */
688 VM_REG_ACCESS vm_prop_id_t get_target_prop_at_level(
689 VMG_ int level) VM_REG_CONST
690 {
691 return get_target_prop_from_frame(vmg_ get_fp_at_level(vmg_ level));
692 }
693
694 /* given a frame pointer, get the defining object from the frame */
695 VM_REG_ACCESS vm_obj_id_t get_defining_obj_from_frame(VMG_ vm_val_t *fp)
696 VM_REG_CONST
697 {
698 /* get the defining object slot on the stack */
699 vm_val_t *val = get_from_frame(fp, VMRUN_FPOFS_DEFOBJ);
700
701 /* return the appropriate value */
702 return (val->typ != VM_OBJ ? VM_INVALID_OBJ : val->val.obj);
703 }
704
705 /* get the defining object at a given stack level */
706 VM_REG_ACCESS vm_obj_id_t get_defining_obj_at_level(VMG_ int level)
707 VM_REG_CONST
708 {
709 return get_defining_obj_from_frame(vmg_ get_fp_at_level(vmg_ level));
710 }
711
712 /* given a frame pointer, get the original target object */
713 VM_REG_ACCESS vm_obj_id_t get_orig_target_obj_from_frame(
714 VMG_ vm_val_t *fp) VM_REG_CONST
715 {
716 /* get the original target object slot on the stack */
717 vm_val_t *val = get_from_frame(fp, VMRUN_FPOFS_ORIGTARG);
718
719 /* return the appropriate value */
720 return (val->typ == VM_NIL ? VM_INVALID_OBJ : val->val.obj);
721 }
722
723 /* get the current original target object at a given stack level */
724 VM_REG_ACCESS vm_obj_id_t get_orig_target_obj_at_level(VMG_ int level)
725 VM_REG_CONST
726 {
727 return get_orig_target_obj_from_frame(
728 vmg_ get_fp_at_level(vmg_ level));
729 }
730
731 /* get the enclosing entry pointer from a given frame */
732 static const uchar *get_enclosing_entry_ptr_from_frame(VMG_ vm_val_t *fp)
733 {
734 return (const uchar *)get_from_frame(fp, VMRUN_FPOFS_ENC_EP)->val.ptr;
735 }
736
737 /* get the recursive native caller contxt from the frame */
738 static const vm_rcdesc *get_rcdesc_from_frame(VMG_ vm_val_t *fp)
739 {
740 return (vm_rcdesc *)get_from_frame(fp, VMRUN_FPOFS_RCDESC)->val.ptr;
741 }
742
743 /* get the frame reference object from a frame */
744 static vm_val_t *get_frameref_slot(VMG_ vm_val_t *fp)
745 { return get_from_frame(fp, VMRUN_FPOFS_FRAMEREF); }
746
747 /* get the invokee from a frame */
748 static vm_val_t *get_invokee_from_frame(VMG_ vm_val_t *fp)
749 { return get_from_frame(fp, VMRUN_FPOFS_INVOKEE); }
750
751 /* get the invokee of the current function */
752 VM_REG_ACCESS vm_val_t *get_invokee(VMG0_)
753 { return get_invokee_from_frame(vmg_ frame_ptr_); }
754
755 /*
756 * Get the return offset from a given frame. This is the offset of
757 * the return address from the start of the method header for the
758 * frame.
759 */
760 static ulong get_return_ofs_from_frame(VMG_ vm_val_t *fp)
761 { return get_from_frame(fp, VMRUN_FPOFS_RET)->val.ofs; }
762
763 /*
764 * Get the return address from a given frame. (The return address
765 * is the third item pushed before the enclosing frame pointer,
766 * hence it's at offset -3 from the frame pointer.) Returns zero if
767 * we were called by recursive invocation of the VM - this is not
768 * ambiguous with an actual return address of zero, since zero is
769 * never a valid code address.
770 */
771 static const uchar *get_return_addr_from_frame(VMG_ vm_val_t *fp)
772 {
773 pool_ofs_t ofs;
774
775 /* get the return method offset from the stack */
776 ofs = get_return_ofs_from_frame(vmg_ fp);
777
778 /* check for special offset values */
779 switch (ofs)
780 {
781 case VMRUN_RET_RECURSIVE:
782 /*
783 * recursive VM call - there's no byte-code return address in
784 * this case since the return takes us back to native code, so
785 * return 0 to indicate this
786 */
787 return 0;
788
789 case VMRUN_RET_OP:
790 /*
791 * return from operator overload; the true return address is in
792 * the stack slot just above the last argument
793 */
794 ofs = G_interpreter->get_param_from_frame(
795 vmg_ fp, G_interpreter->get_argc_from_frame(vmg_ fp))
796 ->val.intval;
797 break;
798
799 case VMRUN_RET_OP_ASILCL:
800 /*
801 * return from operator overload and assign local: the true
802 * return address is two slots above the last argument
803 */
804 ofs = G_interpreter->get_param_from_frame(
805 vmg_ fp, G_interpreter->get_argc_from_frame(vmg_ fp) + 1)
806 ->val.intval;
807 break;
808 }
809
810 /*
811 * add the offset to the enclosing entry pointer to yield the
812 * absolute pool address of the return point
813 */
814 return ofs + get_enclosing_entry_ptr_from_frame(vmg_ fp);
815 }
816
817 /*
818 * Given a frame pointer, set up a function pointer for the return
819 * address from the frame.
820 */
821 static void set_return_funcptr_from_frame(VMG_ class CVmFuncPtr *func_ptr,
822 vm_val_t *frame_ptr);
823
824 /* reset the machine registers to the initial conditions */
825 void reset(VMG0_);
826
827 /*
828 * Get the current "self" object. The "self" object is always the
829 * implicit first parameter to any method. Note that this version of
830 * the method *doesn't* check for nil - it assumes that the caller
831 * knows for sure that there's a valid "self", so dispenses with any
832 * checks to save time.
833 */
834 VM_REG_ACCESS vm_obj_id_t get_self(VMG0_) VM_REG_CONST
835 {
836 /* get the object value of the 'self' slot in the current frame */
837 return get_from_frame(frame_ptr_, VMRUN_FPOFS_SELF)->val.obj;
838 }
839
840 /* get the pointer to the current "self" value */
841 VM_REG_ACCESS vm_val_t *get_self_val(VMG0_) VM_REG_CONST
842 { return get_from_frame(frame_ptr_, VMRUN_FPOFS_SELF); }
843
844 /* get the self value from a frame */
845 VM_REG_ACCESS vm_val_t *get_self_val_from_frame(VMG_ vm_val_t *fp)
846 { return get_from_frame(fp, VMRUN_FPOFS_SELF); }
847
848 /*
849 * Get the current "self" object, checking for nil. If "self" is nil,
850 * we'll return VM_INVALID_OBJ. This version (not get_self()) should
851 * be used whenever it's not certain from context that there's a valid
852 * "self".
853 */
854 VM_REG_ACCESS vm_obj_id_t get_self_check(VMG0_) VM_REG_CONST
855 {
856 /* get the 'self' slot from the stack frame */
857 vm_val_t *valp = get_from_frame(frame_ptr_, VMRUN_FPOFS_SELF);
858
859 /* if it's an object, return the ID, otherwise invalid */
860 return (valp->typ == VM_OBJ ? valp->val.obj : VM_INVALID_OBJ);
861 }
862
863 /* set the current 'self' object */
864 VM_REG_ACCESS void set_self(VMG_ const vm_val_t *val)
865 {
866 /* store the given value in the 'self' slot in the current frame */
867 *get_from_frame(frame_ptr_, VMRUN_FPOFS_SELF) = *val;
868 }
869
870 /* create a method context object suitable for LOADCTX */
871 static void create_loadctx_obj(VMG_ vm_val_t *result,
872 vm_obj_id_t self,
873 vm_obj_id_t defobj,
874 vm_obj_id_t targobj,
875 vm_prop_id_t targprop);
876
877 /*
878 * Set the current execution context: the 'self' value, the target
879 * property, the original target object, and the defining object.
880 */
881 VM_REG_ACCESS void set_method_ctx(
882 VMG_ vm_obj_id_t new_self, vm_prop_id_t new_target_prop,
883 vm_obj_id_t new_target_obj, vm_obj_id_t new_defining_obj)
884 {
885 /* set the "self" slot in the current stack frame */
886 get_from_frame(frame_ptr_, VMRUN_FPOFS_SELF)->set_obj(new_self);
887
888 /* set the target property slot in the frame */
889 get_from_frame(frame_ptr_, VMRUN_FPOFS_PROP)
890 ->set_propid(new_target_prop);
891
892 /* set the original target object slot in the frame */
893 get_from_frame(frame_ptr_, VMRUN_FPOFS_ORIGTARG)
894 ->set_obj(new_target_obj);
895
896 /* set the defining object slot in the frame */
897 get_from_frame(frame_ptr_, VMRUN_FPOFS_DEFOBJ)
898 ->set_obj(new_defining_obj);
899 }
900
901 /* get the current target property value */
902 VM_REG_ACCESS vm_prop_id_t get_target_prop(VMG0_) VM_REG_CONST
903 {
904 return get_from_frame(frame_ptr_, VMRUN_FPOFS_PROP)->val.prop;
905 }
906
907 /* get the current defining object */
908 VM_REG_ACCESS vm_obj_id_t get_defining_obj(VMG0_) VM_REG_CONST
909 {
910 return get_from_frame(frame_ptr_, VMRUN_FPOFS_DEFOBJ)->get_as_obj();
911 }
912
913 /* get the current original target object */
914 VM_REG_ACCESS vm_obj_id_t get_orig_target_obj(VMG0_) VM_REG_CONST
915 {
916 return get_from_frame(frame_ptr_, VMRUN_FPOFS_ORIGTARG)->val.obj;
917 }
918
919 /* push an object ID */
920 void push_obj(VMG_ vm_obj_id_t obj)
921 { push()->set_obj(obj); }
922
923 /* push an object ID, or nil if it's an invalid object */
924 void push_obj_or_nil(VMG_ vm_obj_id_t obj)
925 {
926 if (obj == VM_INVALID_OBJ)
927 push()->set_nil();
928 else
929 push()->set_obj(obj);
930 }
931
932 /* push a property ID */
933 void push_prop(VMG_ vm_prop_id_t prop)
934 { push()->set_propid(prop); }
935
936 /* push a property ID, or nil if it's invalid */
937 void push_prop_or_nil(VMG_ vm_prop_id_t prop)
938 {
939 if (prop == VM_INVALID_PROP)
940 push()->set_nil();
941 else
942 push()->set_propid(prop);
943 }
944
945 /* push a boolean value */
946 void push_bool(VMG_ int flag)
947 { push()->set_logical(flag); }
948
949 /* push nil */
950 void push_nil(VMG0_)
951 { push()->set_nil(); }
952
953 /* push a code offset value */
954 void push_codeofs(VMG_ pool_ofs_t ofs)
955 { push()->set_codeofs(ofs); }
956
957 /* push a code pointer value */
958 void push_codeptr(VMG_ const void *p)
959 { push()->set_codeptr(p); }
960
961 /* push a stack pointer value */
962 void push_stackptr(VMG_ vm_val_t *stack_ptr)
963 { push()->set_stack((void *)stack_ptr); }
964
965 /* push an integer value */
966 void push_int(VMG_ int32_t intval)
967 { push()->set_int(intval); }
968
969 /* push an enumerator value */
970 void push_enum(VMG_ uint32_t intval)
971 { push()->set_enum(intval); }
972
973 /* push a C string value */
974 void push_string(VMG_ const char *str, size_t len);
975 void push_string(VMG_ const char *str)
976 { push_string(vmg_ str, strlen(str)); }
977
978 /* push a printf-formatted string */
979 void push_stringf(VMG_ const char *fmt, ...);
980 void push_stringvf(VMG_ const char *fmt, va_list va);
981
982 /* get the function entrypoint address */
983 VM_REG_ACCESS const uchar *get_entry_ptr() VM_REG_CONST
984 { return entry_ptr_native_; }
985
986 /* get the current program counter offset from the entry pointer */
987 VM_REG_ACCESS uint get_method_ofs() VM_REG_CONST
988 {
989 /*
990 * Return the current program counter minus the current entry
991 * pointer. If there is no current program counter, we're not
992 * executing in byte code, so there's no method offset.
993 */
994 if (pc_ptr_ != 0)
995 return *pc_ptr_ - entry_ptr_native_;
996 else
997 return 0;
998 }
999
1000 /*
1001 * Convert a pointer to the currently executing method into an offset
1002 * from the start of the current method.
1003 */
1004 VM_REG_ACCESS ulong pc_to_method_ofs(const uchar *p)
1005 {
1006 /*
1007 * get the memory address of the current entry pointer, and
1008 * subtract that from the given memory pointer to get an offset
1009 * from the start of the current method
1010 */
1011 return p - entry_ptr_native_;
1012 }
1013
1014 /*
1015 * Create an exception of the given imported class and throw it. If
1016 * the class is not exported, we'll create a basic run-time exception;
1017 * if that's not defined, we'll create an arbitrary object.
1018 *
1019 * Arguments to the exception constructor are on the stack, with the
1020 * argument count in argc. If the imported class doesn't exist, we'll
1021 * instead throw an intrinsic-class-general-error exception with the
1022 * given fallback message as explanatory text.
1023 */
1024 void throw_new_class(VMG_ vm_obj_id_t cls, uint argc,
1025 const char *fallback_msg);
1026
1027 /*
1028 * Create an exception of the given RuntimeError subclass and throw it.
1029 * If the class isn't exported, we'll create a base RuntimeError.
1030 *
1031 * Push any constructor arguments *besides* the error number onto the
1032 * stack before calling this. A RuntimeError subclass constructor will
1033 * always take an additional first argument giving the error number,
1034 * but don't push that or include it in the 'argc' value.
1035 */
1036 void throw_new_rtesub(VMG_ vm_obj_id_t cls, uint argc, int errnum);
1037
1038 /*
1039 * Save/restore the interpreter context. This is for use by the
1040 * debugger when evaluating an expression in the course of execution,
1041 * to ensure that everything is reset properly to the enclosing
1042 * execution context when it's finished.
1043 */
1044 void save_context(VMG_ vmrun_save_ctx *ctx);
1045 void restore_context(VMG_ vmrun_save_ctx *ctx);
1046
1047 /*
1048 * Get the boundaries of the given source-code statement in the given
1049 * function. Fills in the line pointer and *stm_start and *stm_end
1050 * with information on the source line containing the given offset in
1051 * the given method. Returns true if source information is
1052 * successfully located for the given machine code address, false if
1053 * not.
1054 *
1055 * If no debugging information is available for the given code
1056 * location, this function cannot get the source-code statement
1057 * bounds, and returns false.
1058 */
1059 static int get_stm_bounds(VMG_ const class CVmFuncPtr *func_ptr,
1060 ulong pc_ofs,
1061 class CVmDbgLinePtr *line_ptr,
1062 const uchar **stm_start, const uchar **stm_end);
1063
1064
1065 /* -------------------------------------------------------------------- */
1066 /*
1067 * Set the HALT VM flag. This allows the debugger to terminate the
1068 * program immediately, without allowing any more byte-code
1069 * instructions to execute.
1070 */
1071 void set_halt_vm(int f) { halt_vm_ = f; }
1072
1073 /* -------------------------------------------------------------------- */
1074 /*
1075 * Start profiling. This deletes any old profiling records and starts
1076 * a new profiling session. We'll capture profiling data until
1077 * end_profiling() is called. This function is only included in the
1078 * build if the profiler is included in the build.
1079 */
1080 void start_profiling();
1081
1082 /* end profiling */
1083 void end_profiling();
1084
1085 /*
1086 * get the profiling data - we'll invoke the callback once for each
1087 * function in our table of data
1088 */
1089 void get_profiling_data(VMG_
1090 void (*cb)(void *ctx, const char *func_name,
1091 unsigned long time_direct,
1092 unsigned long time_in_children,
1093 unsigned long call_cnt),
1094 void *cb_ctx);
1095
1096 /* get the last program counter address */
1097 VM_REG_ACCESS const uchar *get_last_pc() VM_REG_CONST
1098 { return pc_ptr_ != 0 ? *pc_ptr_ : 0; }
1099
1100 protected:
1101 /*
1102 * Execute byte code starting at a given address. This function
1103 * retains control until the byte code function invoked returns or
1104 * throws an unhandled exception.
1105 *
1106 * If an exception occurs and is not handled by the byte code, we'll
1107 * throw VMERR_UNHANDLED_EXC with the exception object as the first
1108 * parameter.
1109 */
1110 void run(VMG_ const uchar *p);
1111
1112 /*
1113 * Display a dstring via the default string display function. This
1114 * function pushes a string value (with the given constant pool
1115 * offset), then does the same work as do_call() to invoke a function
1116 * with one argument.
1117 *
1118 * The string is identified by its offset in the constant pool.
1119 */
1120 const uchar *disp_dstring(VMG_ pool_ofs_t ofs, uint caller_ofs,
1121 vm_obj_id_t self);
1122
1123 /*
1124 * Display the value at top of stack via the default string display
1125 * function. does the same work as do_call() to invoke the function
1126 * with one argument, which must already be on the stack.
1127 */
1128 const uchar *disp_string_val(VMG_ uint caller_ofs, vm_obj_id_t self);
1129
1130 /*
1131 * Set up a function header pointer for the current function
1132 */
1133 void set_current_func_ptr(VMG_ class CVmFuncPtr *func_ptr);
1134
1135 /* call a built-in function */
1136 void call_bif(VMG_ uint set_index, uint func_index, uint argc);
1137
1138 /*
1139 * Throw an exception. Returns a non-null program counter if a
1140 * handler was found, false if not. If a handler was found, byte-code
1141 * execution can proceed; if not, the byte-code execution loop must
1142 * pass the exception up to its caller.
1143 */
1144 const uchar *do_throw(VMG_ const uchar *pc, vm_obj_id_t exception_obj);
1145
1146 /*
1147 * Inherit a property - this is essentially the same as get_prop,
1148 * but rather than getting the property from the given object, this
1149 * ignores any such property defined directly by the object and goes
1150 * directly to the inherited definition. However, the "self" object
1151 * is still the same as the current "self" object, since we want to
1152 * evaluate the inherited method in the context of the original
1153 * target "self" object.
1154 */
1155 const uchar *inh_prop(VMG_ uint caller_ofs, vm_prop_id_t prop, uint argc);
1156
1157 /*
1158 * Look up a property value without evaluating it. Returns true if we
1159 * found the property, false if not.
1160 */
1161 inline static int get_prop_no_eval(VMG_ const vm_val_t **target_obj,
1162 vm_prop_id_t target_prop,
1163 uint *argc, vm_obj_id_t *srcobj,
1164 vm_val_t *val,
1165 const vm_val_t **self,
1166 vm_val_t *new_self);
1167
1168 /*
1169 * Evaluate a property value. If the value contains code, we'll
1170 * execute the code; if it contains a self-printing string, we'll
1171 * display the string; otherwise, we'll push the value onto the stack.
1172 *
1173 * 'found' indicates whether or not the property is defined by the
1174 * object. False indicates that the property is not defined, true
1175 * that it is defined. If the property isn't defined, we'll simply
1176 * discard arguments and push nil.
1177 *
1178 * If 'caller_ofs' is zero, we'll recursively invoke the interpreter
1179 * loop if it's necessary to run a method; otherwise, we'll set up at
1180 * the beginning of the method's code and let the caller proceed into
1181 * the code.
1182 */
1183 const uchar *eval_prop_val(VMG_ int found, uint caller_ofs,
1184 const vm_val_t *val, vm_obj_id_t self,
1185 vm_prop_id_t target_prop,
1186 const vm_val_t *orig_target_obj,
1187 vm_obj_id_t defining_obj,
1188 uint argc, const vm_rcdesc *rc);
1189
1190 /*
1191 * Check a property for validity in a speculative evaluation. If
1192 * evaulating the property would cause any side effects, we'll throw
1193 * an error (VMERR_BAD_SPEC_EXPR); otherwise, we won't do anything.
1194 * Side effects include displaying a dstring or invoking a method.
1195 */
1196 void check_prop_spec_eval(VMG_ vm_obj_id_t obj, vm_prop_id_t prop);
1197
1198 /*
1199 * Return from a function or method. Returns the new program counter
1200 * at which to continue execution, and restore machine registers to
1201 * the enclosing frame.
1202 *
1203 * Returns a non-null program counter if execution should proceed,
1204 * null if we're returning from the outermost stack level. When we
1205 * return null, the caller must return control to the host
1206 * environment, since the host environment called the function from
1207 * which we're returning.
1208 */
1209 const uchar *do_return(VMG0_);
1210
1211 /*
1212 * append a stack trace to the given string, returning a new string
1213 * object
1214 */
1215 vm_obj_id_t append_stack_trace(VMG_ vm_obj_id_t str_obj);
1216
1217 /*
1218 * Validate a local stack index. This verifies that an index used in a
1219 * GETSPN, GETSPX, SETSPN, or SETSPX instruction refers to the
1220 * temporary storage area for the current function.
1221 */
1222 void validate_local_stack_index(VMG_ unsigned int idx)
1223 {
1224 /* get a pointer to the current function header */
1225 CVmFuncPtr hdr_ptr;
1226 hdr_ptr.set(entry_ptr_native_);
1227
1228 /*
1229 * Check that the index is below the index of the last local
1230 * variable. The locals start just below the frame pointer.
1231 */
1232 if (idx >= get_depth_rel(frame_ptr_) - hdr_ptr.get_local_cnt())
1233 err_throw(VMERR_STACK_OVERFLOW);
1234 }
1235
1236 /* push a value onto the stack */
1237 void pushval(VMG_ const vm_val_t *val) { push(val); }
1238
1239 /* pop a value off the stack */
1240 void popval(VMG_ vm_val_t *val) { pop(val); }
1241
1242 /* add two values, leaving the result in *val1 */
1243 int compute_sum(VMG_ vm_val_t *val1, const vm_val_t *val2);
1244
1245 /* compute a sum, specialized for individual opcodes */
1246 const uchar *compute_sum_inc(VMG_ const uchar *p);
1247 const uchar *compute_sum_add(VMG_ const uchar *p);
1248 const uchar *compute_sum_lcl_imm(VMG_ vm_val_t *lclp,
1249 const vm_val_t *ival,
1250 int lclidx, const uchar *p);
1251
1252 /* subtract one value from another, leaving the result in *val1 */
1253 int compute_diff(VMG_ vm_val_t *val1, vm_val_t *val2);
1254
1255 /* compute a difference, specialized for individual opcodes */
1256 const uchar *compute_diff_dec(VMG_ const uchar *p);
1257 const uchar *compute_diff_sub(VMG_ const uchar *p);
1258
1259 /* compute the product, leaving the result in *val1 */
1260 int compute_product(VMG_ vm_val_t *val1, vm_val_t *val2);
1261
1262 /* compute the quotient val1/val2, leaving the result in *val2 */
1263 int compute_quotient(VMG_ vm_val_t *val1, vm_val_t *val2);
1264
1265 /* compute the modulo val1%val2, leaving the result in *val2 */
1266 int compute_mod(VMG_ vm_val_t *val1, vm_val_t *val2);
1267
1268 /* XOR two values and push the result */
1269 int xor_and_push(VMG_ vm_val_t *val1, vm_val_t *val2);
1270
1271 /* process a MAKELSTPAR instruction */
1272 void makelstpar(VMG0_);
1273
1274 /*
1275 * index container_val by index_val (i.e., compute
1276 * container_val[index_val]), storing the result at *result
1277 */
1278 VM_REG_ACCESS int apply_index(VMG_ vm_val_t *result,
1279 const vm_val_t *container_val,
1280 const vm_val_t *index_val);
1281
1282 /*
1283 * Set the element at index index_val in container_val to new_val,
1284 * and push the new container value. The container may be a new
1285 * object, since some types (lists, for example) cannot have their
1286 * values changed but instead create new objects when an indexed
1287 * element is modified.
1288 */
1289 VM_REG_ACCESS int set_index(VMG_ vm_val_t *container_val,
1290 const vm_val_t *index_val,
1291 const vm_val_t *new_val);
1292
1293 /*
1294 * create a new object of the given index into the metaclass
1295 * dependency table for the load image file, using the given number
1296 * of parameters; removes the parameters from the stack, and leaves
1297 * the new object reference in register R0
1298 */
1299 const uchar *new_and_store_r0(VMG_ const uchar *pc,
1300 uint metaclass_idx, uint argc,
1301 int is_transient);
1302
1303 /*
1304 * Compare the two values at top of stack for equality; returns true
1305 * if the values are equal, false if not. Removes the two values from
1306 * the stack.
1307 */
1308 int pop2_equal(VMG0_)
1309 {
1310 /* compare the values and return the result */
1311 int ret = get(1)->equals(vmg_ get(0));
1312
1313 /* discard the values */
1314 discard(2);
1315
1316 /* return the result */
1317 return ret;
1318 }
1319
1320 /*
1321 * Compare the magnitude of the two values at the top of the stack.
1322 * Returns 1 if the value at (TOS-1) is greater than the value at TOS,
1323 * -1 if (TOS-1) is less than (TOS), and 0 if the two value are equal.
1324 * Removes the two values from the stack.
1325 */
1326 int pop2_compare(VMG0_)
1327 {
1328 /* compare the values and return the result */
1329 int ret = get(1)->compare_to(vmg_ get(0));
1330
1331 /* discard the values */
1332 discard(2);
1333
1334 /* return the result */
1335 return ret;
1336 }
1337
1338 /* is TOS-1 < TOS ? */
1339 int pop2_compare_lt(VMG0_)
1340 {
1341 /* compare the values and return the result */
1342 int ret = get(1)->is_lt(vmg_ get(0));
1343
1344 /* discard the values */
1345 discard(2);
1346
1347 /* return the result */
1348 return ret;
1349 }
1350
1351 /* is TOS-1 <= TOS ? */
1352 int pop2_compare_le(VMG0_)
1353 {
1354 /* compare the values and return the result */
1355 int ret = get(1)->is_le(vmg_ get(0));
1356
1357 /* discard the values */
1358 discard(2);
1359
1360 /* return the result */
1361 return ret;
1362 }
1363
1364 /* is TOS-1 > TOS ? */
1365 int pop2_compare_gt(VMG0_)
1366 {
1367 /* compare the values and return the result */
1368 int ret = get(1)->is_gt(vmg_ get(0));
1369
1370 /* discard the values */
1371 discard(2);
1372
1373 /* return the result */
1374 return ret;
1375 }
1376
1377 /* is TOS-1 >= TOS ? */
1378 int pop2_compare_ge(VMG0_)
1379 {
1380 /* compare the values and return the result */
1381 int ret = get(1)->is_ge(vmg_ get(0));
1382
1383 /* discard the values */
1384 discard(2);
1385
1386 /* return the result */
1387 return ret;
1388 }
1389
1390 /* given a constant pool offset, get a pointer to the constant data */
1391 static const char *get_const_ptr(VMG_ pool_ofs_t ofs)
1392 { return G_const_pool->get_ptr(ofs); }
1393
1394 /*
1395 * get a signed 16-bit byte-code operand, incrementing the
1396 * instruction pointer past the operand
1397 */
1398 int16_t get_op_int16(const uchar **p)
1399 {
1400 int16_t ret = (int16_t)osrp2s(*p);
1401 *p += 2;
1402 return ret;
1403 }
1404
1405 /* get an unsigned 16-bit byte-code operand */
1406 uint16_t get_op_uint16(const uchar **p)
1407 {
1408 uint16_t ret = (uint16_t)osrp2(*p);
1409 *p += 2;
1410 return ret;
1411 }
1412
1413 /* get a signed 32-bit byte-code operand */
1414 int32_t get_op_int32(const uchar **p)
1415 {
1416 int32_t ret = (int32_t)osrp4s(*p);
1417 *p += 4;
1418 return ret;
1419 }
1420
1421 /* get an unsigned 32-bit byte-code operand */
1422 uint32_t get_op_uint32(const uchar **p)
1423 {
1424 uint32_t ret = (uint32_t)t3rp4u(*p);
1425 *p += 4;
1426 return ret;
1427 }
1428
1429 /* get a signed 8-bit byte-code operand */
1430 int get_op_int8(const uchar **p)
1431 {
1432 int ret = (int)(signed char)**p;
1433 ++(*p);
1434 return ret;
1435 }
1436
1437 /* get an unsigned 8-bit byte-code operand */
1438 uint get_op_uint8(const uchar **p)
1439 {
1440 uint ret = (uint)**p;
1441 ++(*p);
1442 return ret;
1443 }
1444
1445 /* record a function or method entry in the profiler data */
1446 void prof_enter(VMG_ const uchar *fptr);
1447
1448 /* record a function or method exit in the profiler data */
1449 void prof_leave();
1450
1451 /* find or create a function entry in the master profiler table */
1452 class CVmHashEntryProfiler
1453 *prof_find_master_rec(const struct vm_profiler_rec *p);
1454
1455 /* calculate an elapsed time */
1456 void prof_calc_elapsed(vm_prof_time *diff, const vm_prof_time *a,
1457 const vm_prof_time *b);
1458
1459 /* add an elapsed time value to a cumulative elapsed time value */
1460 void prof_add_elapsed(vm_prof_time *sum, const vm_prof_time *val);
1461
1462 /* hash table enumeration callback for dumping profiler data */
1463 static void prof_enum_cb(void *ctx0, class CVmHashEntry *entry0);
1464
1465 /* validate the built-in function pointer at top of stack */
1466 void validate_bifptr(VMG0_);
1467
1468 /*
1469 * Function header size - obtained from the image file upon loading
1470 */
1471 size_t funchdr_size_;
1472
1473 /*
1474 * A pointer to a global variable in the object table (CVmObjTable)
1475 * containing the function to invoke for the SAY and SAYVAL opcodes.
1476 * This can be a function pointer, a function object, or nil. If this
1477 * is nil, the SAY opcode will throw an error.
1478 */
1479 struct vm_globalvar_t *say_func_;
1480
1481 /*
1482 * The method to invoke for the SAY and SAYVAL opcodes when a valid
1483 * "self" object is available. If no method is defined, this will
1484 * be set to VM_INVALID_PROP.
1485 */
1486 vm_prop_id_t say_method_;
1487
1488 /*
1489 * R0 - data register 0. This register stores function return
1490 * values.
1491 */
1492 VM_IF_REGS_IN_STRUCT(vm_val_t r0_;)
1493
1494
1495 /*
1496 * native entry pointer value - this is simply the translated value of
1497 * the entry pointer (i.e., G_code_pool->get_ptr(entry_ptr_))
1498 */
1499 VM_IF_REGS_IN_STRUCT(const uchar *entry_ptr_native_;)
1500
1501 /*
1502 * FP - frame pointer register. This points to the base of the
1503 * current stack activation frame. Local variables and parameters
1504 * are reachable relative to this register.
1505 */
1506 VM_IF_REGS_IN_STRUCT(vm_val_t *frame_ptr_;)
1507
1508 /*
1509 * Pointer to program counter - we use this in the debugger to create
1510 * pseudo-stack frames for system code when we recursively invoke the
1511 * VM, and for finding the current PC from intrinsic function code.
1512 */
1513 VM_IF_REGS_IN_STRUCT(const uchar **pc_ptr_;)
1514
1515 /*
1516 * Flag: VM is halting. This is used by the debugger to force the
1517 * program to stop executing. This is not used except with the
1518 * debug-mode interpreter.
1519 */
1520 int halt_vm_;
1521
1522 /* flag: profiling is active */
1523 int profiling_;
1524
1525 /* in case we have a profiler, include the profiler stack */
1526 struct vm_profiler_rec *prof_stack_;
1527 size_t prof_stack_max_;
1528
1529 /* next available index in the profiler stack */
1530 size_t prof_stack_idx_;
1531
1532 /*
1533 * Start of execution in the currently active function, since the last
1534 * call or return. This uses the OS-specific high-precision timer
1535 * (defined by os_prof_curtime() in vmprof.h).
1536 *
1537 * Each time we call a function or return from a function, we measure
1538 * the delta from this value, then add that in to the cumulative time
1539 * for the function.
1540 */
1541 vm_prof_time prof_start_;
1542
1543 /*
1544 * profiler master hash table - this is a table with one entry for
1545 * every function and method called since we began profiling, keyed by
1546 * method or function ID (the object.property for a method, or the
1547 * entrypoint code offset for a function)
1548 */
1549 class CVmHashTable *prof_master_table_;
1550 };
1551
1552 #endif /* VMRUN_H */
1553