remove ad_transfer in save_message

towards removing all add_
change to set_builder and chaining in reg values
This commit is contained in:
Torsten Ruger 2018-08-14 20:08:58 +03:00
parent 37461a1727
commit 520dc7b41f
5 changed files with 24 additions and 15 deletions

View File

@ -33,9 +33,7 @@ module Risc
name = name.to_s name = name.to_s
return @names[name] if @names.has_key?(name) return @names[name] if @names.has_key?(name)
if name == "message" if name == "message"
reg = Risc.message_reg return Risc.message_reg.set_builder(self)
reg.builder = self
return reg
end end
if name.index("label") if name.index("label")
reg = Risc.label( @source , "#{name}_#{object_id}") reg = Risc.label( @source , "#{name}_#{object_id}")
@ -44,8 +42,7 @@ module Risc
raise "Must create (with !) before using #{name}" unless name[-1] == "!" raise "Must create (with !) before using #{name}" unless name[-1] == "!"
name = name[0 ... -1] name = name[0 ... -1]
type = infer_type(name ) type = infer_type(name )
reg = @compiler.use_reg( type.object_class.name ) reg = @compiler.use_reg( type.object_class.name ).set_builder(self)
reg.builder = self
end end
@names[name] = reg @names[name] = reg
reg reg
@ -129,8 +126,8 @@ module Risc
# move a machine int from register "from" to a Parfait::Integer in register "to" # move a machine int from register "from" to a Parfait::Integer in register "to"
# have to grab an integer from space and stick it in the "to" register first. # have to grab an integer from space and stick it in the "to" register first.
def add_new_int( source , from, to ) def add_new_int( source , from, to )
to.builder = self # esecially div10 comes in without having used builder to.set_builder( self ) # esecially div10 comes in without having used builder
from.builder = self # not named regs, different regs ==> silent errors from.set_builder( self ) # not named regs, different regs ==> silent errors
build do build do
space! << Parfait.object_space space! << Parfait.object_space
to << space[:next_integer] to << space[:next_integer]

View File

@ -117,8 +117,8 @@ module Risc
# This relies on linux to save and restore all registers # This relies on linux to save and restore all registers
# #
def save_message(builder) def save_message(builder)
r8 = RegisterValue.new( :r8 , :Message) r8 = RegisterValue.new( :r8 , :Message).set_builder(builder)
builder.add_transfer("save_message", Risc.message_reg , r8 ) builder.build {r8 << message}
end end
# restore the message that we save in r8 # restore the message that we save in r8

View File

@ -11,7 +11,7 @@ module Risc
attr_reader :symbol , :type , :extra attr_reader :symbol , :type , :extra
attr_accessor :builder attr_reader :builder
# The first arg is a symbol :r0 - :r12 # The first arg is a symbol :r0 - :r12
# Second arg is the type, which may be given as the symbol of the class name # Second arg is the type, which may be given as the symbol of the class name
@ -34,6 +34,15 @@ module Risc
return :fixnum unless @type return :fixnum unless @type
@type.class_name @type.class_name
end end
# allows to set the builder, which is mainly done by the builder
# but sometimes, eg in exit, one nneds to create the reg by hand and set
# return the RegisterValue for chaining in assignment
def set_builder( builder )
@builder = builder
self
end
# using the registers type, resolve the slot to an index # using the registers type, resolve the slot to an index
# Using the index and the register, add a SlotToReg to the instruction # Using the index and the register, add a SlotToReg to the instruction
def resolve_and_add(slot , instruction , compiler) def resolve_and_add(slot , instruction , compiler)

View File

@ -45,15 +45,13 @@ module Risc
assert_equal Transfer , built.next.class assert_equal Transfer , built.next.class
end end
def test_returns_slot def test_returns_slot
r2 = RegisterValue.new(:r2 , :Message) r2 = RegisterValue.new(:r2 , :Message).set_builder( @builder )
r2.builder = @builder
built = @builder.build{ r2 << space![:next_message] } built = @builder.build{ r2 << space![:next_message] }
assert_equal SlotToReg , built.class assert_equal SlotToReg , built.class
assert_equal :r1 , built.array.symbol assert_equal :r1 , built.array.symbol
end end
def test_returns_slot_reverse def test_returns_slot_reverse
r2 = RegisterValue.new(:r2 , :Message) r2 = RegisterValue.new(:r2 , :Message).set_builder( @builder )
r2.builder = @builder
built = @builder.build{ r2 << space![:next_message] } built = @builder.build{ r2 << space![:next_message] }
assert_equal SlotToReg , built.class assert_equal SlotToReg , built.class
assert_equal :r1 , built.array.symbol assert_equal :r1 , built.array.symbol

View File

@ -35,9 +35,14 @@ module Risc
transfer = @r0 << @r1 transfer = @r0 << @r1
assert_equal Transfer , transfer.class assert_equal Transfer , transfer.class
end end
def test_set_builder
reg = @r0.set_builder(FakeBuilder.new)
assert_equal RegisterValue , reg.class
assert reg.builder
end
def test_calls_builder def test_calls_builder
builder = FakeBuilder.new builder = FakeBuilder.new
@r0.builder = builder @r0.set_builder( builder )
@r0 << @r1 @r0 << @r1
assert_equal Transfer , builder.built.class assert_equal Transfer , builder.built.class
end end