From 88dbc7c84f5dab9f26425cbeb64aceabccc29ae3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 6 Apr 2018 16:35:27 +0300 Subject: [PATCH] slot to reg for builder --- lib/risc/builder.rb | 5 ++--- lib/risc/risc_value.rb | 26 ++++++++++++++++---------- test/risc/test_builder.rb | 27 ++++++++++++++++++++++++--- test/risc/test_risc_value.rb | 10 +++++++--- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index 07fd3630..da510e0f 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -11,10 +11,9 @@ module Risc def method_missing(*args) super if args.length != 1 name = args[0].to_s.capitalize.to_sym - type = Risc.resolve_type(name , @compiler) - reg = @compiler.use_reg( type ) + Risc.resolve_type(name , @compiler) #checking + reg = @compiler.use_reg( name ) reg.builder = self - puts reg reg end def build(&block) diff --git a/lib/risc/risc_value.rb b/lib/risc/risc_value.rb index 63812b7b..413e118f 100644 --- a/lib/risc/risc_value.rb +++ b/lib/risc/risc_value.rb @@ -8,12 +8,10 @@ module Risc attr_accessor :builder - def initialize( r , type , value = nil) - raise "wrong type for register init #{r}" unless r.is_a? Symbol - raise "double r #{r}" if r.to_s[0,1] == "rr" - raise "not reg #{r}" unless self.class.look_like_reg r + def initialize( reg , type , value = nil) + raise "not reg #{reg}" unless self.class.look_like_reg( reg ) @type = type - @symbol = r + @symbol = reg @value = value end @@ -65,7 +63,6 @@ module Risc # - another RiscValue, resulting in a Transfer instruction # - an RValue, which gets procuced by the [] below def <<( load ) - puts "LOAD #{load}" case load when RValue raise "not yet" @@ -84,16 +81,25 @@ module Risc # The RValue then gets used in a RegToSlot ot SlotToReg, where # the values are unpacked to call Risc.reg_to_slot or Risc.slot_to_reg def []( index ) - RValue.new( self , index) + RValue.new( self , index , builder) end end # Just a struct, see comment for [] of RiscValue # class RValue - attr_reader :register , :index - def initialize(register, index) - @register , @index = register , index + attr_reader :register , :index , :builder + def initialize(register, index , builder) + @register , @index , @builder = register , index , builder + end + + # fullfil the objects purpose by creating a SlotToReg instruction from + # itself (the slot) and the register given + def >>( reg ) + raise "not reg #{reg}" unless reg.is_a?(RiscValue) + slot = Risc.slot_to_reg("#{register.type}[#{index}] -> #{reg.type}" , @register , @index , reg) + builder.add_instruction(slot) if builder + slot end end diff --git a/test/risc/test_builder.rb b/test/risc/test_builder.rb index 7333d0aa..12a7200d 100644 --- a/test/risc/test_builder.rb +++ b/test/risc/test_builder.rb @@ -12,13 +12,34 @@ module Risc def test_has_build assert_nil @builder.build{ } end - def test_has_build_and_returns_built + def test_has_attribute + assert_nil @builder.built + end + def test_register_alloc_space + reg = @builder.space + assert_equal RiscValue , reg.class + assert_equal :Space , reg.type + end + def test_register_alloc_message + reg = @builder.message + assert_equal :r1 , reg.symbol + assert_equal :Message , reg.type + end + def test_returns_built r1 = RiscValue.new(:r1 , :Space) built = @builder.build{ space << r1 } assert_equal Transfer , built.class end - def test_has_attribute - assert_nil @builder.built + def test_returns_two + r1 = RiscValue.new(:r1 , :Space) + built = @builder.build{ space << r1 ; space << r1} + assert_equal Transfer , built.next.class + end + def test_returns_slot + r2 = RiscValue.new(:r2 , :Message) + built = @builder.build{ space[:first_message] >> r2 } + assert_equal SlotToReg , built.class + assert_equal :r1 , built.array.symbol end end end diff --git a/test/risc/test_risc_value.rb b/test/risc/test_risc_value.rb index b9ee809e..d3c6f576 100644 --- a/test/risc/test_risc_value.rb +++ b/test/risc/test_risc_value.rb @@ -14,11 +14,9 @@ module Risc @r0 = RiscValue.new(:r0 , :Message) @r1 = RiscValue.new(:r1 , :Space) end - def test_r0 assert_equal :r0 , @r0.symbol end - def test_load_space move = @r0 << Parfait.object_space assert_equal LoadConstant , move.class @@ -39,6 +37,12 @@ module Risc assert_equal :first_message , message.index assert_equal @r0 , message.register end - #message << space[:first_message] + def test_slot_to_reg + instr = @r1[:first_message] >> @r0 + assert_equal SlotToReg , instr.class + assert_equal @r1 , instr.array + assert_equal @r0 , instr.register + assert_equal 4 , instr.index + end end end