parent
5b2c7745fe
commit
047a36178f
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user