start to move slot_load code to register_value

the iea is to iterate through register_values while reducing the slot_load to a number of Slot_to_regs
wip
This commit is contained in:
Torsten Ruger 2018-07-13 21:56:55 +03:00
parent 21009b0e9b
commit 61c840c023
5 changed files with 149 additions and 29 deletions

View File

@ -22,7 +22,7 @@ module Mom
# SlotDefinition (see there)
#
# @right: A SlotDefinition with slots or a Mom::Constant
# original_source: optinally another mom instrucion that wil be passed down to created
# original_source: optinally another mom instruction that wil be passed down to created
# risc instructions. (Because SlotLoad is often used internally in mom)
class SlotLoad < Instruction
attr_reader :left , :right , :original_source
@ -39,38 +39,59 @@ module Mom
end
def to_risc(compiler)
#puts "RISC #{self}"
const = @right.to_register(compiler , original_source)
left_slots = @left.slots
case @left.known_object
when Symbol
left = Risc.message_reg
left_index = Risc.resolve_to_index(@left.known_object , left_slots.first)
if left_slots.length > 1
# swap the existing target (with a new reg) and update the index
new_left = compiler.use_reg( :Object )
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
left = new_left
left_index = Risc.resolve_to_index(left_slots[0] , left_slots[1] ,compiler)
if left_slots.length > 2
#same again, once more updating target
new_left = compiler.use_reg( :Object )
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
left = new_left
left_index = Risc.resolve_to_index(left_slots[1] , left_slots[2] ,compiler)
end
raise "more slots not implemented #{left_slots}" if left_slots.length > 3
end
sym_to_risc(compiler , const)
when Parfait::CacheEntry
left = compiler.use_reg( :Object )
const << Risc.load_constant(original_source, @left.known_object , left)
left_index = Risc.resolve_to_index(:cache_entry , left_slots.first)
const << Risc.reg_to_slot(original_source, const.register , left, left_index)
else
raise "We have left #{@left.known_object}"
end
const << Risc.reg_to_slot(original_source, const.register , left, left_index)
compiler.reset_regs
return const
end
def sym_to_risc(compiler , const)
left_slots = @left.slots
raise "Not Message #{object}" unless @left.known_object == :message
left = Risc.message_reg
slot = left_slots.shift
left = left.resolve_and_add( slot , const , compiler)
#left_index = Risc.resolve_to_index(@left.known_object , left_slots.first)
const << Risc.reg_to_slot(original_source, const.register , left, left_index)
end
def old_sym_to_risc(compiler , const)
left = Risc.message_reg
left_slots = @left.slots
left_index = Risc.resolve_to_index(@left.known_object , left_slots.first)
if left_slots.length > 1
# swap the existing target (with a new reg) and update the index
new_left = compiler.use_reg( :Object )
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
left = new_left
left_index = Risc.resolve_to_index(left_slots[0] , left_slots[1] ,compiler)
if left_slots.length > 2
#same again, once more updating target
new_left = compiler.use_reg( :Object )
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
left = new_left
left_index = Risc.resolve_to_index(left_slots[1] , left_slots[2] ,compiler)
end
raise "more slots not implemented #{left_slots}" if left_slots.length > 3
end
const << Risc.reg_to_slot(original_source, const.register , left, left_index)
end
end
end

View File

