fix unconditional jump

and affected tests
This commit is contained in:
Torsten Ruger 2018-03-20 22:05:09 +05:30
parent c12830ee6d
commit 77084dc894
5 changed files with 17 additions and 9 deletions

View File

@ -21,7 +21,7 @@ module Mom
# The argument that is passed is a MethodCompiler, which has the method and some # 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) # state about registers used. (also provides helpers to generate risc instructions)
def to_risc(compiler) def to_risc(compiler)
raise Risc::Label.new(self.class.name, self.class.name + "_todo") raise self.class.name + "_todo"
end end
end end

View File

@ -1,6 +1,9 @@
module Mom 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 class Jump < Instruction
attr_reader :target attr_reader :target
@ -8,8 +11,8 @@ module Mom
def initialize(target) def initialize(target)
@target = target @target = target
end end
def to_risc(context) def to_risc(compiler)
Risc::Label.new(self,"Jump") Risc::Unconditional.new(self , @target.to_risc(compiler))
end end
end end

View File

@ -60,6 +60,11 @@ module Risc
super(source , label) super(source , label)
end end
end end
class Unconditional < Branch
end
class IsZero < Branch class IsZero < Branch
end end

View File

@ -8,7 +8,7 @@ module Risc
super super
@input = "if(@a) ; arg = 5 ; else; arg = 6; end" @input = "if(@a) ; arg = 5 ; else; arg = 6; end"
@expect = [SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant, IsSame , @expect = [SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant, IsSame ,
Label, LoadConstant, SlotToReg, RegToSlot, Label, Label , Label, LoadConstant, SlotToReg, RegToSlot, Unconditional, Label ,
LoadConstant, SlotToReg, RegToSlot, Label] LoadConstant, SlotToReg, RegToSlot, Label]
end end
@ -45,7 +45,7 @@ module Risc
def test_true_jump # should jumpp to merge label def test_true_jump # should jumpp to merge label
produced = produce_body produced = produce_body
assert_equal "Jump" , produced.next(10).name assert produced.next(10).label.name.start_with?("merge_label")
end end
end end
end end

View File

@ -8,7 +8,7 @@ module Risc
super super
@input = "while(@a) ; arg = 5 end" @input = "while(@a) ; arg = 5 end"
@expect = [Label, SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant , @expect = [Label, SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant ,
IsSame, LoadConstant, SlotToReg, RegToSlot, Label, Label] IsSame, LoadConstant, SlotToReg, RegToSlot, Unconditional, Label]
end end
def test_while_instructions def test_while_instructions
@ -37,9 +37,9 @@ module Risc
assert produced.next(11).name.start_with?("merge_label") assert produced.next(11).name.start_with?("merge_label")
end end
def test_true_jump # should jumpp to merge label def test_back_jump # should jump back to condition label
produced = produce_body produced = produce_body
assert_equal "Jump" , produced.next(10).name assert_equal produced , produced.next(10).label
end end
end end
end end