slot to reg for builder

This commit is contained in:
Torsten Ruger 2018-04-06 16:35:27 +03:00
parent c233bd82d6
commit 88dbc7c84f
4 changed files with 49 additions and 19 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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