Add a SameCheck and rename the label

SameCheck is very similar to NotSameCheck, but only used from the language
Label was named jump, fixed that
This commit is contained in:
Torsten Rüger 2020-02-19 02:16:44 +07:00
parent b88c5ff498
commit d751c53d1d
6 changed files with 48 additions and 11 deletions

View File

@ -47,6 +47,7 @@ require_relative "instruction/block_yield"
require_relative "instruction/resolve_method" require_relative "instruction/resolve_method"
require_relative "instruction/truth_check" require_relative "instruction/truth_check"
require_relative "instruction/not_same_check" require_relative "instruction/not_same_check"
require_relative "instruction/same_check"
require_relative "instruction/jump" require_relative "instruction/jump"
require_relative "instruction/return_jump" require_relative "instruction/return_jump"
require_relative "instruction/slot_load" require_relative "instruction/slot_load"

View File

@ -11,11 +11,15 @@ module SlotMachine
# is met. This means that the asm implementation is somewhat the reverse # is met. This means that the asm implementation is somewhat the reverse
# of the SlotMachine names. But it's easier to understand (imho) # of the SlotMachine names. But it's easier to understand (imho)
class Check < Instruction class Check < Instruction
attr_reader :false_jump attr_reader :false_label
def initialize(false_jump)
@false_jump = false_jump def initialize(false_label)
raise "Jump target must be a label #{false_jump}" unless false_jump.is_a?(Label) set_label(false_label)
end end
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
end end
end end

View File

@ -26,7 +26,7 @@ module SlotMachine
l_reg = left.to_register(compiler, self) l_reg = left.to_register(compiler, self)
r_reg = right.to_register(compiler, self) r_reg = right.to_register(compiler, self)
compiler.add_code Risc.op( self , :- , l_reg , r_reg) compiler.add_code Risc.op( self , :- , l_reg , r_reg)
compiler.add_code Risc::IsZero.new( self, false_jump.risc_label(compiler)) compiler.add_code Risc::IsZero.new( self, false_label.risc_label(compiler))
end end
end end
end end

View File

@ -0,0 +1,32 @@
module SlotMachine
# SlotMachine internal check, as the name says to see if two values are the same
# In other words, we this checks identity, bit-values, pointers
#
# The values that are compared are defined as Slots, ie can be anything
# available to the machine through frame message or self
#
# Acording to SlotMachine::Check logic, we jump to the given label is the values are not the same
#
class SameCheck < Check
attr_reader :left , :right
def initialize(left, right , label)
super(label)
@left , @right = left , right
end
def to_s
"SameCheck: #{left}:#{right}"
end
# basically move both left and right values into register
# subtract them and see if IsZero comparison
def to_risc(compiler)
l_reg = left.to_register(compiler, self)
r_reg = right.to_register(compiler, self)
compiler.add_code Risc.op( self , :- , l_reg , r_reg)
compiler.add_code Risc::IsNotZero.new( self, false_label.risc_label(compiler))
end
end
end

View File

@ -8,18 +8,18 @@ module SlotMachine
class TruthCheck < Check class TruthCheck < Check
attr_reader :condition attr_reader :condition
def initialize(condition , false_jump) def initialize(condition , false_label)
super(false_jump) super(false_label)
@condition = condition @condition = condition
raise "condition must be slot_definition #{condition}" unless condition.is_a?(Slotted) raise "condition must be slot_definition #{condition}" unless condition.is_a?(Slotted)
end end
def to_s def to_s
"TruthCheck #{@condition} -> #{false_jump}" "TruthCheck #{@condition} -> #{false_label}"
end end
def to_risc(compiler) def to_risc(compiler)
false_label = @false_jump.risc_label(compiler) false_label = @false_label.risc_label(compiler)
builder = compiler.builder("TruthCheck") builder = compiler.builder("TruthCheck")
condition_reg = @condition.to_register(compiler,self) condition_reg = @condition.to_register(compiler,self)
builder.build do builder.build do

View File

@ -15,7 +15,7 @@ module Sol
end end
def test_check_label def test_check_label
assert_equal NotSameCheck, @ins.class assert_equal NotSameCheck, @ins.class
assert @ins.false_jump.name.start_with?("method_ok_") , @ins.false_jump.name assert @ins.false_label.name.start_with?("method_ok_") , @ins.false_label.name
end end
def test_transfer def test_transfer
assert_equal ArgumentTransfer, @ins.next(3).class assert_equal ArgumentTransfer, @ins.next(3).class
@ -74,7 +74,7 @@ module Sol
end end
def test_check_label def test_check_label
assert_equal NotSameCheck, @ins.class assert_equal NotSameCheck, @ins.class
assert @ins.false_jump.name.start_with?("cache_ok_") , @ins.false_jump.name assert @ins.false_label.name.start_with?("cache_ok_") , @ins.false_label.name
end end
def test_array def test_array
check_array [NotSameCheck, SlotLoad, ResolveMethod, Label, MessageSetup , check_array [NotSameCheck, SlotLoad, ResolveMethod, Label, MessageSetup ,