1 /******************************************************
8 * Copyright (c) 2006, 2007 Charles R. Childers
10 * Permission to use, copy, modify, and distribute this
11 * software for any purpose with or without fee is hereby
12 * granted, provided that the above copyright notice and
13 * this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR
16 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
19 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
21 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
22 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 ******************************************************/
33 /******************************************************
36 *|F| Pointer into the current heap
39 *|F| The instruction pointer
41 *|F| VM_STACK data, address, alternate
42 *|F| The data, address, and return stacks
44 ******************************************************/
46 VM_STACK data
, address
, alternate
;
49 /******************************************************
51 *|F| Prepare the virtual machine. This sets up the
52 *|F| stacks, and any other things that need to be
55 ******************************************************/
58 data
.stack
= gc_alloc(MAX_DATA_STACK
, sizeof(long), GC_KEEP
);
59 address
.stack
= gc_alloc(MAX_DATA_STACK
, sizeof(long), GC_KEEP
);
60 alternate
.stack
= gc_alloc(MAX_DATA_STACK
, sizeof(long), GC_KEEP
);
68 /******************************************************
70 *|F| Run through a list of instructions
74 ******************************************************/
75 void vm_run(Inst prog
[])
86 /******************************************************
88 *|F| Check for over/underflow and reset if detected
89 *|F| If the return stack over/underflows, exit Toka
91 ******************************************************/
94 if (data
.sp
< 0 || data
.sp
> MAX_DATA_STACK
)
96 if ((address
.sp
< 0 || address
.sp
> MAX_RETURN_STACK
) || (alternate
.sp
< 0 || alternate
.sp
> MAX_DATA_STACK
))
101 /******************************************************
103 *|F| Push a number to the stack.
105 ******************************************************/
112 /******************************************************
114 *|F| Push the value in the following memory location
117 ******************************************************/
120 vm_push((long)*ip
++);
124 /******************************************************
126 *|F| Push the value in the following memory location
129 ******************************************************/
132 vm_push((long)*ip
++);
136 /******************************************************
138 *|F| Push the pointer in the following memory location
139 *|F| to the stack. This is a helper function for
142 ******************************************************/
145 vm_push((long)*ip
++);
149 /******************************************************
151 *|F| Invoke the primitive id # specified in the
153 ******************************************************/