From 047a36178fadf19f6c9a5fe052371b7685966178 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 19 Aug 2018 12:56:44 +0300 Subject: [PATCH] start with #11 by adding striaght to compiler --- lib/mom/instruction/slot_definition.rb | 28 +++++++++++-------- test/mom/instruction/test_slot_definition1.rb | 3 +- test/mom/instruction/test_slot_definition2.rb | 5 ++-- test/mom/instruction/test_slot_definition3.rb | 12 ++++---- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/mom/instruction/slot_definition.rb b/lib/mom/instruction/slot_definition.rb index 322361e1..51801aea 100644 --- a/lib/mom/instruction/slot_definition.rb +++ b/lib/mom/instruction/slot_definition.rb @@ -45,6 +45,10 @@ module Mom "unknown" end end + + # load the slots into a register + # the code is added to compiler + # the register returned def to_register(compiler, source) if known_object.respond_to?(:ct_type) type = known_object.ct_type @@ -57,13 +61,15 @@ module Mom case known_object when Constant parfait = known_object.to_parfait(compiler) - const = Risc.load_constant(source, parfait , right) + const = Risc.load_constant(source, parfait , right) + compiler.add_code const raise "Can't have slots into Constants" if slots.length > 0 when Parfait::Object , Risc::Label - const = Risc.load_constant(source, known_object , right) + const = Risc::SlotToReg.new( source , right ,index, right) + compiler.add_code const Risc.load_constant(source, known_object , right) if slots.length > 0 # desctructively replace the existing value to be loaded if more slots - const << Risc.slot_to_reg( source , right ,slots[0], right) + compiler.add_code Risc.slot_to_reg( source , right ,slots[0], right) end when Symbol return sym_to_risc(compiler , source) @@ -73,29 +79,27 @@ module Mom if slots.length > 1 # desctructively replace the existing value to be loaded if more slots index = Risc.resolve_to_index(slots[0] , slots[1] ,compiler) - const << Risc::SlotToReg.new( source , right ,index, right) + compiler.add_code Risc::SlotToReg.new( source , right ,index, right) if slots.length > 2 raise "3 slots only for type #{slots}" unless slots[2] == :type - const << Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right) + compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right) end end - const + return const.register end # resolve the slots one by one to slot_to_reg instructions using the # type information inferred from their names / type hierachy def sym_to_risc(compiler , source) - #label just to collect the instructions - # (they should be added to the compiler/builder anyway) - instructions = Risc.label( "" , "tmp") slots = @slots.dup raise "Not Message #{object}" unless @known_object == :message left = Risc.message_reg - left = left.resolve_and_add( slots.shift , instructions , compiler) + left = left.resolve_and_add( slots.shift , compiler) + reg = compiler.current.register while( !slots.empty? ) - left = left.resolve_and_add( slots.shift , instructions , compiler) + left = left.resolve_and_add( slots.shift , compiler) end - return instructions.next + return reg end end diff --git a/test/mom/instruction/test_slot_definition1.rb b/test/mom/instruction/test_slot_definition1.rb index 9d3d6280..4b5a5392 100644 --- a/test/mom/instruction/test_slot_definition1.rb +++ b/test/mom/instruction/test_slot_definition1.rb @@ -7,7 +7,8 @@ module Mom Parfait.boot! @compiler = Risc::FakeCompiler.new @definition = SlotDefinition.new(StringConstant.new("hi") , []) - @instruction = @definition.to_register(@compiler , InstructionMock.new) + @register = @definition.to_register(@compiler , InstructionMock.new) + @instruction = @compiler.instructions.first end def test_def_class assert_equal Risc::LoadConstant , @instruction.class diff --git a/test/mom/instruction/test_slot_definition2.rb b/test/mom/instruction/test_slot_definition2.rb index 4b3a963b..94ab0943 100644 --- a/test/mom/instruction/test_slot_definition2.rb +++ b/test/mom/instruction/test_slot_definition2.rb @@ -6,7 +6,8 @@ module Mom Parfait.boot! @compiler = Risc::FakeCompiler.new @definition = SlotDefinition.new(:message , :caller) - @instruction = @definition.to_register(@compiler , InstructionMock.new) + @register = @definition.to_register(@compiler , "fake source") + @instruction = @compiler.instructions.first end def test_def_class assert_equal Risc::SlotToReg , @instruction.class @@ -18,7 +19,7 @@ module Mom assert_equal :r0 , @instruction.array.symbol end def test_def_register # to next free register r1 - assert_equal :r1 , @instruction.register.symbol + assert_equal :r1 , @register.symbol end def test_def_index # at caller index 6 assert_equal 6 , @instruction.index diff --git a/test/mom/instruction/test_slot_definition3.rb b/test/mom/instruction/test_slot_definition3.rb index 3d8153e5..9b3f5e39 100644 --- a/test/mom/instruction/test_slot_definition3.rb +++ b/test/mom/instruction/test_slot_definition3.rb @@ -6,22 +6,22 @@ module Mom Parfait.boot! @compiler = Risc::FakeCompiler.new @definition = SlotDefinition.new(:message , [:caller , :type]) - @instruction = @definition.to_register(@compiler , InstructionMock.new) + @register = @definition.to_register(@compiler , InstructionMock.new) end def test_def_next_class - assert_equal Risc::SlotToReg , @instruction.next.class + assert_equal Risc::SlotToReg , @compiler.instructions[1].class end def test_def_next_next_class - assert_equal NilClass , @instruction.next.next.class + assert_equal NilClass , @compiler.instructions[2].class end def test_def_next_index - assert_equal 0 , @instruction.next.index + assert_equal 0 , @compiler.instructions[1].index end def test_def_next_register - assert_equal :r1 , @instruction.next.register.symbol + assert_equal :r1 , @compiler.instructions[1].register.symbol end def test_def_next_array - assert_equal :r1 , @instruction.next.array.symbol + assert_equal :r1 , @compiler.instructions[1].array.symbol end end end