diff --git a/lib/slot_machine/instructions/check.rb b/lib/slot_machine/instructions/check.rb index 54b9fc11..95b73b8e 100644 --- a/lib/slot_machine/instructions/check.rb +++ b/lib/slot_machine/instructions/check.rb @@ -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 diff --git a/lib/slot_machine/instructions/return_sequence.rb b/lib/slot_machine/instructions/return_sequence.rb index 448728a5..1b0d91ef 100644 --- a/lib/slot_machine/instructions/return_sequence.rb +++ b/lib/slot_machine/instructions/return_sequence.rb @@ -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 diff --git a/lib/slot_machine/instructions/truth_check.rb b/lib/slot_machine/instructions/truth_check.rb index 339ddcff..3bbebd33 100644 --- a/lib/slot_machine/instructions/truth_check.rb +++ b/lib/slot_machine/instructions/truth_check.rb @@ -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 diff --git a/test/slot_machine/instructions/test_label.rb b/test/slot_machine/instructions/test_label.rb new file mode 100644 index 00000000..951abe28 --- /dev/null +++ b/test/slot_machine/instructions/test_label.rb @@ -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 diff --git a/test/slot_machine/instructions/test_return_sequence.rb b/test/slot_machine/instructions/test_return_sequence.rb index c19cb712..476cc32c 100644 --- a/test/slot_machine/instructions/test_return_sequence.rb +++ b/test/slot_machine/instructions/test_return_sequence.rb @@ -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 diff --git a/test/slot_machine/instructions/test_same_check.rb b/test/slot_machine/instructions/test_same_check.rb new file mode 100644 index 00000000..9a7c510e --- /dev/null +++ b/test/slot_machine/instructions/test_same_check.rb @@ -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 diff --git a/test/slot_machine/instructions/test_truth_check.rb b/test/slot_machine/instructions/test_truth_check.rb index b4e4ae7c..48772580 100644 --- a/test/slot_machine/instructions/test_truth_check.rb +++ b/test/slot_machine/instructions/test_truth_check.rb @@ -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"))