From f31d22d9017b281c340dad7d1fef1e20cb6ebd38 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 15 Jul 2018 15:16:12 +0300 Subject: [PATCH] use real type in register, not symbol this has some more consequences, upcoming --- lib/risc/register_value.rb | 32 +++++++++++++++++----- test/mom/assign/test_assign_local_false.rb | 2 +- test/mom/assign/test_assign_local_int.rb | 2 +- test/risc/test_builder.rb | 8 +++--- test/risc/test_register_value1.rb | 6 ++-- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/risc/register_value.rb b/lib/risc/register_value.rb index 63300f53..d4b62395 100644 --- a/lib/risc/register_value.rb +++ b/lib/risc/register_value.rb @@ -1,15 +1,23 @@ 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 attr_reader :symbol , :type , :value 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) 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 @symbol = reg @value = value @@ -28,7 +36,6 @@ module Risc # 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 @@ -43,9 +50,9 @@ module Risc def get_new_left(slot, compiler) new_type = resolve_new_type(slot , compiler) if( @symbol == :r0 ) - new_left = compiler.use_reg( new_type.class_name ) + new_left = compiler.use_reg( new_type ) else - new_left = RegisterValue.new( @symbol , new_type.class_name) + new_left = RegisterValue.new( @symbol , new_type) end new_left end @@ -53,7 +60,18 @@ module Risc # 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) + 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 def to_s @@ -145,7 +163,7 @@ module Risc # itself (the slot) and the register given def <<( reg ) 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 reg_to_slot end diff --git a/test/mom/assign/test_assign_local_false.rb b/test/mom/assign/test_assign_local_false.rb index 7a542131..fd8ad78b 100644 --- a/test/mom/assign/test_assign_local_false.rb +++ b/test/mom/assign/test_assign_local_false.rb @@ -20,7 +20,7 @@ module Risc def test_frame_load 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 end def test_value_load diff --git a/test/mom/assign/test_assign_local_int.rb b/test/mom/assign/test_assign_local_int.rb index fc305355..be51beb8 100644 --- a/test/mom/assign/test_assign_local_int.rb +++ b/test/mom/assign/test_assign_local_int.rb @@ -20,7 +20,7 @@ module Risc def test_frame_load 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 end def test_value_load diff --git a/test/risc/test_builder.rb b/test/risc/test_builder.rb index da22b4bf..f50c8f55 100644 --- a/test/risc/test_builder.rb +++ b/test/risc/test_builder.rb @@ -19,17 +19,17 @@ module Risc def test_alloc_space reg = @builder.space assert_equal RegisterValue , reg.class - assert_equal :Space , reg.type + assert_equal :Space , reg.type.class_name end def test_next_message reg = @builder.next_message assert_equal :r1 , reg.symbol - assert_equal :Message , reg.type + assert_equal :Message , reg.type.class_name end def test_message reg = @builder.message assert_equal :r0 , reg.symbol - assert_equal :Message , reg.type + assert_equal :Message , reg.type.class_name end def test_returns_built r1 = RegisterValue.new(:r1 , :Space) @@ -95,7 +95,7 @@ module Risc op = @builder.build {space - callable_method} assert_equal OperatorInstruction , op.class assert_equal :- , op.operator - assert_equal :Space , op.left.type + assert_equal :Space , op.left.type.class_name end end class TestCompilerBuilder < MiniTest::Test diff --git a/test/risc/test_register_value1.rb b/test/risc/test_register_value1.rb index 18f98051..1e153e96 100644 --- a/test/risc/test_register_value1.rb +++ b/test/risc/test_register_value1.rb @@ -26,7 +26,7 @@ module Risc assert_raises {@r0.resolve_index(:something)} end 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 def test_revolve_new_type_1 # returned by FakeCompiler , not real @@ -39,10 +39,10 @@ module Risc 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 + assert_equal RegisterValue , @r0.get_new_left(:caller , @compiler).class end 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