From 77084dc894240b726c78b5e364c9354be6d159e6 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 20 Mar 2018 22:05:09 +0530 Subject: [PATCH] fix unconditional jump and affected tests --- lib/mom/instruction/instruction.rb | 2 +- lib/mom/instruction/jump.rb | 9 ++++++--- lib/risc/instructions/branch.rb | 5 +++++ test/mom/test_if_else.rb | 4 ++-- test/mom/test_while.rb | 6 +++--- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/mom/instruction/instruction.rb b/lib/mom/instruction/instruction.rb index 59793c3d..fa31bc85 100644 --- a/lib/mom/instruction/instruction.rb +++ b/lib/mom/instruction/instruction.rb @@ -21,7 +21,7 @@ module Mom # The argument that is passed is a MethodCompiler, which has the method and some # state about registers used. (also provides helpers to generate risc instructions) def to_risc(compiler) - raise Risc::Label.new(self.class.name, self.class.name + "_todo") + raise self.class.name + "_todo" end end diff --git a/lib/mom/instruction/jump.rb b/lib/mom/instruction/jump.rb index b0a13172..ea14f0b5 100644 --- a/lib/mom/instruction/jump.rb +++ b/lib/mom/instruction/jump.rb @@ -1,6 +1,9 @@ module Mom - # unconditional jump to the instruction given as target + # Unconditional jump to the Label given as target + # Eg used at the end of while or end of if_true branch + # + # Risc equivalent is the same really, called Unconditional there. # class Jump < Instruction attr_reader :target @@ -8,8 +11,8 @@ module Mom def initialize(target) @target = target end - def to_risc(context) - Risc::Label.new(self,"Jump") + def to_risc(compiler) + Risc::Unconditional.new(self , @target.to_risc(compiler)) end end diff --git a/lib/risc/instructions/branch.rb b/lib/risc/instructions/branch.rb index 9594bbc7..fcd8e7a9 100644 --- a/lib/risc/instructions/branch.rb +++ b/lib/risc/instructions/branch.rb @@ -60,6 +60,11 @@ module Risc super(source , label) end end + + class Unconditional < Branch + + end + class IsZero < Branch end diff --git a/test/mom/test_if_else.rb b/test/mom/test_if_else.rb index 55edfbcb..1c57656f 100644 --- a/test/mom/test_if_else.rb +++ b/test/mom/test_if_else.rb @@ -8,7 +8,7 @@ module Risc super @input = "if(@a) ; arg = 5 ; else; arg = 6; end" @expect = [SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant, IsSame , - Label, LoadConstant, SlotToReg, RegToSlot, Label, Label , + Label, LoadConstant, SlotToReg, RegToSlot, Unconditional, Label , LoadConstant, SlotToReg, RegToSlot, Label] end @@ -45,7 +45,7 @@ module Risc def test_true_jump # should jumpp to merge label produced = produce_body - assert_equal "Jump" , produced.next(10).name + assert produced.next(10).label.name.start_with?("merge_label") end end end diff --git a/test/mom/test_while.rb b/test/mom/test_while.rb index a9ac7a92..cebdc106 100644 --- a/test/mom/test_while.rb +++ b/test/mom/test_while.rb @@ -8,7 +8,7 @@ module Risc super @input = "while(@a) ; arg = 5 end" @expect = [Label, SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant , - IsSame, LoadConstant, SlotToReg, RegToSlot, Label, Label] + IsSame, LoadConstant, SlotToReg, RegToSlot, Unconditional, Label] end def test_while_instructions @@ -37,9 +37,9 @@ module Risc assert produced.next(11).name.start_with?("merge_label") end - def test_true_jump # should jumpp to merge label + def test_back_jump # should jump back to condition label produced = produce_body - assert_equal "Jump" , produced.next(10).name + assert_equal produced , produced.next(10).label end end end