rename register to risc

seems to fit the layer much better as we really have a very reduced
instruction set
This commit is contained in:
Torsten Ruger
2017-01-19 09:02:29 +02:00
parent da5823a1a0
commit aa79e41d1c
127 changed files with 348 additions and 346 deletions

2
test/risc/helper.rb Normal file
View File

@ -0,0 +1,2 @@
require_relative "../helper"
Risc.machine.boot unless Risc.machine.booted

View File

@ -0,0 +1,23 @@
require_relative "../helper"
require "risc/interpreter"
module Risc
module Ticker
include AST::Sexp
include InterpreterHelpers
def setup
Risc.machine.boot
do_clean_compile
Vm.compile_ast( @input )
Collector.collect_space
@interpreter = Interpreter.new
@interpreter.start Risc.machine.init
end
# must be after boot, but before main compile, to define method
def do_clean_compile
end
end
end

View File

@ -0,0 +1,64 @@
require_relative "helper"
module Risc
class AddTest < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
return 5 + 7
end
end
HERE
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
NilClass]
end
def test_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r2 )
assert Integer , @interpreter.get_register( :r2 ).class
end
def test_transfer
transfer = ticks 19
assert_equal RiscTransfer , transfer.class
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
end
def test_call
ret = ticks(18)
assert_equal FunctionReturn , ret.class
object = @interpreter.get_register( ret.register )
link = object.get_internal_word( ret.index )
assert_equal Label , link.class
end
def test_adding
done_op = ticks(12)
assert_equal OperatorInstruction , done_op.class
left = @interpreter.get_register(done_op.left)
rr = done_op.right
right = @interpreter.get_register(rr)
assert_equal Fixnum , left.class
assert_equal Fixnum , right.class
assert_equal 7 , right
assert_equal 12 , left
done_tr = ticks(1)
assert_equal RegToSlot , done_tr.class
result = @interpreter.get_register(done_op.left)
assert_equal result , 12
end
end
end

View File

@ -0,0 +1,9 @@
require_relative "test_add"
require_relative "test_change"
require_relative "test_byte_to_reg"
require_relative "test_called_if"
require_relative "test_simple_if"
require_relative "test_puts"
require_relative "test_plus"
require_relative "test_mult"
require_relative "test_reg_to_byte"

View File

@ -0,0 +1,67 @@
require_relative "helper"
module Risc
class TestInterpretRegToByte < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
"Hello".set_internal_byte(1,104)
end
end
HERE
@input = s(:statements, s(:call,
:set_internal_byte ,
s(:arguments, s(:int, 1), s(:int, 104)),
s(:receiver, s(:string, "Hello"))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot,
RiscTransfer, FunctionCall, Label, LoadConstant, SlotToReg,
RegToSlot, SlotToReg, SlotToReg, SlotToReg, SlotToReg,
SlotToReg, RegToByte, Label, FunctionReturn, RiscTransfer,
SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot,
Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
end
def test_branch
was = @interpreter.instruction
assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal FunctionCall , ticks(8).class
end
def test_exit
done = ticks(50)
assert_equal NilClass , done.class
end
def test_reg_to_byte
done = ticks(37)
assert_equal RegToByte , done.class
assert_equal "h".ord , @interpreter.get_register(done.register)
end
end
end

View File

@ -0,0 +1,55 @@
require_relative "helper"
module Risc
class IfCalledTest < MiniTest::Test
include Ticker
include Compiling
def setup
@string_input = <<HERE
class Space
int itest(int n)
if_zero( n - 12)
"then".putstring()
else
"else".putstring()
end
end
int main()
itest(20)
end
end
HERE
@input = s(:statements, s(:call, :itest , s(:arguments, s(:int, 20))))
super
end
# must be after boot, but before main compile, to define method
def do_clean_compile
clean_compile :Space , :itest , {:n => :Integer} ,
s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:arg, :n), s(:int, 12))),
s(:true_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "else"))))))
end
def test_if
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
SlotToReg, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label,
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg,
LoadConstant, OperatorInstruction, IsZero, SlotToReg, LoadConstant,
RegToSlot, LoadConstant, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall,
Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg,
SlotToReg, RiscTransfer, Syscall, RiscTransfer, RiscTransfer,
RegToSlot, Label, FunctionReturn, RiscTransfer, SlotToReg,
SlotToReg, Branch, Label, Label, FunctionReturn,
RiscTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
NilClass]
end
end
end

View File

@ -0,0 +1,48 @@
require_relative "helper"
module Risc
class AddChange < MiniTest::Test
include Ticker
def setup
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
@state_events = {}
@instruction_events = []
super
end
def state_changed( a , b)
@state_events[:state_changed] = [a , b]
end
def instruction_changed(was , is )
@instruction_events << was
end
def test_state_change
@interpreter.register_event :state_changed , self
ticks 30
assert @state_events[:state_changed]
assert_equal 2 , @state_events[:state_changed].length
assert_equal :running, @state_events[:state_changed][0]
@interpreter.unregister_event :state_changed , self
end
def test_instruction_events
@interpreter.register_event :instruction_changed , self
ticks 30
assert_equal 20 , @instruction_events.length
@interpreter.unregister_event :instruction_changed , self
end
def test_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
NilClass]
end
end
end

