-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.odin
More file actions
163 lines (149 loc) · 3.42 KB
/
Copy pathvm.odin
File metadata and controls
163 lines (149 loc) · 3.42 KB
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
package compiler
import "core:fmt"
import "core:slice"
import "core:log"
//bytecode info
Block :: struct {
code: []u8,
data: []u8
}
//vm runtime info
VM :: struct {
block: ^Block,
ip: int,
base_pointer: int, //the base of the current procedure
stack: []i32,
stack_top: int
}
create_vm_from_block :: proc(block: ^Block) -> ^VM{
vm, _ := new(VM)
vm.stack = make([]i32, 250)
vm.block = block
return vm
}
execute :: proc(vm: ^VM){
for vm.ip < len(vm.block.code) {
//todo(fbr): only print while debugging -- add comp time if
//fmt.println("----------------")
//print_instruction(vm.block.code, vm.ip)
//defer print_stack(vm)
switch OpCode(read_byte(vm)){
case .Push:
push(vm, read_i32(vm))
case .Exit:
return
case .Add:
push(vm, pop(vm) + pop(vm))
case .Subtract:
second := pop(vm)
first := pop(vm)
push(vm, first - second)
case .Divide:
second := pop(vm)
first := pop(vm)
push(vm, first / second)
case .Multiply:
push(vm, pop(vm) * pop(vm))
case .Call:
new_ip := int(read_i32(vm)) //jump
push(vm, i32(vm.ip)) //push return address
push(vm, i32(vm.base_pointer)) //push current base pointer
vm.ip = new_ip
vm.base_pointer = vm.stack_top
case .Return:
vm.stack_top = vm.base_pointer //pop off all locals
vm.base_pointer = int(pop(vm)) //restore base pointer
vm.ip = int(pop(vm)) //jump to return address
case .Return_Value:
return_value := pop(vm)
vm.stack_top = vm.base_pointer //pop off all locals
vm.base_pointer = int(pop(vm)) //restore base pointer
vm.ip = int(pop(vm)) //jump to return address
push(vm, return_value)
case .Pop:
pop(vm)
case .Set_Local:
slot := read_i16(vm)
vm.stack[vm.base_pointer + int(slot)] = pop(vm)
case .Get_Local:
slot := read_i16(vm)
push(vm, vm.stack[vm.base_pointer + int(slot)])
case .Swap:
location_to_swap := vm.stack_top + int(read_i16(vm))
temp := pop(vm)
push(vm, vm.stack[location_to_swap])
vm.stack[location_to_swap] = temp
case .Print:
parameter := pop(vm)
log.logf(.Info, "%v\n", parameter)
case .Jump_If_False:
jumpOffset := read_i32(vm)
condition := pop(vm)
if condition == 0 {
vm.ip += int(jumpOffset)
}
case .Jump:
vm.ip += int(read_i32(vm))
case .Equal:
if pop(vm) == pop(vm){
push(vm, 1)
}else{
push(vm, 0)
}
case .Not_Equal:
if pop(vm) != pop(vm){
push(vm, 1)
}else{
push(vm, 0)
}
case .Greater_Than:
if pop(vm) > pop(vm){
push(vm, 1)
}else{
push(vm, 0)
}
case .Lesser_Than:
if pop(vm) < pop(vm){
push(vm, 1)
}else{
push(vm, 0)
}
case .Greater_Equal:
if pop(vm) >= pop(vm){
push(vm, 1)
}else{
push(vm, 0)
}
case .Lesser_Equal:
if pop(vm) <= pop(vm){
push(vm, 1)
}else{
push(vm, 0)
}
}
}
}
push :: #force_inline proc (vm: ^VM, value: i32){
vm.stack[vm.stack_top] = value;
vm.stack_top += 1
}
pop :: #force_inline proc (vm: ^VM) -> i32 {
val := vm.stack[vm.stack_top-1]
vm.stack_top -= 1
return val
}
read_byte :: #force_inline proc(vm: ^VM) -> u8 {
val := vm.block.code[vm.ip]
vm.ip+=1
return val
}
read_i16 :: #force_inline proc(vm: ^VM) -> i16 {
val := (transmute(^i16)&vm.block.code[vm.ip])^
vm.ip+=2
return val
}
read_i32 :: #force_inline proc(vm: ^VM) -> i32 {
val := (transmute(^i32)&vm.block.code[vm.ip])^
vm.ip+=4
return val
}