did return sequence

surprisingly easy. also some check polish
This commit is contained in:
Torsten 2020-03-03 16:09:41 +02:00
parent fd43fc9e5c
commit a4c0b8e5f9
7 changed files with 65 additions and 26 deletions

View File

@ -19,7 +19,8 @@ module SlotMachine
def set_label(false_label)
@false_label = false_label
raise "Jump target must be a label #{false_label}" unless false_label.is_a?(Label) end
raise "Jump target must be a label #{false_label}" unless false_label.is_a?(Label)
end
end
end

View File

@ -20,16 +20,9 @@ module SlotMachine
#
class ReturnSequence < Instruction
def to_risc(compiler)
compiler.reset_regs
builder = compiler.builder(self)
builder.build do
object! << message[:return_value]
caller_reg! << message[:caller]
caller_reg[:return_value] << object
end
builder.build do
return_address! << message[:return_address]
return_address << return_address[ Parfait::Integer.integer_index]
compiler.build(to_s) do
message[:caller][:return_value] << message[:return_value]
return_address = message[:return_address].to_reg
message << message[:caller]
add_code Risc.function_return("return #{compiler.callable.name}", return_address)
end

View File

@ -21,7 +21,7 @@ module SlotMachine
def to_risc(compiler)
false_label = @false_label.risc_label(compiler)
condition_reg = @condition.to_register(compiler,self)
compiler.build(self.to_s) do
object = load_object Parfait.object_space.false_object
object.op :- , condition_reg

View File

@ -0,0 +1,20 @@
require_relative "helper"
module SlotMachine
class TestLabel < SlotMachineInstructionTest
def instruction
Label.new("ok" , "target")
end
def test_len
assert_equal 2 , all.length
assert_equal Risc::Label , all.first.class
end
def test_1_label
assert_equal Risc::Label , risc(1).class
end
def test_2_slot
label = @instruction.risc_label( @compiler)
assert_equal risc(1) , label
end
end
end

View File

@ -6,29 +6,26 @@ module SlotMachine
ReturnSequence.new("source")
end
def test_len
assert_equal 8 , all.length , all_str
assert_equal 7 , all.length , all_str
end
def test_1_load_return_value
assert_slot_to_reg risc(1) ,:r0 , 5 , :r1
assert_slot_to_reg risc(1) ,:message , 6 , "message.caller"
end
def test_2_load_caller
assert_slot_to_reg risc(2) ,:r0 , 6 , :r2
assert_slot_to_reg risc(2) ,"message.caller" , 5 , "message.caller.return_value"
end
def test_3_store_return_in_caller
assert_reg_to_slot risc(3) , :r1 , :r2 , 5
assert_reg_to_slot risc(3) , "message.caller.return_value" , "message.caller" , 5
end
def test_4_load_return_address
assert_slot_to_reg risc(4) ,:r0 , 4 , :r3
assert_slot_to_reg risc(4) ,:message , 4 , "message.return_address"
end
def test_5_get_int_for_address
assert_slot_to_reg risc(5) ,:r3 , 2 , :r3
def test_5_swap_messages
assert_slot_to_reg risc(5) ,:message, 6 , :message
end
def test_6_swap_messages
assert_slot_to_reg risc(6) ,:r0 , 6 , :r0
end
def test_7_do_return
assert_equal Risc::FunctionReturn , risc(7).class
assert_equal :r3 , risc(7).register.symbol
def test_6_do_return
assert_equal Risc::FunctionReturn , risc(6).class
assert_equal :"message.return_address" , risc(6).register.symbol
end
end
end

View File

@ -0,0 +1,28 @@
require_relative "helper"
module SlotMachine
class TestSameCheck < SlotMachineInstructionTest
def instruction
left = SlottedMessage.new( [:caller])
right = SlottedMessage.new( [:next_message])
SameCheck.new(left , right , Label.new("ok" , "target"))
end
def test_len
assert_equal 5 , all.length
assert_equal Risc::Label , all.first.class
end
def test_1_slot
assert_slot_to_reg risc(1) ,:message , 6 , :"message.caller"
end
def test_2_slot
assert_slot_to_reg risc(2) ,:message , 1 , :"message.next_message"
end
def test_3_op
assert_operator risc(3) , :- , "message.caller" , "message.next_message"
end
def test_4_zero
assert_equal Risc::IsNotZero , risc(4).class
assert_label risc(4).label , "target"
end
end
end

View File

@ -1,7 +1,7 @@
require_relative "helper"
module SlotMachine
class TestSameCheck < SlotMachineInstructionTest
class TestTruthCheck < SlotMachineInstructionTest
def instruction
target = SlottedMessage.new( [:caller])
TruthCheck.new(target , Label.new("ok" , "target"))