diff --git a/lib/risc/instructions/slot_to_reg.rb b/lib/risc/instructions/slot_to_reg.rb index 709375f1..e7e4ffcc 100644 --- a/lib/risc/instructions/slot_to_reg.rb +++ b/lib/risc/instructions/slot_to_reg.rb @@ -19,10 +19,11 @@ module Risc # a new regsister will be created as the result, ie the reg part for slot_to_reg def self.slot_to_reg( source , array , index ) raise "Register #{array}" if RegisterValue.look_like_reg(array.symbol) + new_name = "#{array.symbol}.#{index.to_s.downcase}".to_sym index = array.resolve_index(index) if index.is_a?(Symbol) type = array.type_at(index) #puts "Slot for #{array.symbol}@ index #{index} is #{type}" - to = RegisterValue.new( "#{array.symbol}.#{type.to_s.downcase}".to_sym , type ) + to = RegisterValue.new( new_name , type ) SlotToReg.new( source , array , index , to) end end diff --git a/lib/risc/register_slot.rb b/lib/risc/register_slot.rb index dc23248a..63b11fee 100644 --- a/lib/risc/register_slot.rb +++ b/lib/risc/register_slot.rb @@ -25,17 +25,25 @@ module Risc def <<( reg ) case reg when RegisterValue - puts reg.symbol to_mem("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg) when RegisterSlot - puts reg.register.symbol reg = to_reg("reduce #{@register.symbol}[@index]") - to_mem("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg) + to_mem("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg) else raise "not reg value or slot #{reg}" end end + # for chaining the array operator is defined here too. + # It basically reduces the slot to a register and applies the [] on that reg. + # thus returning a new RegisterSlot. + # Example: message[:caller][:next_message] + # message[:caller] returns a RegisterSlot, which would be self for this example + # to evaluate self[:next_message] we reduce self to a register with to_reg + def [](index) + reg = to_reg("reduce #{@register.symbol}[@index]") + reg[index] + end # push the given register into the slot that self represents # ie create a slot_to_reg instruction and add to the compiler # the register represents and "array", and the content of the @@ -47,7 +55,7 @@ module Risc end # load the conntent of the slot that self descibes into a a new register. - # the regsiter is created, and the slot_to_reg instruction added to the + # the register is created, and the slot_to_reg instruction added to the # compiler. the return is a bit like @register[@index] def to_reg(source ) slot_to_reg = Risc.slot_to_reg(source , register, index) diff --git a/lib/risc/register_value.rb b/lib/risc/register_value.rb index 20ba211b..b2686a84 100644 --- a/lib/risc/register_value.rb +++ b/lib/risc/register_value.rb @@ -82,7 +82,6 @@ module Risc new_type , extra = compiler.slot_type(slot , type) unless new_type new_name = "#{@symbol}.#{slot}" raise "no #{self}" if RegisterValue.look_like_reg(@symbol) - puts "New name #{new_name}" new_left = RegisterValue.new( new_name.to_sym , new_type , extra) new_left end diff --git a/lib/slot_machine/instruction/basic_values.rb b/lib/slot_machine/basic_values.rb similarity index 100% rename from lib/slot_machine/instruction/basic_values.rb rename to lib/slot_machine/basic_values.rb diff --git a/lib/slot_machine/instruction.rb b/lib/slot_machine/instruction.rb index 75175686..eadfe5fd 100644 --- a/lib/slot_machine/instruction.rb +++ b/lib/slot_machine/instruction.rb @@ -38,9 +38,9 @@ module SlotMachine end +require_relative "basic_values" require_relative "instruction/label" require_relative "instruction/check" -require_relative "instruction/basic_values" require_relative "instruction/simple_call" require_relative "instruction/dynamic_call" require_relative "instruction/block_yield" diff --git a/lib/slot_machine/instruction/argument_transfer.rb b/lib/slot_machine/instruction/argument_transfer.rb index c9b35b0f..9f9dd323 100644 --- a/lib/slot_machine/instruction/argument_transfer.rb +++ b/lib/slot_machine/instruction/argument_transfer.rb @@ -40,11 +40,9 @@ module SlotMachine #TODO transfer the Number of arguments to :arguments_given (to be checked on entry) 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/slot_machine/instruction/slot_load.rb b/lib/slot_machine/instruction/slot_load.rb index 722ebcdd..79718d8a 100644 --- a/lib/slot_machine/instruction/slot_load.rb +++ b/lib/slot_machine/instruction/slot_load.rb @@ -48,7 +48,6 @@ module SlotMachine def to_risc(compiler) const_reg = @right.to_register(compiler , original_source) @left.reduce_and_load(const_reg , compiler , original_source ) - compiler.reset_regs end end diff --git a/lib/slot_machine/slotted_object.rb b/lib/slot_machine/slotted_object.rb index ad373e31..1d29259f 100644 --- a/lib/slot_machine/slotted_object.rb +++ b/lib/slot_machine/slotted_object.rb @@ -42,8 +42,9 @@ module SlotMachine # most likely be united def reduce_and_load(const_reg , compiler , original_source ) raise "only cache" unless known_object.is_a?( Parfait::CacheEntry) - left = compiler.use_reg( :CacheEntry ) - compiler.add_code Risc.load_constant(original_source, known_object , left) + load = Risc.load_constant(original_source, known_object ) + left = load.register + compiler.add_code load compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slots.name) end end diff --git a/test/risc/test_register_slot.rb b/test/risc/test_register_slot.rb index d429c7d6..70bad648 100644 --- a/test/risc/test_register_slot.rb +++ b/test/risc/test_register_slot.rb @@ -15,10 +15,11 @@ module Risc end def test_reg_to_slot_inst @r0[:next_message] << @r0 + inst = @compiler.current assert_equal RegToSlot , @compiler.current.class - assert_equal @r0 , @compiler.current.register - assert_equal 1 , @compiler.current.index - assert_equal :message , @compiler.current.array.symbol + assert_equal @r0 , inst.register + assert_equal 1 , inst.index + assert_equal :message , inst.array.symbol end end class TestRegisterSlot2 < MiniTest::Test @@ -30,14 +31,14 @@ module Risc def test_reg_to_slot_reg reg = @r0[:next_message] << @r0[:next_message] assert_equal RegisterValue , reg.class - assert_equal :"message.message" , reg.symbol + assert_equal :"message.next_message" , reg.symbol assert_equal "Message_Type" , reg.type.name end def test_reg_to_slot_inst1 @r0[:next_message] << @r0[:next_message] inst = @compiler.risc_instructions.next assert_equal SlotToReg , inst.class - assert_equal :"message.message" , inst.register.symbol + assert_equal :"message.next_message" , inst.register.symbol assert_equal 1 , inst.index assert_equal :message , inst.array.symbol end @@ -45,7 +46,28 @@ module Risc @r0[:next_message] << @r0[:next_message] inst = @compiler.current assert_equal RegToSlot , inst.class - assert_equal :"message.message" , inst.register.symbol + assert_equal :"message.next_message" , inst.register.symbol + assert_equal 1 , inst.index + assert_equal :message , inst.array.symbol + end + end + class TestRegisterSlot3 < MiniTest::Test + def setup + Parfait.boot!(Parfait.default_test_options) + @compiler = Risc.test_compiler + @r0 = RegisterValue.new(:message , :Message).set_compiler(@compiler) + end + def test_arr_reg + slot = @r0[:next_message][:type] + assert_equal RegisterSlot , slot.class + assert_equal :"message.next_message" , slot.register.symbol + assert_equal "Message_Type" , slot.register.type.name + end + def test_arr_inst + slot = @r0[:next_message][:type] + inst = @compiler.current + assert_equal SlotToReg , inst.class + assert_equal :"message.next_message" , inst.register.symbol assert_equal 1 , inst.index assert_equal :message , inst.array.symbol end diff --git a/test/slot_machine/instruction/test_argument_transfer.rb b/test/slot_machine/instruction/test_argument_transfer.rb index ab7ac776..d8799062 100644 --- a/test/slot_machine/instruction/test_argument_transfer.rb +++ b/test/slot_machine/instruction/test_argument_transfer.rb @@ -11,25 +11,25 @@ module SlotMachine assert_equal 8 , all.length , all_str end def test_1_slot - assert_slot_to_reg risc(1) ,:r0 , 9 , :r2 + assert_slot_to_reg risc(1) ,:message , 9 , :"message.arg1" end def test_2_slot - assert_slot_to_reg risc(2) ,:r0 , 1 , :r3 + assert_slot_to_reg risc(2) ,:message , 1 , :"message.next_message" end def test_3_reg - assert_reg_to_slot risc(3) , :r2 , :r3 , 2 + assert_reg_to_slot risc(3) , :"message.arg1" , :"message.next_message" , 2 end def test_4_slot - assert_slot_to_reg risc(4) ,:r0 , 2 , :r2 + assert_slot_to_reg risc(4) ,:message , 2 , :"message.receiver" end def test_5 - assert_slot_to_reg risc(5) ,:r2 , 0 , :r2 + assert_slot_to_reg risc(5) ,:"message.receiver" , 0 , :"message.receiver.type" end def test_6 - assert_slot_to_reg risc(6) ,:r0 , 1 , :r3 + assert_slot_to_reg risc(6) ,:message , 1 , :"message.next_message" end def test_7 - assert_reg_to_slot risc(7) , :r2 , :r3 , 9 + assert_reg_to_slot risc(7) , :"message.receiver.type" , :"message.next_message" , 9 end end end diff --git a/test/slot_machine/instruction/test_not_same_check.rb b/test/slot_machine/instruction/test_not_same_check.rb index 8325d3bc..e4af014e 100644 --- a/test/slot_machine/instruction/test_not_same_check.rb +++ b/test/slot_machine/instruction/test_not_same_check.rb @@ -4,19 +4,20 @@ module SlotMachine class TestNotSameCheck < SlotMachineInstructionTest def instruction target = Slotted.for(:message , [:caller]) - NotSameCheck.new(target , target , Label.new("ok" , "target")) + target2 = Slotted.for(:message , [:next_message]) + NotSameCheck.new(target , target2 , Label.new("ok" , "target")) end def test_len assert_equal 5 , all.length , all_str end def test_1_slot - assert_slot_to_reg risc(1) ,:r0 , 6 , :r2 + assert_slot_to_reg risc(1) ,:message , 6 , :"message.caller" end def test_2_slot - assert_slot_to_reg risc(2) ,:r0 , 6 , :r4 + assert_slot_to_reg risc(2) ,:message , 1 , :"message.next_message" end def test_3_op - assert_operator risc(3) , :-, :r2 , :r4 + assert_operator risc(3) , :-, :"message.caller" , :"message.next_message" end def test_4_zero assert_zero risc(4) , "target" diff --git a/test/slot_machine/instruction/test_slot_load1.rb b/test/slot_machine/instruction/test_slot_load1.rb index f600d53a..29f253a7 100644 --- a/test/slot_machine/instruction/test_slot_load1.rb +++ b/test/slot_machine/instruction/test_slot_load1.rb @@ -18,19 +18,19 @@ module SlotMachine assert_equal Risc::RegToSlot , @instructions.next.class end def test_ins_arr - assert_equal :r0 , @instructions.array.symbol + assert_equal :message , @instructions.array.symbol end def test_ins_reg - assert_equal :r2 , @instructions.register.symbol + assert_equal :"message.type" , @instructions.register.symbol end def test_ins_index assert_equal 0 , @instructions.index end def test_ins_next_reg - assert_equal :r2 , @instructions.next.register.symbol + assert_equal :"message.type" , @instructions.next.register.symbol end def test_ins_next_arr - assert_equal :r0 , @instructions.next.array.symbol + assert_equal :message , @instructions.next.array.symbol end def test_ins_next_index assert_equal 6 , @instructions.next.index diff --git a/test/slot_machine/instruction/test_slot_load2.rb b/test/slot_machine/instruction/test_slot_load2.rb index 1a51d4eb..e57184ff 100644 --- a/test/slot_machine/instruction/test_slot_load2.rb +++ b/test/slot_machine/instruction/test_slot_load2.rb @@ -21,28 +21,28 @@ module SlotMachine assert_equal NilClass , @instructions.next(4).class end def test_ins_next_reg - assert_equal :r2 , @instructions.next.register.symbol + assert_equal :"message.caller.type" , @instructions.next.register.symbol end def test_ins_next_arr - assert_equal :r2 , @instructions.next.array.symbol + assert_equal :"message.caller" , @instructions.next.array.symbol end def test_ins_next_index assert_equal 0 , @instructions.next.index end def test_ins_next_2_reg - assert_equal :r3 , @instructions.next(2).register.symbol + assert_equal :"message.caller" , @instructions.next(2).register.symbol end def test_ins_next_2_arr - assert_equal :r0 , @instructions.next(2).array.symbol + assert_equal :"message" , @instructions.next(2).array.symbol end def test_ins_next_2_index assert_equal 6 , @instructions.next(2).index end def test_ins_next_3_reg - assert_equal :r2 , @instructions.next(3).register.symbol + assert_equal :"message.caller.type" , @instructions.next(3).register.symbol end def test_ins_next_3_arr - assert_equal :r3 , @instructions.next(3).array.symbol + assert_equal :"message.caller" , @instructions.next(3).array.symbol end def test_ins_next_3_index assert_equal 0 , @instructions.next(3).index diff --git a/test/slot_machine/instruction/test_slot_load3.rb b/test/slot_machine/instruction/test_slot_load3.rb index 18c46f1e..e8e18599 100644 --- a/test/slot_machine/instruction/test_slot_load3.rb +++ b/test/slot_machine/instruction/test_slot_load3.rb @@ -23,25 +23,25 @@ module SlotMachine assert_equal NilClass , @instructions.next(3).class end def test_ins_load - assert_equal :r3 , @instructions.next.register.symbol + assert @instructions.next.register.is_object? assert_equal Parfait::CacheEntry , @instructions.next.constant.class end def test_ins_next_reg - assert_equal :r2 , @instructions.register.symbol + assert_equal :"message.type" , @instructions.register.symbol end def test_ins_next_arr - assert_equal :r0 , @instructions.array.symbol + assert_equal :message , @instructions.array.symbol end def test_ins_next_index assert_equal 0 , @instructions.index end def test_ins_next_2_reg - assert_equal :r2 , @instructions.next(2).register.symbol + assert_equal :"message.type" , @instructions.next(2).register.symbol end def test_ins_next_2_arr - assert_equal :r3 , @instructions.next(2).array.symbol + assert @instructions.next(2).array.is_object? end def test_ins_next_2_index assert_equal 1 , @instructions.next(2).index