fix get_internal_byte

improved operators and tests
some logic errors still
This commit is contained in:
Torsten 2020-03-09 12:47:12 +02:00
parent a70f63e7bb
commit fb4fa598f2
9 changed files with 46 additions and 45 deletions

View File

@ -72,6 +72,7 @@ module Risc
# from itself (the slot) and the register given
def <=( reg )
raise "not reg #{reg}" unless reg.is_a?(RegisterValue)
raise "Index must be register #{index}" unless(index.is_a?(RegisterValue))
reg_to_byte = Risc.reg_to_byte("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg , register, index)
compiler.add_code(reg_to_byte) if compiler
reg_to_byte

View File

@ -162,6 +162,7 @@ module Risc
# since << covers all other cases, this must have a RegisterSlot as the right
def <=( right )
raise "not implemented for #{right.class}:#{right}" unless right.is_a?( RegisterSlot )
raise "Right index must be register #{right.index}" unless(right.index.is_a?(RegisterValue))
ins = Risc.byte_to_reg("#{right.register.type}[#{right.index}] -> #{self.type}" , right.register , right.index , self)
compiler.add_code(ins) if compiler
return ins

View File

@ -4,9 +4,8 @@ module SlotMachine
builder = compiler.builder(compiler.source)
integer_tmp = builder.allocate_int
builder.build do
object! << message[:receiver]
integer! << message[:arg1] #"at"
integer.reduce_int
object = message[:receiver].to_reg
integer = message[:arg1].to_reg.reduce_int(false)
object <= object[integer]
integer_tmp[Parfait::Integer.integer_index] << object
message[:return_value] << integer_tmp

View File

@ -22,7 +22,7 @@ module SlotMachine
save_message( builder )
builder.build do
message << message[:return_value]
message.reduce_int
message.reduce_int(false) #hack, noo type check
add_code Risc::Syscall.new("emit_syscall(exit)", :exit )
end
end
@ -32,7 +32,7 @@ module SlotMachine
# This relies on linux to save and restore all registers
#
def self.save_message(builder)
r8 = Risc::RegisterValue.new( :r8 , :Message).set_builder(builder)
r8 = Risc::RegisterValue.new( :saved_message , :Message).set_compiler(builder.compiler)
builder.build {r8 << message}
end
@ -42,7 +42,7 @@ module SlotMachine
# so that the return value already has an integer instance
# This instance is filled with os return value
def self.restore_message(builder)
r8 = Risc::RegisterValue.new( :r8 , :Message)
r8 = Risc::RegisterValue.new( :saved_message , :Message).set_compiler(builder.compiler)
builder.build do
integer_reg! << message
message << r8

View File

@ -2,6 +2,7 @@ require_relative "../helper"
module Risc
class TestRegisterSlot1 < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = Risc.test_compiler
@ -73,6 +74,7 @@ module Risc
end
end
class TestRegisterSlot4 < MiniTest::Test
include HasCompiler
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = Risc.test_compiler
@ -85,14 +87,12 @@ module Risc
assert_equal NilClass , @compiler.risc_instructions.next(3).class
end
def test_slot_to
slot = @compiler.risc_instructions.next(1)
assert_slot_to_reg slot , :message, 1, :"message.next_message"
assert slot.register.compiler
assert_slot_to_reg 1 , :message, 1, :"message.next_message"
assert risc(1).register.compiler
end
def test_reg_to
reg = @compiler.risc_instructions.next(2)
assert_reg_to_slot reg , :message, :"message.next_message" , 4
assert reg.register.compiler
assert_reg_to_slot 2 , :message, :"message.next_message" , 4
assert risc(2).register.compiler
end
end
end

View File

@ -59,22 +59,22 @@ module Risc
assert_operator ret , :<< , :message , "id_.type"
end
def test_byte_to_reg
instr = @r0 <= @r1[1]
instr = @r0 <= @r1[@r0]
assert_equal ByteToReg , instr.class
assert_equal @r1 , instr.array
assert_equal @r0 , instr.register
assert_equal 1 , instr.index
assert_equal @r0 , instr.index
end
def test_slot_to_reg
instr = @r0 << @r2[:next_object]
assert_slot_to_reg instr , "id_" , 2 , :message
end
def test_reg_to_byte
instr = @r1[1] <= @r0
instr = @r1[@r0] <= @r0
assert_equal RegToByte , instr.class
assert_equal @r1 , instr.array
assert_equal @r0 , instr.register
assert_equal 1 , instr.index
assert_equal @r0 , instr.index
end
end
end

View File

@ -33,7 +33,7 @@ module SlotMachine
assert_load 2 , Parfait::NilClass , "id_nilclass_"
assert_slot_to_reg 3 , "id_factory_" , 2 , "id_factory_.next_object"
assert_operator 4 , :- , "id_nilclass_" , "id_factory_.next_object"
assert_zero 5 , "cont_label"
assert_not_zero 5 , "cont_label"
assert_slot_to_reg 6 , "id_factory_" , 2 , "id_factory_.next_object"
assert_reg_to_slot 7 , "id_factory_.next_object" , "id_factory_" , 2
assert_load 8 , Parfait::Factory , "id_factory_"

View File

@ -14,13 +14,13 @@ module SlotMachine
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 68 , @method.to_risc.risc_instructions.length
assert_equal 69 , @method.to_risc.risc_instructions.length
end
def test_allocate
assert_allocate
end
def test_return
assert_return(60)
assert_return(61)
end
def test_all
assert_slot_to_reg 23 , "message" , 2 , "message.receiver"
@ -49,19 +49,19 @@ module SlotMachine
assert_data 46 , 10
assert_transfer 47 , :integer_reg , :integer_1
assert_operator 48 , :* , :integer_1 , :integer_const
assert_transfer 49 , "message.receiver.data_1" , :integer_1
# assert_operator 50 , :- , :r2 , :integer_1
assert_data 50 , 6
assert_operator 51 , :+ , :integer_1 , :integer_const
assert_data 52 , 4
assert_operator 53 , :>> , :integer_1 , :integer_const
assert_operator 54 , :+ , :integer_reg , :integer_1
assert_reg_to_slot 55 , :integer_reg , "id_factory_.next_object" , 2
assert_reg_to_slot 56 , "id_factory_.next_object" , :message , 5
assert_slot_to_reg 57 ,:message , 5 , "message.return_value"
assert_reg_to_slot 58 , "message.return_value" , :message , 5
assert_branch 59 , "return_label"
assert_label 60 , "return_label"
assert_operator 49 , :- , "message.receiver.data_1" , :integer_1
assert_transfer 50 , "message.receiver.data_1" , :integer_1
assert_data 51 , 6
assert_operator 52 , :+ , :integer_1 , :integer_const
assert_data 53 , 4
assert_operator 54 , :>> , :integer_1 , :integer_const
assert_operator 55 , :+ , :integer_reg , :integer_1
assert_reg_to_slot 56 , :integer_reg , "id_factory_.next_object" , 2
assert_reg_to_slot 57 , "id_factory_.next_object" , :message , 5
assert_slot_to_reg 58 ,:message , 5 , "message.return_value"
assert_reg_to_slot 59 , "message.return_value" , :message , 5
assert_branch 60 , "return_label"
assert_label 61 , "return_label"
end
end
end

View File

@ -15,29 +15,29 @@ module SlotMachine
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 41 , @method.to_risc.risc_instructions.length
assert_equal 40 , @method.to_risc.risc_instructions.length
end
def test_allocate
assert_allocate
end
def test_all
assert_slot_to_reg risc(23),:r0 , 2 , :r2
assert_slot_to_reg risc(24),:r0 , 9 , :r3
assert_slot_to_reg risc(25),:r3 , 2 , :r3
assert_slot_to_reg 23 ,:message , 2 , "message.receiver"
assert_slot_to_reg 24 ,:message , 9 , "message.arg1"
assert_slot_to_reg 25 ,"message.arg1" , 2 , "message.arg1.data_1"
assert_equal Risc::ByteToReg , risc(26).class
assert_equal :r2 , risc(26).array.symbol
assert_equal :r2 , risc(26).register.symbol
assert_equal :r3 , risc(26).index.symbol
assert_equal :"message.receiver" , risc(26).array.symbol
assert_equal :"message.receiver" , risc(26).register.symbol
assert_equal :"message.arg1.data_1" , risc(26).index.symbol
assert_reg_to_slot risc(27) , :r2 , :r1 , 2
assert_reg_to_slot risc(28) , :r1 , :r0 , 5
assert_slot_to_reg risc(29),:r0 , 5 , :r2
assert_reg_to_slot risc(30) , :r2 , :r0 , 5
assert_reg_to_slot 27 , "message.receiver" , "id_factory_.next_object" , 2
assert_reg_to_slot 28 , "id_factory_.next_object" , :message , 5
assert_slot_to_reg 29 ,:message , 5 , "message.return_value"
assert_reg_to_slot 30 , "message.return_value" , :message , 5
assert_branch risc(31) , "return_label"
assert_label risc(32) , "return_label"
assert_branch 31 , "return_label"
assert_label 32 , "return_label"
end
def test_return
assert_return(32)