From 393f0d9a603b136e320b3f05a16599b4fe7e1325 Mon Sep 17 00:00:00 2001 From: Torsten Date: Thu, 27 Feb 2020 18:19:27 +0200 Subject: [PATCH] fix argument transfer (to be logical) --- lib/risc/builder.rb | 8 +++----- .../instruction/argument_transfer.rb | 10 ++++++---- lib/sol/send_statement.rb | 6 +----- lib/sol/yield_statement.rb | 6 +----- .../instruction/test_argument_transfer.rb | 20 ++++++++++++------- test/sol/class_send/test_send_class.rb | 2 +- test/sol/send/test_send_self.rb | 2 +- test/sol/send/test_send_simple_args.rb | 7 +++---- test/sol/test_yield_statement.rb | 6 ++---- 9 files changed, 31 insertions(+), 36 deletions(-) diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index b4600561..b857c145 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -4,7 +4,7 @@ module Risc # # The code is added to the method_compiler. # - # Basically this allows to many Risc instructions with extremely readable code. + # Basically this allows to express many Risc instructions with extremely readable code. # example: # space << Parfait.object_space # load constant # message[:receiver] << space #make current message's (r0) receiver the space @@ -15,7 +15,6 @@ module Risc attr_reader :built , :compiler , :names # pass a compiler, to which instruction are added (usually) - # second arg determines weather instructions are added (default true) # call build with a block to build def initialize(compiler, for_source) raise "no compiler" unless compiler @@ -27,10 +26,10 @@ module Risc end # make the magic: convert incoming names into registers that have the - # type set according to the name (using resolve_type) + # type set according to the name (using infer_type) # names are stored, so subsequent calls use the same register def method_missing(name , *args) - super if args.length != 0 + return super if args.length != 0 name = name.to_s return @names[name] if @names.has_key?(name) if name == "message" @@ -72,7 +71,6 @@ module Risc as_string = "word" if as_string == "name" as_string = "message" if as_string == "next_message" as_string = "message" if as_string == "caller" - as_string = "named_list" if as_string == "arguments" sym = as_string.camelise.to_sym clazz = Parfait.object_space.get_class_by_name(sym) raise "Not implemented/found object #{name}:#{sym}" unless clazz diff --git a/lib/slot_machine/instruction/argument_transfer.rb b/lib/slot_machine/instruction/argument_transfer.rb index 9ba17438..c9b35b0f 100644 --- a/lib/slot_machine/instruction/argument_transfer.rb +++ b/lib/slot_machine/instruction/argument_transfer.rb @@ -26,7 +26,7 @@ module SlotMachine super(source) @receiver , @arguments = receiver , arguments raise "Receiver not Slot #{@receiver}" unless @receiver.is_a?(Slotted) - @arguments.each{|a| raise "args not SlotLoad #{a}" unless a.is_a?(SlotLoad)} + @arguments.each{|a| raise "args not Slotted #{a}" unless a.is_a?(Slotted)} end def to_s @@ -38,11 +38,13 @@ module SlotMachine def to_risc(compiler) transfer = SlotLoad.new(self.source ,[:message , :next_message , :receiver] , @receiver, self).to_risc(compiler) #TODO transfer the Number of arguments to :arguments_given (to be checked on entry) - compiler.reset_regs - @arguments.each do |arg| - arg.to_risc(compiler) + arg_target = [:message , :next_message ] + @arguments.each_with_index do |arg , index| # +1 because of type compiler.reset_regs + load = SlotMachine::SlotLoad.new(self.source, arg_target + ["arg#{index+1}".to_sym] , arg) + load.to_risc(compiler) end + compiler.reset_regs transfer end end diff --git a/lib/sol/send_statement.rb b/lib/sol/send_statement.rb index a0cd9221..65483990 100644 --- a/lib/sol/send_statement.rb +++ b/lib/sol/send_statement.rb @@ -68,11 +68,7 @@ module Sol def message_setup(compiler,called_method) setup = SlotMachine::MessageSetup.new( called_method ) slot_receive = @receiver.to_slotted(compiler) - arg_target = [:message , :next_message ] - args = [] - @arguments.each_with_index do |arg , index| # +1 because of type - args << SlotMachine::SlotLoad.new(self, arg_target + ["arg#{index+1}".to_sym] , arg.to_slotted(compiler)) - end + args = @arguments.collect { |arg| arg.to_slotted(compiler)} setup << SlotMachine::ArgumentTransfer.new(self, slot_receive , args ) end diff --git a/lib/sol/yield_statement.rb b/lib/sol/yield_statement.rb index df49fb67..584b7b78 100644 --- a/lib/sol/yield_statement.rb +++ b/lib/sol/yield_statement.rb @@ -50,11 +50,7 @@ module Sol arg_index = compiler.get_method.arguments_type.get_length - 1 setup = SlotMachine::MessageSetup.new( arg_index ) slot_receive = @receiver.to_slotted(compiler) - arg_target = [:message , :next_message ] - args = [] - @arguments.each_with_index do |arg , index| # +1 because of type - args << SlotMachine::SlotLoad.new(self, arg_target + ["arg#{index+1}".to_sym] , arg.to_slotted(compiler)) - end + args = @arguments.collect { |arg| arg.to_slotted(compiler)} setup << SlotMachine::ArgumentTransfer.new( self , slot_receive , args ) setup << SlotMachine::BlockYield.new( self , arg_index ) end diff --git a/test/slot_machine/instruction/test_argument_transfer.rb b/test/slot_machine/instruction/test_argument_transfer.rb index b9872ccd..ab7ac776 100644 --- a/test/slot_machine/instruction/test_argument_transfer.rb +++ b/test/slot_machine/instruction/test_argument_transfer.rb @@ -3,15 +3,15 @@ require_relative "helper" module SlotMachine class TestArgumentTransfer < SlotMachineInstructionTest def instruction - receiver = SlottedMessage.new( [:receiver]) - arg = SlotLoad.new("test", [:message, :caller] , [:message,:type] ) + receiver = SlottedMessage.new( [:arg1]) + arg = SlottedMessage.new( [:receiver , :type]) ArgumentTransfer.new("" , receiver ,[arg]) end def test_len - assert_equal 6 , all.length , all_str + assert_equal 8 , all.length , all_str end def test_1_slot - assert_slot_to_reg risc(1) ,:r0 , 2 , :r2 + assert_slot_to_reg risc(1) ,:r0 , 9 , :r2 end def test_2_slot assert_slot_to_reg risc(2) ,:r0 , 1 , :r3 @@ -20,10 +20,16 @@ module SlotMachine assert_reg_to_slot risc(3) , :r2 , :r3 , 2 end def test_4_slot - assert_slot_to_reg risc(4) ,:r0 , 0 , :r2 + assert_slot_to_reg risc(4) ,:r0 , 2 , :r2 end - def test_5_reg - assert_reg_to_slot risc(5) , :r2 , :r0 , 6 + def test_5 + assert_slot_to_reg risc(5) ,:r2 , 0 , :r2 + end + def test_6 + assert_slot_to_reg risc(6) ,:r0 , 1 , :r3 + end + def test_7 + assert_reg_to_slot risc(7) , :r2 , :r3 , 9 end end end diff --git a/test/sol/class_send/test_send_class.rb b/test/sol/class_send/test_send_class.rb index 5a64e8ff..1e4ba0c2 100644 --- a/test/sol/class_send/test_send_class.rb +++ b/test/sol/class_send/test_send_class.rb @@ -45,7 +45,7 @@ module Sol assert_equal Parfait::Class, @ins.next.receiver.known_object.class end def test_arg_one - assert_equal SlotLoad, @ins.next(1).arguments[0].class + assert_equal SlottedConstant, @ins.next(1).arguments[0].class end def test_receiver_move_class assert_equal ArgumentTransfer, @ins.next(1).class diff --git a/test/sol/send/test_send_self.rb b/test/sol/send/test_send_self.rb index 9b4fa42f..6f93fecf 100644 --- a/test/sol/send/test_send_self.rb +++ b/test/sol/send/test_send_self.rb @@ -11,7 +11,7 @@ module Sol assert_equal SlottedMessage, @ins.next.receiver.class end def test_arg_one - assert_equal SlotLoad, @ins.next(1).arguments[0].class + assert_equal SlottedConstant, @ins.next(1).arguments[0].class end def test_call_two assert_equal SimpleCall, @ins.next(2).class diff --git a/test/sol/send/test_send_simple_args.rb b/test/sol/send/test_send_simple_args.rb index 1fa1a0da..e5ac2170 100644 --- a/test/sol/send/test_send_simple_args.rb +++ b/test/sol/send/test_send_simple_args.rb @@ -12,12 +12,11 @@ module Sol [SlotMachine::IntegerConstant , 5] end def test_args_two_move - assert_equal :next_message, @ins.next(1).arguments[1].left.slots.name - assert_equal :arg2, @ins.next(1).arguments[1].left.slots.next_slot.name + assert_equal SlottedConstant, @ins.next(1).arguments[1].class end def test_args_two_str - assert_equal SlotMachine::IntegerConstant, @ins.next(1).arguments[1].right.known_object.class - assert_equal 2, @ins.next(1).arguments[1].right.known_object.value + assert_equal SlottedConstant, @ins.next(1).arguments[1].class + assert_equal 2, @ins.next(1).arguments[1].known_object.value end def test_array check_array [MessageSetup,ArgumentTransfer,SimpleCall, SlotLoad, ReturnJump, diff --git a/test/sol/test_yield_statement.rb b/test/sol/test_yield_statement.rb index ca93b614..91a0c88d 100644 --- a/test/sol/test_yield_statement.rb +++ b/test/sol/test_yield_statement.rb @@ -22,10 +22,8 @@ module Sol assert_equal 1, @ins.next(3).arguments.length end def test_args_one_l - left = @ins.next(3).arguments[0].left - assert_equal Symbol, left.known_object.class - assert_equal :message, left.known_object - assert_equal "message.next_message.arg1", left.to_s + left = @ins.next(3).arguments[0] + assert_equal SlotMachine::IntegerConstant, left.known_object.class end def test_check_left assert_equal SlottedObject, @ins.left.class