View File

@ -0,0 +1,40 @@
require_relative "helper"
module Risc
class MultTest < MiniTest::Test
include Ticker
include AST::Sexp
def setup
@string_input = <<HERE
class Space
int main()
return #{2**31} * #{2**31}
end
end
HERE
@input = s(:statements, s(:return, s(:operator_value, :*, s(:int, 2147483648), s(:int, 2147483648))))
super
end
def test_mult
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
NilClass]
check_return 0
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end
end
end

View File

@ -0,0 +1,39 @@
require_relative "helper"
module Risc
class PlusTest < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
return #{2**62 - 1} + 1
end
end
HERE
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 4611686018427387903), s(:int, 1))))
super
end
def test_add
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
NilClass]
check_return 0
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end
end
end

View File

@ -0,0 +1,71 @@
require_relative "helper"
module Risc
class TestPuts < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
"Hello again".putstring()
end
end
HERE
@input = s(:statements, s(:call, :putstring, s(:arguments), s(:receiver, s(:string, "Hello again"))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer,
FunctionCall, Label, LoadConstant, SlotToReg, RegToSlot,
SlotToReg, SlotToReg, RiscTransfer, Syscall, RiscTransfer,
RiscTransfer, RegToSlot, Label, FunctionReturn, RiscTransfer,
SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot,
Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
end
def test_branch
was = @interpreter.instruction
assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal FunctionCall , ticks(8).class
end
def test_putstring
done = ticks(29)
assert_equal Syscall , done.class
assert_equal "Hello again" , @interpreter.stdout
end
def test_return
done = ticks(34)
assert_equal FunctionReturn , done.class
assert Label , @interpreter.instruction.class
assert @interpreter.instruction.is_a?(Instruction) , "not instruction #{@interpreter.instruction}"
end
def test_exit
done = ticks(45)
assert_equal NilClass , done.class
assert_equal "Hello again" , @interpreter.stdout
end
end
end

View File

@ -0,0 +1,67 @@
require_relative "helper"
module Risc
class TestInterpretByteToReg < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
"Hello".get_internal_byte(1)
end
end
HERE
@input = s(:statements, s(:call,
:get_internal_byte,
s(:arguments, s(:int, 1)),
s(:receiver, s(:string, "Hello"))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label,
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg,
SlotToReg, ByteToReg, RegToSlot, Label, FunctionReturn,
RiscTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
NilClass]
end
def test_branch
was = @interpreter.instruction
assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal FunctionCall , ticks(8).class
end
def test_exit
done = ticks(46)
assert_equal NilClass , done.class
end
def test_byte_to_reg
done = ticks(32)
assert_equal ByteToReg , done.class
assert_equal "H".ord , @interpreter.get_register(done.register)
end
end
end

View File

@ -0,0 +1,42 @@
require_relative "helper"
module Risc
class IfSimpleTest < MiniTest::Test
include Ticker
include Compiling
def setup
@string_input = <<HERE
class Space
int main()
if_zero( 10 - 12)
"then".putstring()
else
"else".putstring()
end
end
end
HERE
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))),
s(:true_statements, s(:call, :putstring, s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, :putstring, s(:arguments), s(:receiver, s(:string, "else"))))))
super
end
def test_if
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
LoadConstant, OperatorInstruction, IsZero, SlotToReg, LoadConstant,
RegToSlot, LoadConstant, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall,
Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg,
SlotToReg, RiscTransfer, Syscall, RiscTransfer, RiscTransfer,
RegToSlot, Label, FunctionReturn, RiscTransfer, SlotToReg,
SlotToReg, Branch, Label, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
NilClass]
end
end
end

9
test/risc/test_all.rb Normal file
View File

@ -0,0 +1,9 @@
require_relative "interpreter/test_all"
require_relative "test_assembler"
require_relative "test_collector"
require_relative "test_compat"
require_relative "test_instructions"
require_relative "test_machine"
require_relative "test_padding"
require_relative "test_positioned"

View File

@ -0,0 +1,43 @@
require_relative "../helper"
module Risc
class TestAssembler < MiniTest::Test
def setup
@machine = Risc.machine.boot
end
def test_no_object
@assembler = Assembler.new(@machine , {})
assert_nil @machine.translate_arm
end
def test_space
@assembler = Assembler.new(@machine , Collector.collect_space)
assert_nil @machine.translate_arm
end
def test_write_fails
@assembler = Assembler.new(@machine , {})
assert_raises{ @assembler.assemble} #must translate first
end
def test_assemble_no_objects
@assembler = Assembler.new(@machine , {})
assert_nil @machine.translate_arm
assert @assembler.assemble
end
def test_translate_space
@assembler = Assembler.new(@machine , Collector.collect_space)
assert_nil @machine.translate_arm
end
def test_assemble_space
@assembler = Assembler.new(@machine , Collector.collect_space)
assert_nil @machine.translate_arm
assert @assembler.assemble
end
def test_write_space
@assembler = Assembler.new(@machine , Collector.collect_space)
assert_nil @machine.translate_arm
assert @assembler.write_as_string
end
end
end

