From d751c53d1d5b27ddb3eea8fe04b5e70593e9fd18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Wed, 19 Feb 2020 02:16:44 +0700 Subject: [PATCH] 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 --- lib/slot_machine/instruction.rb | 1 + lib/slot_machine/instruction/check.rb | 12 ++++--- .../instruction/not_same_check.rb | 2 +- lib/slot_machine/instruction/same_check.rb | 32 +++++++++++++++++++ lib/slot_machine/instruction/truth_check.rb | 8 ++--- test/sol/test_yield_statement.rb | 4 +-- 6 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 lib/slot_machine/instruction/same_check.rb diff --git a/lib/slot_machine/instruction.rb b/lib/slot_machine/instruction.rb index 54c09e47..75175686 100644 --- a/lib/slot_machine/instruction.rb +++ b/lib/slot_machine/instruction.rb @@ -47,6 +47,7 @@ require_relative "instruction/block_yield" require_relative "instruction/resolve_method" require_relative "instruction/truth_check" require_relative "instruction/not_same_check" +require_relative "instruction/same_check" require_relative "instruction/jump" require_relative "instruction/return_jump" require_relative "instruction/slot_load" diff --git a/lib/slot_machine/instruction/check.rb b/lib/slot_machine/instruction/check.rb index 213e938a..54b9fc11 100644 --- a/lib/slot_machine/instruction/check.rb +++ b/lib/slot_machine/instruction/check.rb @@ -11,11 +11,15 @@ module SlotMachine # is met. This means that the asm implementation is somewhat the reverse # of the SlotMachine names. But it's easier to understand (imho) class Check < Instruction - attr_reader :false_jump - def initialize(false_jump) - @false_jump = false_jump - raise "Jump target must be a label #{false_jump}" unless false_jump.is_a?(Label) + attr_reader :false_label + + def initialize(false_label) + set_label(false_label) 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 diff --git a/lib/slot_machine/instruction/not_same_check.rb b/lib/slot_machine/instruction/not_same_check.rb index d967a501..cb083e12 100644 --- a/lib/slot_machine/instruction/not_same_check.rb +++ b/lib/slot_machine/instruction/not_same_check.rb @@ -26,7 +26,7 @@ module SlotMachine 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::IsZero.new( self, false_jump.risc_label(compiler)) + compiler.add_code Risc::IsZero.new( self, false_label.risc_label(compiler)) end end end diff --git a/lib/slot_machine/instruction/same_check.rb b/lib/slot_machine/instruction/same_check.rb new file mode 100644 index 00000000..3d1696cc --- /dev/null +++ b/lib/slot_machine/instruction/same_check.rb @@ -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 diff --git a/lib/slot_machine/instruction/truth_check.rb b/lib/slot_machine/instruction/truth_check.rb index b990dd71..61672053 100644 --- a/lib/slot_machine/instruction/truth_check.rb +++ b/lib/slot_machine/instruction/truth_check.rb @@ -8,18 +8,18 @@ module SlotMachine class TruthCheck < Check attr_reader :condition - def initialize(condition , false_jump) - super(false_jump) + def initialize(condition , false_label) + super(false_label) @condition = condition raise "condition must be slot_definition #{condition}" unless condition.is_a?(Slotted) end def to_s - "TruthCheck #{@condition} -> #{false_jump}" + "TruthCheck #{@condition} -> #{false_label}" end def to_risc(compiler) - false_label = @false_jump.risc_label(compiler) + false_label = @false_label.risc_label(compiler) builder = compiler.builder("TruthCheck") condition_reg = @condition.to_register(compiler,self) builder.build do diff --git a/test/sol/test_yield_statement.rb b/test/sol/test_yield_statement.rb index 78a2bb0f..ca93b614 100644 --- a/test/sol/test_yield_statement.rb +++ b/test/sol/test_yield_statement.rb @@ -15,7 +15,7 @@ module Sol end def test_check_label 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 def test_transfer assert_equal ArgumentTransfer, @ins.next(3).class @@ -74,7 +74,7 @@ module Sol end def test_check_label 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 def test_array check_array [NotSameCheck, SlotLoad, ResolveMethod, Label, MessageSetup ,