-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.rb
executable file
·338 lines (278 loc) · 7.68 KB
/
vm.rb
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env ruby
require_relative "bc"
require_relative "m"
require_relative "messages"
require_relative "rf"
class VirtualMachine
include Inspector
attr_accessor :instructions, :interpreting, :running,
:max_stack_depth, :memory
attr_reader :stack_ptr, :isp, :cycles, :max_stack_depth, :stack
def initialize()
@max_stack_depth = 100
@instructions = Array.new
@memory = Memory.new
@returns = Array.new(size=100, default=0)
@cycles = 0
@stack = Array.new(size=100, default=0)
@isp = 0
@debug = false
@interactive = false
@interpreting = false # i.e. interpreting
@running = true # main program operation
@stack_ptr = -1 # "last element"
end
def halt_vm()
@running = false
if @debug then puts "Halting vardi..." end
exit(0)
end
def start_vm()
@running = true
program_loop
end
def start_interpreter()
@interpreting = true
program_loop
end
def halt_interpreter()
@interpreting = false
end
def set_stack_ptr val
@stack_ptr = val unless val == @max_stack_depth
end
def pretty_print_instructions
puts VMInfo::PP_INSTRUCTIONS + @instructions.to_s
end
def step
@isp +=1
end
def step_back
@isp -= 1 unless @isp == 0
end
def fetch()
if @isp == @instructions.length then return -1 end
val = @instructions[@isp]
step()
return val
end
def peek_instr
if @isp == @instructions.length then return -1 end
val = @instructions[@isp]
return val
end
def i_to_hex i
return i.to_s(16).upcase
end
def bytecode_exit
if @debug then debug_bytecode_exit end
halt_interpreter()
halt_vm() unless @interactive
end
def interpret()
@cycles += 1
#fetch
instr = fetch()
#decode
case instr
when -1
puts "Overran!"
bytecode_exit
when Bytecode::NOP
if @debug then debug_bytecode_nop end
when Bytecode::PEEK
if @debug then debug_bytecode_peek end
puts @stack.last.to_s
when Bytecode::PRINT
if @debug then debug_bytecode_print end
print @stack.pop.to_s
when Bytecode::CONS
a = fetch()
@stack.push(a)
if @debug then debug_bytecode_cons end
#if @debug then puts "\tCONS.#{@stack.last.to_s}" end
when Bytecode::POP
if @debug then debug_bytecode_pop end
@stack.pop()
when Bytecode::JMP
if @debug then debug_bytecode_jmp end
addr = fetch()
@isp = addr
when Bytecode::JMS
if @debug then debug_bytecode_jms end
upper = @stack.pop().to_s(16)
lower = @stack.pop().to_s(16)
addr = (upper + lower).to_i(16)
@isp = addr
when Bytecode::JIZ
if @debug then debug_bytecode_jiz end
v = @stack.last()
if v == 0
addr = fetch()
@isp = addr
if @debug then puts "TRUE" end
elsif
fetch() #skip jmp marker
if @debug then puts "FALSE" end
end
when Bytecode::TEXT
if @debug then debug_bytecode_text end
print @stack.pop.chr
$stdout.flush()
when Bytecode::AND
if @debug then debug_bytecode_and end
#pop, AND, push
a = @stack.pop()
b = @stack.pop()
c = a & b
@stack.push(c)
when Bytecode::NOT
if @debug then debug_bytecode_not end
#pop, NOT, push
a = @stack.pop()
c = ~a
@stack.push(c)
when Bytecode::SWAP
if @debug then debug_bytecode_swap end
last = @stack.pop()
older = @stack.pop()
@stack.push(last).push(older)
when Bytecode::DUP
if @debug then debug_bytecode_dup end
@stack.push(@stack.last)
when Bytecode::RSWP
if @debug then debug_bytecode_rswp end
addr = @returns.pop()
val = @stack.pop()
@returns.push(val)
@stack.push(addr)
when Bytecode::ISWP
# if @debug then puts "\tISWP.*0x#{@instructions[@isp].to_s(16)}<-0x#{@stack.last.to_s(16)}" end
if @debug then debug_bytecode_iswp end
instr = @instructions[@isp]
val = @stack.pop()
@stack.push(instr)
@instructions[@isp] = val
when Bytecode::DEC
if @debug then debug_bytecode_dec end
a = @stack.pop()
@stack.push(a-1)
when Bytecode::ADD
if @debug then debug_bytecode_add end
a = @stack.pop()
b = @stack.pop()
@stack.push(b+a)
when Bytecode::SUB
if @debug then debug_bytecode_sub end
a = @stack.pop()
b = @stack.pop()
@stack.push(b-a)
if @debug then puts "RES: #{@stack.last()}"end
when Bytecode::MUL
if @debug then debug_bytecode_mul end
a = @stack.pop()
b = @stack.pop()
@stack.push(b*a)
when Bytecode::DIV
if @debug then debug_bytecode_div end
a = @stack.pop()
b = @stack.pop()
@stack.push(b/a)
when Bytecode::CRAY
#TODO: Malbolge crazy operator
if @debug then debug_bytecode_cray end
when Bytecode::JVM
#if @debug then puts "\t***COFFEETIME***" end
if @debug then debug_bytecode_jvm end
duration = fetch()
sleep(duration)
when Bytecode::EXIT
if @debug then debug_bytecode_exit end
bytecode_exit
when Bytecode::RET
if @debug then debug_bytecode_ret end
# if @debug then puts "\tRET.0x#{@returns.last.to_s(16)}" end
addr = @returns.pop()
@isp = addr
when Bytecode::CALL
# if @debug then puts "\tCALL.*0x#{@instructions[@isp].to_s(16)}<-0x#{(@isp-1).to_s(16)}" end
if @debug then debug_bytecode_call end
addr = fetch()
current = @isp
@isp = addr
@returns.push(current)
when Bytecode::CLS
if @debug then puts "\tCLS.*0x#{@stack.last.to_s(16)}<-0x#{(@isp-1).to_s(16)}" end
@returns.push(@isp)
addr = @stack.pop()
@isp = addr
when Bytecode::LOAD
if @debug then puts "\tLOAD" end
upper = @stack.pop().to_s(16)
lower = @stack.pop().to_s(16)
addr = (upper + lower).to_i(16)
value = @stack.pop()
if @debug then puts "ADDR: *0x#{addr.to_s(16).upcase}\nDATA: #{value}" end
@memory.load(addr,value)
when Bytecode::FETCH
if @debug then puts "\tFETCH" end
upper = @stack.pop().to_s(16)
lower = @stack.pop().to_s(16)
addr = (upper + lower).to_i(16)
value = @memory.fetch(addr)
if @debug then puts "ADDR: *0x#{addr.to_s(16).upcase}\nDATA: #{value}" end
@stack.push(value)
else
#curse programmer in hex
puts VMInfo::MAL_INSTR
puts "#{@isp} -> [#{i_to_hex ( @instructions[@isp] ) }]."
@running = false
end
if (@isp == @instructions.length) then halt_interpreter() end
# Let us exit even if programmer forgot to 0x1111
#if @instructions.length <= @isp then @running = false end
end
def set_flags
if ARGV.include?("-d")
@debug = true
end
if ARGV.include?("-i")
@interactive = true
end
if ARGV.include?("-h")
halt_interpreter()
end
end
def program_loop
while @running
if @interactive
handle_input
end
if @interpreting
interpret()
end
end
end
def load_file
file_name = ARGV[0].to_s
raise RuntimeError.new("No file was given.") unless File.exist?(file_name) && File.readable?(file_name)
file = IO.read(file_name)
file.each_byte do |byte|
@instructions << byte.to_i
end
end
def print_stats()
puts "Program length: #{@instructions.length} bytes. Executed #{@cycles} cycles."
end
def start
set_flags
load_file
if @debug
pretty_print_instructions()
end
start_interpreter()
if @debug
print_stats()
end
end
end