@ -15,6 +15,47 @@ module Risc
@value = value
end
# using the registers type, resolve the slot to an index
# Using the index and the register, add a SlotToReg to the instruction
def resolve_and_add(slot , instruction , compiler)
index = resolve_index( slot )
new_left = get_new_left( slot , compiler )
instruction << Risc::SlotToReg.new( "SlotLoad #{type}[#{slot}]" , @symbol ,index, new_left)
new_left
end
# resolve the given slot name (instance variable name) to an index using the type
# RegisterValue has the current type, so we just look up the index in the type
def resolve_index(slot)
#puts "TYPE #{type} obj:#{object} var:#{slot} comp:#{compiler}"
type = Parfait.object_space.get_type_by_class_name(@type)
index = type.variable_index(slot)
raise "Index not found for #{slot} in #{type} of type #{@type}" unless index
return index
end
# when following variables in resolve_and_add, get a new RegisterValue
# that represents the new value.
# Ie in "normal case" a the same register, with the type of the slot
# (the not normal case, the first reduction, uses a new register, as we don't
# overwrite the message)
# We get the type with resolve_new_type
def get_new_left(slot, compiler)
new_type = resolve_new_type(slot , compiler)
if( @symbol == :r0 )
new_left = compiler.use_reg( new_type )
else
new_left = RegisterValue.new( @symbol , new_type)
end
new_left
end
# resolve the type of the slot, by inferring from it's name
# Currently this is implemented in Risc.resolve_type , but code shoudl be moved here
def resolve_new_type(slot, compiler)
Risc.resolve_type(slot , compiler)
end
def to_s
s = "#{symbol}:#{type}"
s += ":#{value}" if value

View File

@ -5,17 +5,17 @@ module Mom
def setup
Parfait.boot!
Risc.boot!
@load = SlotLoad.new( [:message, :caller] , [:message, :caller , :type] )
@load = SlotLoad.new( [:message, :caller, :type] , [:message, :caller , :type] )
@compiler = CompilerMock.new
@instruction = @load.to_risc(@compiler)
end
def test_ins_next_class
assert_equal Risc::SlotToReg , @instruction.next.class
assert_equal Risc::SlotToReg , @instruction.next(1).class
assert_equal Risc::SlotToReg , @instruction.next(2).class
end
def test_ins_next_next_class
assert_equal Risc::RegToSlot , @instruction.next.next.class
assert_equal Risc::RegToSlot , @instruction.next(3).class
end
def test_ins_next_reg
@ -28,14 +28,24 @@ module Mom
assert_equal 0 , @instruction.next.index
end
def test_ins_next_next_reg
assert_equal :r1 , @instruction.next.next.register.symbol
def test_ins_next_2_reg
assert_equal :r1 , @instruction.next(2).register.symbol
end
def test_ins_next_next_arr
assert_equal :r0 , @instruction.next.next.array.symbol
def test_ins_next_2_arr
assert_equal :r0 , @instruction.next(2).array.symbol
end
def test_ins_next_next_index
assert_equal 6 , @instruction.next.next.index
def test_ins_next_2_index
assert_equal 6 , @instruction.next(2).index
end
def test_ins_next_3_reg
assert_equal :r1 , @instruction.next(3).register.symbol
end
def test_ins_next_3_arr
assert_equal :r1 , @instruction.next(3).array.symbol
end
def test_ins_next_3_index
assert_equal 0 , @instruction.next(3).index
end
end
end

View File

@ -0,0 +1,48 @@
require_relative "../helper"
module Risc
class FakeCompiler
def resolve_type(name)
Parfait.object_space.types.values.first
end
def use_reg(type)
RegisterValue.new(:r1 , type)
end
end
class TestRegisterValue < MiniTest::Test
def setup
Parfait.boot!
@r0 = RegisterValue.new(:r0 , :Message)
@r1 = RegisterValue.new(:r1 , :Space)
@compiler = FakeCompiler.new
end
def test_resolves_index_ok
assert_equal 6 , @r0.resolve_index(:caller)
end
def test_resolves_index_fail
assert_raises {@r0.resolve_index(:something)}
end
def test_revolve_new_type_0
assert_equal "Message_Type", @r0.resolve_new_type(:caller , @compiler).name
end
def test_revolve_new_type_1
# returned by FakeCompiler , not real
assert_equal "BinaryCode_Type", @r1.resolve_new_type(:receiver , @compiler).name
end
def test_get_new_left_0
assert_equal RegisterValue , @r0.get_new_left(:caller , @compiler).class
end
def test_get_new_left_0_reg
assert_equal :r1 , @r0.get_new_left(:caller , @compiler).symbol
end
def test_get_new_left_1
assert_equal RegisterValue , @r1.get_new_left(:caller , @compiler).class
end
def test_get_new_left_1_reg
assert_equal :r1 , @r1.get_new_left(:caller , @compiler).symbol
end
end
end