-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
60 lines (49 loc) · 1.8 KB
/
vm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package idris_go_rts
import . "reflect"
//-------------------------------------------------------------------------------------------------
// Data structures/types
//-------------------------------------------------------------------------------------------------
type VirtualMachine struct {
ValueStack []interface{}
ValueStackTop uintptr
ValueStackBase uintptr
ReturnValue interface{}
CallStack []CallPair
}
type vmFunction func(vm *VirtualMachine, oldbase uintptr)
type CallPair struct {
fn vmFunction
base uintptr
}
//-------------------------------------------------------------------------------------------------
// Virtual machine functions
//-------------------------------------------------------------------------------------------------
func Slide(vm *VirtualMachine, num_args uintptr) {
for i := uintptr(0); i < num_args; i++ {
(*vm).ValueStack[(*vm).ValueStackBase + i] = (*vm).ValueStack[(*vm).ValueStackTop + i]
}
}
func Project(vm *VirtualMachine, value interface{}, loc uintptr, arity uintptr) {
args := ValueOf(value).Interface().(Con).args
for i := uintptr(0); i < arity; i++ {
(*vm).ValueStack[(*vm).ValueStackBase + i + loc] = args[i]
}
}
func Reserve(vm *VirtualMachine, size uintptr) {
for i := uintptr(len((*vm).ValueStack)); i < size + 2; i++ { // TODO: why +2 now?
(*vm).ValueStack = append((*vm).ValueStack, nil)
}
}
func Call(vm *VirtualMachine, fn vmFunction, base uintptr) {
fn(vm, base)
for length := len((*vm).CallStack); length > 0; length = len((*vm).CallStack) {
top := (*vm).CallStack[length - 1]
function := top.fn
base := top.base
(*vm).CallStack = (*vm).CallStack[:length-1]
function(vm, base)
}
}
func TailCall(vm *VirtualMachine, fn vmFunction, base uintptr) {
(*vm).CallStack = append((*vm).CallStack, CallPair{fn, base})
}