2018-04-19 18:33:40 +02:00
|
|
|
require "util/eventable"
|
|
|
|
require "risc/interpreter"
|
|
|
|
require_relative "compiling"
|
|
|
|
|
2017-04-10 15:12:15 +02:00
|
|
|
module Risc
|
2018-04-19 18:33:40 +02:00
|
|
|
module Ticker
|
|
|
|
include CompilerHelper
|
|
|
|
|
|
|
|
def setup
|
|
|
|
Risc.machine.boot
|
|
|
|
do_clean_compile
|
|
|
|
Vool::VoolCompiler.ruby_to_vool( @string_input )
|
|
|
|
Collector.collect_space
|
|
|
|
@interpreter = Interpreter.new
|
|
|
|
@interpreter.start Risc.machine.risc_init
|
|
|
|
end
|
|
|
|
|
|
|
|
# must be after boot, but before main compile, to define method
|
|
|
|
def do_clean_compile
|
|
|
|
end
|
2017-04-10 15:12:15 +02:00
|
|
|
|
2018-04-04 19:05:09 +02:00
|
|
|
# check the given array of instructions is what the interpreter actually does
|
|
|
|
# possible second argument ignores the given amount, usually up until main
|
|
|
|
def check_chain( should , start_at = 0 )
|
|
|
|
ticks( start_at ) if start_at > 0
|
2017-04-10 15:12:15 +02:00
|
|
|
should.each_with_index do |name , index|
|
|
|
|
got = ticks(1)
|
2018-04-04 19:05:09 +02:00
|
|
|
assert_equal got.class ,name , "Wrong class for #{index + 1 - start_at}, expect #{name} , got #{got}"
|
2017-04-10 15:12:15 +02:00
|
|
|
end
|
|
|
|
end
|
2018-04-04 19:05:09 +02:00
|
|
|
# check the main only, ignoring the __init instructions
|
|
|
|
def check_main_chain( should )
|
|
|
|
check_chain( should , main_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
# how many instruction up until the main starts, ie
|
|
|
|
# ticks(main_at) will be the label for main
|
|
|
|
def main_at
|
2018-04-07 17:58:44 +02:00
|
|
|
26
|
2018-04-04 19:05:09 +02:00
|
|
|
end
|
2017-04-10 15:12:15 +02:00
|
|
|
|
2018-03-23 19:02:17 +01:00
|
|
|
def get_return
|
2017-04-10 15:12:15 +02:00
|
|
|
assert_equal Parfait::Message , @interpreter.get_register(:r0).class
|
2018-03-23 19:02:17 +01:00
|
|
|
@interpreter.get_register(:r0).return_value
|
2017-04-10 15:12:15 +02:00
|
|
|
end
|
|
|
|
|
2018-04-04 19:05:09 +02:00
|
|
|
# do as many as given ticks in the main, ie main_at more
|
|
|
|
def main_ticks(num)
|
|
|
|
ticks( main_at + num)
|
|
|
|
end
|
|
|
|
|
|
|
|
# do a certain number of steps in the interpreter and return the last executed instruction
|
2018-03-23 17:55:23 +01:00
|
|
|
def ticks( num )
|
2017-04-10 15:12:15 +02:00
|
|
|
last = nil
|
|
|
|
num.times do
|
|
|
|
last = @interpreter.instruction
|
|
|
|
@interpreter.tick
|
|
|
|
end
|
|
|
|
return last
|
|
|
|
end
|
|
|
|
|
2018-04-04 19:05:09 +02:00
|
|
|
# collect the classes of all executed istructions
|
|
|
|
def all_classes
|
2017-04-10 15:12:15 +02:00
|
|
|
classes = []
|
|
|
|
tick = 1
|
|
|
|
begin
|
2018-04-08 21:29:08 +02:00
|
|
|
while true and (classes.length < 300)
|
2017-04-10 15:12:15 +02:00
|
|
|
cl = ticks(1).class
|
|
|
|
tick += 1
|
|
|
|
classes << cl
|
|
|
|
break if cl == NilClass
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
puts "Error at tick #{tick}"
|
|
|
|
puts e
|
|
|
|
puts e.backtrace
|
|
|
|
end
|
2018-04-04 19:05:09 +02:00
|
|
|
classes
|
|
|
|
end
|
2018-04-19 18:47:35 +02:00
|
|
|
alias :run_all :all_classes
|
2018-04-04 19:05:09 +02:00
|
|
|
# for chaning the tests quickly output all instructions that are executed
|
|
|
|
def show_ticks
|
|
|
|
classes = all_classes
|
|
|
|
output_classes(classes)
|
|
|
|
end
|
|
|
|
# show all instructions of the main only
|
|
|
|
def show_main_ticks
|
|
|
|
classes = all_classes
|
|
|
|
classes = classes.slice(main_at , 1000)
|
|
|
|
output_classes(classes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def output_classes(classes)
|
2017-04-10 15:12:15 +02:00
|
|
|
str = classes.to_s.gsub("Risc::","")
|
2018-04-02 22:25:52 +02:00
|
|
|
all = str.split(",").each_slice(5).collect {|line| " " + line.join(",")}
|
|
|
|
puts all.join(",\n")
|
2017-04-10 15:12:15 +02:00
|
|
|
puts "length = #{classes.length}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|