-
Notifications
You must be signed in to change notification settings - Fork 4
/
foovm.go
196 lines (190 loc) · 4.26 KB
/
foovm.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package foovm
import (
"fmt"
)
// Instructions
const (
Nil byte = iota // 0x00
Push byte = iota // 0x01
Pop byte = iota // 0x02
Load byte = iota // 0x03
Store byte = iota // 0x04
Add byte = iota // 0x05
Sub byte = iota // 0x06
Mul byte = iota // 0x07
Div byte = iota // 0x08
Call byte = iota // 0x09
Ret byte = iota // 0x0a
Jmp byte = iota // 0x0b
Cmp byte = iota // 0x0c
Jeq byte = iota // 0x0d
Jne byte = iota // 0x0e
Jgt byte = iota // 0x0f
Jlt byte = iota // 0x10
Jle byte = iota // 0x11
Jge byte = iota // 0x12
)
type FooVM struct {
Cmp1 byte
Cmp2 byte
// Ret Array
Rets []uint16
// 256 B Stack
RSP uint16
Stack []byte
// 64 KB Heap
RHP uint16
Heap []byte
}
func New() *FooVM {
fvm := new(FooVM)
fvm.Rets = make([]uint16, 0)
fvm.Stack = make([]byte, 256)
fvm.Heap = make([]byte, 256*256)
return fvm
}
func PrintDebug(fvm *FooVM) {
fmt.Println("\n\nDebug:")
fmt.Printf("RSP=%#x, RHP=%#x", fvm.RSP, fvm.RHP)
fmt.Println("\n\nStack:")
for i, v := range fvm.Stack {
if i%16 == 0 && i != 0 {
fmt.Println("")
}
fmt.Printf("%#x ", v)
}
fmt.Println("\n\nHeap:")
for i, v := range fvm.Heap {
if i%32 == 0 && i != 0 {
fmt.Println("")
}
if i%256 == 0 && i != 0 {
fmt.Println("-----")
}
if i == 8*256 {
break
}
fmt.Printf("%#x ", v)
}
return
}
// Execute bytecode
func (fvm *FooVM) Exec() {
for {
switch inst := fvm.Heap[fvm.RHP]; {
case inst == Nil:
fvm.RHP++
break
case inst == Push:
fvm.Stack[fvm.RSP] = fvm.Heap[fvm.RHP+1]
fvm.RSP++
fvm.RHP += 2
break
case inst == Pop:
fvm.RSP--
fvm.RHP++
break
case inst == Load:
fvm.Stack[fvm.RSP] = fvm.Heap[uint16(fvm.Stack[fvm.RSP-1])*256+uint16(fvm.Stack[fvm.RSP-2])]
fvm.RSP -= 2
fvm.RHP++
break
case inst == Store:
fvm.Heap[uint16(fvm.Stack[fvm.RSP-1])*256+uint16(fvm.Stack[fvm.RSP-2])] = fvm.Stack[fvm.RSP-3]
fvm.RSP -= 3
fvm.RHP++
break
case inst == Add:
fvm.Stack[fvm.RSP-2] = fvm.Stack[fvm.RSP-1] + fvm.Stack[fvm.RSP-2]
fvm.RSP--
fvm.RHP++
break
case inst == Sub:
fvm.Stack[fvm.RSP-2] = fvm.Stack[fvm.RSP-1] - fvm.Stack[fvm.RSP-2]
fvm.RSP--
fvm.RHP++
break
case inst == Mul:
fvm.Stack[fvm.RSP-2] = fvm.Stack[fvm.RSP-1] * fvm.Stack[fvm.RSP-2]
fvm.RSP--
fvm.RHP++
break
case inst == Div:
fvm.Stack[fvm.RSP-2] = fvm.Stack[fvm.RSP-1] / fvm.Stack[fvm.RSP-2]
fvm.RSP--
fvm.RHP++
break
case inst == Call:
fvm.Rets = append(fvm.Rets, fvm.RHP)
if fvm.Stack[fvm.RSP-1] == 0xff {
if fvm.Stack[fvm.RSP-2] == 0x00 { // Print
temp := uint16(fvm.Stack[fvm.RSP-4])*256 + uint16(fvm.Stack[fvm.RSP-5])
for _, v := range fvm.Heap[temp : temp+uint16(fvm.Stack[fvm.RSP-3])] {
fmt.Printf("%c", v)
}
fvm.RSP -= 5
fvm.RHP++
} else if fvm.Stack[fvm.RSP-2] == 0x01 { // Exit
return
}
} else {
fvm.RHP = uint16(fvm.Stack[fvm.RSP-1])*256 + uint16(fvm.Stack[fvm.RSP-2])
fvm.RSP -= 2
}
break
case inst == Ret:
fvm.RHP = fvm.Rets[len(fvm.Rets)-1]
fvm.Rets = fvm.Rets[0:len(fvm.Rets)-1]
fvm.RHP++
break
case inst == Jmp:
fvm.RHP = uint16(fvm.Stack[fvm.RSP-1])*256 + uint16(fvm.Stack[fvm.RSP-2])
fvm.RSP -= 2
break
case inst == Cmp:
fvm.Cmp1 = fvm.Stack[fvm.RSP-1]
fvm.Cmp2 = fvm.Stack[fvm.RSP-2]
fvm.RSP -= 2
fvm.RHP++
break
case inst == Jeq:
if fvm.Cmp1 == fvm.Cmp2 {
fvm.RHP = uint16(fvm.Stack[fvm.RSP]-1)*256 + uint16(fvm.Stack[fvm.RSP-2])
}
fvm.RSP -= 2
break
case inst == Jne:
if fvm.Cmp1 != fvm.Cmp2 {
fvm.RHP = uint16(fvm.Stack[fvm.RSP]-1)*256 + uint16(fvm.Stack[fvm.RSP-2])
}
fvm.RSP -= 2
break
case inst == Jgt:
if fvm.Cmp1 > fvm.Cmp2 {
fvm.RHP = uint16(fvm.Stack[fvm.RSP]-1)*256 + uint16(fvm.Stack[fvm.RSP-2])
}
fvm.RSP -= 2
break
case inst == Jlt:
if fvm.Cmp1 < fvm.Cmp2 {
fvm.RHP = uint16(fvm.Stack[fvm.RSP]-1)*256 + uint16(fvm.Stack[fvm.RSP-2])
}
fvm.RSP -= 2
break
case inst == Jle:
if fvm.Cmp1 <= fvm.Cmp2 {
fvm.RHP = uint16(fvm.Stack[fvm.RSP]-1)*256 + uint16(fvm.Stack[fvm.RSP-2])
}
fvm.RSP -= 2
break
case inst == Jge:
if fvm.Cmp1 >= fvm.Cmp2 {
fvm.RHP = uint16(fvm.Stack[fvm.RSP]-1)*256 + uint16(fvm.Stack[fvm.RSP-2])
}
fvm.RSP -= 2
break
default:
return
}
}
}