From f85fe8a2cb2db3ca89caf70ba99b7c18b61eed80 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 19 Aug 2018 15:36:51 +0300 Subject: [PATCH] fix bug in slot_load and definition move parfait helper for reuse --- lib/mom/instruction/slot_definition.rb | 4 +- lib/mom/instruction/slot_load.rb | 3 +- lib/risc/instructions/setter.rb | 5 +-- test/mom/instruction/test_slot_load2.rb | 4 +- test/mom/instruction/test_slot_load3.rb | 50 +++++++++++++++++++++++++ test/parfait/helper.rb | 16 -------- test/support/parfait_test.rb | 15 ++++++++ 7 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 test/mom/instruction/test_slot_load3.rb create mode 100644 test/support/parfait_test.rb diff --git a/lib/mom/instruction/slot_definition.rb b/lib/mom/instruction/slot_definition.rb index 51801aea..1aa058d2 100644 --- a/lib/mom/instruction/slot_definition.rb +++ b/lib/mom/instruction/slot_definition.rb @@ -65,8 +65,8 @@ module Mom compiler.add_code const raise "Can't have slots into Constants" if slots.length > 0 when Parfait::Object , Risc::Label - const = Risc::SlotToReg.new( source , right ,index, right) - compiler.add_code const Risc.load_constant(source, known_object , right) + const = const = Risc.load_constant(source, known_object , right) + compiler.add_code const if slots.length > 0 # desctructively replace the existing value to be loaded if more slots compiler.add_code Risc.slot_to_reg( source , right ,slots[0], right) diff --git a/lib/mom/instruction/slot_load.rb b/lib/mom/instruction/slot_load.rb index ea9a1334..00d873c7 100644 --- a/lib/mom/instruction/slot_load.rb +++ b/lib/mom/instruction/slot_load.rb @@ -39,7 +39,6 @@ module Mom end def to_risc(compiler) - #puts "RISC #{self}" const_reg = @right.to_register(compiler , original_source) left_slots = @left.slots case @left.known_object @@ -48,7 +47,7 @@ module Mom when Parfait::CacheEntry left = compiler.use_reg( :CacheEntry ) compiler.add_code Risc.load_constant(original_source, @left.known_object , left) - compiler.add_code Risc.reg_to_slot(original_source, const.register , left, left_slots.first) + compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, left_slots.first) else raise "We have left #{@left.known_object}" end diff --git a/lib/risc/instructions/setter.rb b/lib/risc/instructions/setter.rb index 8a99f159..af529ab6 100644 --- a/lib/risc/instructions/setter.rb +++ b/lib/risc/instructions/setter.rb @@ -21,10 +21,9 @@ module Risc @register = register @array = array @index = index -# raise "index 0 " if index < 0 - raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) + raise "Not integer or reg index #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) raise "Not register #{register}" unless RegisterValue.look_like_reg(register) - raise "Not register #{array}" unless RegisterValue.look_like_reg(array) + raise "Not slot/register #{array}" unless RegisterValue.look_like_reg(array) end attr_accessor :register , :array , :index diff --git a/test/mom/instruction/test_slot_load2.rb b/test/mom/instruction/test_slot_load2.rb index e7e4b895..1d7dad14 100644 --- a/test/mom/instruction/test_slot_load2.rb +++ b/test/mom/instruction/test_slot_load2.rb @@ -11,12 +11,14 @@ module Mom @instructions = @compiler.instructions end - def test_ins_next_class + def test_ins_next_classes + assert_equal Risc::SlotToReg , @instructions[0].class assert_equal Risc::SlotToReg , @instructions[1].class assert_equal Risc::SlotToReg , @instructions[2].class end def test_ins_next_next_class assert_equal Risc::RegToSlot , @instructions[3].class + assert_equal NilClass , @instructions[4].class end def test_ins_next_reg diff --git a/test/mom/instruction/test_slot_load3.rb b/test/mom/instruction/test_slot_load3.rb new file mode 100644 index 00000000..e7fac0ef --- /dev/null +++ b/test/mom/instruction/test_slot_load3.rb @@ -0,0 +1,50 @@ +require_relative "helper" + +module Mom + class TestSlotLoad3 < Parfait::ParfaitTest + + def setup + super + method = make_method + @compiler = Risc::FakeCompiler.new + @cache_entry = Parfait::CacheEntry.new(method.frame_type, method) + load = SlotLoad.new( [@cache_entry , :cached_type] , [:message, :type] ) + load.to_risc(@compiler) + @instructions = @compiler.instructions + end + def test_ins_next_class + assert_equal Risc::SlotToReg , @instructions[0].class + assert_equal Risc::LoadConstant, @instructions[1].class + end + + def test_ins_next_class + assert_equal Risc::RegToSlot , @instructions[2].class + assert_equal NilClass , @instructions[3].class + end + def test_ins_load + assert_equal :r1 , @instructions[1].register.symbol + assert_equal Parfait::CacheEntry , @instructions[1].constant.class + end + + def test_ins_next_reg + assert_equal :r1 , @instructions[0].register.symbol + end + def test_ins_next_arr + assert_equal :r0 , @instructions[0].array.symbol + end + def test_ins_next_index + assert_equal 0 , @instructions[0].index + end + + def test_ins_next_2_reg + assert_equal :r1 , @instructions[2].register.symbol + end + def test_ins_next_2_arr + assert_equal :r1 , @instructions[2].array.symbol + end + def test_ins_next_2_index + assert_equal 1 , @instructions[2].index + end + + end +end diff --git a/test/parfait/helper.rb b/test/parfait/helper.rb index 62193fa9..26258082 100644 --- a/test/parfait/helper.rb +++ b/test/parfait/helper.rb @@ -1,17 +1 @@ require_relative "../helper" - -module Parfait - class ParfaitTest < MiniTest::Test - - def setup - Parfait.boot! - @space = Parfait.object_space - end - def make_method - @obj = Parfait.object_space.get_type_by_class_name(:Object) - @args = Parfait::Type.for_hash( @obj.object_class , { bar: :Integer , foo: :Type}) - @frame = Parfait::Type.for_hash( @obj.object_class , { local_bar: :Integer , local_foo: :Type}) - @method = Parfait::CallableMethod.new( :meth , @obj , @args , @frame) - end - end -end diff --git a/test/support/parfait_test.rb b/test/support/parfait_test.rb new file mode 100644 index 00000000..a79ece1f --- /dev/null +++ b/test/support/parfait_test.rb @@ -0,0 +1,15 @@ +module Parfait + class ParfaitTest < MiniTest::Test + + def setup + Parfait.boot! + @space = Parfait.object_space + end + def make_method + @obj = Parfait.object_space.get_type_by_class_name(:Object) + @args = Parfait::Type.for_hash( @obj.object_class , { bar: :Integer , foo: :Type}) + @frame = Parfait::Type.for_hash( @obj.object_class , { local_bar: :Integer , local_foo: :Type}) + @method = Parfait::CallableMethod.new( :meth , @obj , @args , @frame) + end + end +end