use real type in register, not symbol

this has some more consequences, upcoming
This commit is contained in:
Torsten Ruger 2018-07-15 15:16:12 +03:00
parent a71a6d34fb
commit f31d22d901
5 changed files with 34 additions and 16 deletions

View File

@ -1,15 +1,23 @@
module Risc module Risc
# RegisterValue is like a variable name, a storage location. The location is a register off course. # RegisterValue is like a variable name, a storage location.
# The location is a register off course.
# The type is always known, and sometimes the value too
#
# When participating in the builder dsl, a builder may be set to get the
# results of dsl operations (like <<) back to the builder
class RegisterValue class RegisterValue
attr_reader :symbol , :type , :value attr_reader :symbol , :type , :value
attr_accessor :builder attr_accessor :builder
# The first arg is a symbol :r0 - :r12
# Second arg is the type, which may be given as the symbol of the class name
# (internally we store the actual type instance, resolving any symbols)
def initialize( reg , type , value = nil) def initialize( reg , type , value = nil)
raise "not reg #{reg}" unless self.class.look_like_reg( reg ) raise "not reg #{reg}" unless self.class.look_like_reg( reg )
type = Parfait.object_space.get_type_by_class_name(type) if type.is_a?(Symbol)
@type = type @type = type
@symbol = reg @symbol = reg
@value = value @value = value
@ -28,7 +36,6 @@ module Risc
# RegisterValue has the current type, so we just look up the index in the type # RegisterValue has the current type, so we just look up the index in the type
def resolve_index(slot) def resolve_index(slot)
#puts "TYPE #{type} obj:#{object} var:#{slot} comp:#{compiler}" #puts "TYPE #{type} obj:#{object} var:#{slot} comp:#{compiler}"
type = Parfait.object_space.get_type_by_class_name(@type)
index = type.variable_index(slot) index = type.variable_index(slot)
raise "Index not found for #{slot} in #{type} of type #{@type}" unless index raise "Index not found for #{slot} in #{type} of type #{@type}" unless index
return index return index
@ -43,9 +50,9 @@ module Risc
def get_new_left(slot, compiler) def get_new_left(slot, compiler)
new_type = resolve_new_type(slot , compiler) new_type = resolve_new_type(slot , compiler)
if( @symbol == :r0 ) if( @symbol == :r0 )
new_left = compiler.use_reg( new_type.class_name ) new_left = compiler.use_reg( new_type )
else else
new_left = RegisterValue.new( @symbol , new_type.class_name) new_left = RegisterValue.new( @symbol , new_type)
end end
new_left new_left
end end
@ -53,7 +60,18 @@ module Risc
# resolve the type of the slot, by inferring from it's name # 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 # Currently this is implemented in Risc.resolve_type , but code shoudl be moved here
def resolve_new_type(slot, compiler) def resolve_new_type(slot, compiler)
Risc.resolve_type(slot , compiler) case slot
when :frame , :arguments , :receiver
type = compiler.resolve_type(slot)
when :name
type = Parfait.object_space.get_type_by_class_name( :Word )
when Symbol
type = @type.type_for(slot)
raise "Not found object #{slot}: in #{@type}" unless type
else
raise "Not implemented object #{slot}:#{slot.class}"
end
return type
end end
def to_s def to_s
@ -145,7 +163,7 @@ module Risc
# itself (the slot) and the register given # itself (the slot) and the register given
def <<( reg ) def <<( reg )
raise "not reg #{reg}" unless reg.is_a?(RegisterValue) raise "not reg #{reg}" unless reg.is_a?(RegisterValue)
reg_to_slot = Risc.reg_to_slot("#{reg.type} -> #{register.type}[#{index}]" , reg , register, index) reg_to_slot = Risc.reg_to_slot("#{reg.type.class_name} -> #{register.type.class_name}[#{index}]" , reg , register, index)
builder.add_code(reg_to_slot) if builder builder.add_code(reg_to_slot) if builder
reg_to_slot reg_to_slot
end end

View File

@ -20,7 +20,7 @@ module Risc
def test_frame_load def test_frame_load
produced = produce_body produced = produce_body
assert_equal :Message , produced.next(1).array.type assert_equal :Message , produced.next(1).array.type.class_name
assert_equal 3 , produced.next(1).index # 3 is frame assert_equal 3 , produced.next(1).index # 3 is frame
end end
def test_value_load def test_value_load

View File

@ -20,7 +20,7 @@ module Risc
def test_frame_load def test_frame_load
produced = produce_body produced = produce_body
assert_equal :Message , produced.next(1).array.type assert_equal :Message , produced.next(1).array.type.class_name
assert_equal 3 , produced.next(1).index # 4 is frame assert_equal 3 , produced.next(1).index # 4 is frame
end end
def test_value_load def test_value_load

View File

@ -19,17 +19,17 @@ module Risc
def test_alloc_space def test_alloc_space
reg = @builder.space reg = @builder.space
assert_equal RegisterValue , reg.class assert_equal RegisterValue , reg.class
assert_equal :Space , reg.type assert_equal :Space , reg.type.class_name
end end
def test_next_message def test_next_message
reg = @builder.next_message reg = @builder.next_message
assert_equal :r1 , reg.symbol assert_equal :r1 , reg.symbol
assert_equal :Message , reg.type assert_equal :Message , reg.type.class_name
end end
def test_message def test_message
reg = @builder.message reg = @builder.message
assert_equal :r0 , reg.symbol assert_equal :r0 , reg.symbol
assert_equal :Message , reg.type assert_equal :Message , reg.type.class_name
end end
def test_returns_built def test_returns_built
r1 = RegisterValue.new(:r1 , :Space) r1 = RegisterValue.new(:r1 , :Space)
@ -95,7 +95,7 @@ module Risc
op = @builder.build {space - callable_method} op = @builder.build {space - callable_method}
assert_equal OperatorInstruction , op.class assert_equal OperatorInstruction , op.class
assert_equal :- , op.operator assert_equal :- , op.operator
assert_equal :Space , op.left.type assert_equal :Space , op.left.type.class_name
end end
end end
class TestCompilerBuilder < MiniTest::Test class TestCompilerBuilder < MiniTest::Test

View File

@ -26,7 +26,7 @@ module Risc
assert_raises {@r0.resolve_index(:something)} assert_raises {@r0.resolve_index(:something)}
end end
def test_revolve_new_type_0 def test_revolve_new_type_0
assert_equal "Message_Type", @r0.resolve_new_type(:caller , @compiler).name assert_equal :Message, @r0.resolve_new_type(:caller , @compiler)
end end
def test_revolve_new_type_1 def test_revolve_new_type_1
# returned by FakeCompiler , not real # returned by FakeCompiler , not real
@ -39,10 +39,10 @@ module Risc
assert_equal :r1 , @r0.get_new_left(:caller , @compiler).symbol assert_equal :r1 , @r0.get_new_left(:caller , @compiler).symbol
end end
def test_get_new_left_1 def test_get_new_left_1
assert_equal RegisterValue , @r1.get_new_left(:caller , @compiler).class assert_equal RegisterValue , @r0.get_new_left(:caller , @compiler).class
end end
def test_get_new_left_1_reg def test_get_new_left_1_reg
assert_equal :r1 , @r1.get_new_left(:caller , @compiler).symbol assert_equal :r1 , @r0.get_new_left(:caller , @compiler).symbol
end end
end end
end end