View File

@ -0,0 +1,11 @@
require_relative "../helper"
module Risc
class TestCollector < MiniTest::Test
def test_simple_collect
Machine.new.boot
objects = Risc::Collector.collect_space
assert ((350 < objects.length) or (430 > objects.length)) , objects.length.to_s
end
end
end

22
test/risc/test_compat.rb Normal file
View File

@ -0,0 +1,22 @@
require_relative "../helper"
class TestCompat < MiniTest::Test
def setup
Risc.machine.boot unless Risc.machine.booted
end
def test_list_create_from_array
array = [1,2,3]
list = Parfait.new_list(array)
assert_equal array , list.to_a
end
def test_word_create_from_string
string = "something"
word = Parfait.new_word(string)
assert_equal word , Parfait.new_word(string)
assert_equal string , word.to_string
end
end

View File

@ -0,0 +1,81 @@
require_relative "../helper"
module Risc
class TestInstructions < MiniTest::Test
def setup
@label = Label.new("test" , "test")
@branch = Branch.new("test" , @label)
@instruction = Instruction.new("test")
end
def test_branch_tos1
assert @branch.to_s.include?("Branch")
assert @branch.to_s.include?("test")
end
def test_branch_tos2
branch = Branch.new(nil ,nil)
assert branch.to_s.include?("Branch")
end
def test_label_tos1
assert @label.to_s.include?("Label")
end
def test_label_tos2
assert Label.new(nil,nil).to_s.include?("Label")
end
def test_last_empty
assert_equal @instruction, @instruction.last
end
def test_last_not_empty
@instruction.set_next @branch
assert_equal @branch, @instruction.last
end
def test_append_empty
@instruction.append @branch
assert_equal @branch, @instruction.last
end
def test_insert
@instruction.insert @branch
assert_equal @branch, @instruction.last
end
def test_append_not_empty
@instruction.append @branch
@instruction.append @label
assert_equal @label, @instruction.last
end
def test_next1
assert_nil @instruction.next
end
def test_next2
@instruction.set_next @label
assert_equal @label , @instruction.next
assert_nil @instruction.next(2)
end
def test_replace
@instruction.append @branch
@instruction.replace_next @label
assert_equal @label, @instruction.last
assert_equal @label, @instruction.next
assert_equal 2 , @instruction.length , @instruction.to_arr
end
def test_each_label1
@instruction.set_next @label
start = Label.new("test" , "test" , @instruction)
count = 0
start.each_label { |l| count += 1 }
assert_equal 2 , count
end
def test_each_label2
@instruction.set_next @branch
start = Label.new("test" , "test" , @instruction)
count = 0
start.each_label { |l| count += 1 }
assert_equal 2 , count
end
def test_label_is_method
label = Label.new("test" , "Object.test")
assert label.is_method
end
def test_label_is_not_method
assert ! @label.is_method
end
end
end

18
test/risc/test_machine.rb Normal file
View File

@ -0,0 +1,18 @@
require_relative "../helper"
module Risc
class TestMachine < MiniTest::Test
def setup
@machine = Risc.machine.boot
end
def test_collect_all_types
objects = Risc::Collector.collect_space
objects.each do |id, objekt|
next unless objekt.is_a?( Parfait::Type )
assert Parfait.object_space.get_type_for( objekt.hash ) , objekt.hash
end
end
end
end

20
test/risc/test_padding.rb Normal file
View File

@ -0,0 +1,20 @@
require_relative "../helper"
class TestPadding < MiniTest::Test
def test_small
[6,27,28].each do |p|
assert_equal 32 , Padding.padded(p) , "Expecting 32 for #{p}"
end
end
def test_medium
[29,33,40,57,60].each do |p|
assert_equal 64 , Padding.padded(p) , "Expecting 64 for #{p}"
end
end
def test_large
[61,65,88].each do |p|
assert_equal 96 , Padding.padded(p) , "Expecting 96 for #{p}"
end
end
end

View File

@ -0,0 +1,42 @@
require_relative "../helper"
class TestPositioned < MiniTest::Test
def setup
Risc.machine.boot unless Risc.machine.booted
end
def test_list1
list = Parfait.new_list([1])
assert_equal 32 , list.padded_length
end
def test_list5
list = Parfait.new_list([1,2,3,4,5])
assert_equal 32 , list.padded_length
end
def test_type
type = Parfait::Type.for_hash Parfait.object_space.get_class_by_name(:Object) , {}
type.set_type( type )
assert_equal 32 , type.padded_length
end
def test_word
word = Parfait::Word.new(12)
assert_equal 32 , word.padded_length
end
def test_raises_no_init
assert_raises { Positioned.position(self)}
end
def test_raises_set_nil
assert_raises { Positioned.set_position(self,nil)}
end
def test_raises_reset_far
assert_raises do
test = TestPosition.new
test.set_position 0
test.set_position 12000
end
end
def test_pos_arm
mov = Arm::ArmMachine.mov :r1, 128
mov.set_position(0)
end
end