This repository has been archived by the owner on Aug 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vm.go
142 lines (132 loc) · 2.51 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
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
package vm
import (
"bufio"
"io"
"os"
"strconv"
)
const _PBUF_CAP = 10
// The VM, with its program and memory abstractions.
type VM struct {
p *program
m *memory
b *bufio.Writer
pbuf []byte
}
// Create a new VM.
func New() *VM {
return NewWithWriter(os.Stdout)
}
// Create a new VM with the specified output stream.
func NewWithWriter(w io.Writer) *VM {
return &VM{newProgram(), newMemory(), bufio.NewWriter(w), make([]byte, 0, _PBUF_CAP)}
}
// Run executes the vm bytecode read by the reader.
func (vm *VM) Run(r io.Reader) {
var i int32
// Parse the content to execute.
vm.parse(r)
// Execution loop.
defer vm.b.Flush()
for i = vm.p.start; vm.p.instrs[i] != _OP_END; i++ {
vm.runInstruction(&i)
}
}
// Run a single instruction.
func (vm *VM) runInstruction(instrIndex *int32) {
a0, a1 := vm.p.args[((*instrIndex)*2)], vm.p.args[((*instrIndex)*2)+1]
switch vm.p.instrs[*instrIndex] {
case _OP_NOP:
// Nothing
case _OP_INT:
// Not implemented
case _OP_MOV:
*a0 = *a1
case _OP_PUSH:
vm.m.pushStack(*a0)
case _OP_POP:
vm.m.popStack(a0)
case _OP_PUSHF:
vm.m.pushStack(vm.m.FLAGS)
case _OP_POPF:
vm.m.popStack(a0)
case _OP_INC:
(*a0)++
case _OP_DEC:
(*a0)--
case _OP_ADD:
*a0 += *a1
case _OP_SUB:
*a0 -= *a1
case _OP_MUL:
*a0 *= *a1
case _OP_DIV:
*a0 /= *a1
case _OP_MOD:
vm.m.remainder = *a0 % *a1
case _OP_REM:
*a0 = vm.m.remainder
case _OP_NOT:
*a0 = ^(*a0)
case _OP_XOR:
*a0 ^= *a1
case _OP_OR:
*a0 |= *a1
case _OP_AND:
*a0 &= *a1
case _OP_SHL:
// cannot shift on signed int32
if *a1 > 0 {
*a0 <<= uint(*a1)
}
case _OP_SHR:
// cannot shift on signed int32
if *a1 > 0 {
*a0 >>= uint(*a1)
}
case _OP_CMP:
if *a0 == *a1 {
vm.m.FLAGS = 0x1
} else if *a0 > *a1 {
vm.m.FLAGS = 0x2
} else {
vm.m.FLAGS = 0x0
}
case _OP_CALL:
vm.m.pushStack(*instrIndex)
fallthrough
case _OP_JMP:
*instrIndex = *a0 - 1
case _OP_RET:
vm.m.popStack(instrIndex)
case _OP_JE:
if vm.m.FLAGS&0x1 != 0 {
*instrIndex = *a0 - 1
}
case _OP_JNE:
if vm.m.FLAGS&0x1 == 0 {
*instrIndex = *a0 - 1
}
case _OP_JG:
if vm.m.FLAGS&0x2 != 0 {
*instrIndex = *a0 - 1
}
case _OP_JGE:
if vm.m.FLAGS&0x3 != 0 {
*instrIndex = *a0 - 1
}
case _OP_JL:
if vm.m.FLAGS&0x3 == 0 {
*instrIndex = *a0 - 1
}
case _OP_JLE:
if vm.m.FLAGS&0x2 == 0 {
*instrIndex = *a0 - 1
}
case _OP_PRN:
vm.pbuf = vm.pbuf[:0] // Clear buffer
vm.pbuf = strconv.AppendInt(vm.pbuf, int64(*a0), 10)
vm.pbuf = append(vm.pbuf, '\n')
vm.b.Write(vm.pbuf)
}
}