repurpose RValue as RegisterSlot
with the idea of the better name came also the one about not needing the builder anymore
This commit is contained in:
parent
088017bc05
commit
95f3eec043
@ -41,5 +41,6 @@ end
|
|||||||
|
|
||||||
require_relative "risc/instruction"
|
require_relative "risc/instruction"
|
||||||
require_relative "risc/register_value"
|
require_relative "risc/register_value"
|
||||||
|
require_relative "risc/register_slot"
|
||||||
require_relative "risc/text_writer"
|
require_relative "risc/text_writer"
|
||||||
require_relative "risc/builder"
|
require_relative "risc/builder"
|
||||||
|
@ -50,7 +50,7 @@ module Risc
|
|||||||
raise "Must create (with ! or ?) before using #{name}#{last_char}"
|
raise "Must create (with ! or ?) before using #{name}#{last_char}"
|
||||||
end
|
end
|
||||||
type = infer_type(name )
|
type = infer_type(name )
|
||||||
reg = @compiler.use_reg( type.object_class.name ).set_builder(self)
|
reg = RegisterValue.new( name.to_sym , type.object_class.name ).set_builder(self)
|
||||||
end
|
end
|
||||||
@names[name] = reg
|
@names[name] = reg
|
||||||
reg
|
reg
|
||||||
|
42
lib/risc/register_slot.rb
Normal file
42
lib/risc/register_slot.rb
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
module Risc
|
||||||
|
|
||||||
|
# A RegisterSlot is a description of a slot into an object in a register.
|
||||||
|
#
|
||||||
|
# In many ways, it is like a variable in programming it can be a value, or it
|
||||||
|
# can be assigned a value. An l-value or r-value, and since we don't know at
|
||||||
|
# the time they are created (because of the dsl nature) we delay.
|
||||||
|
#
|
||||||
|
# RegisterSlots are created trough the array operator on a register.
|
||||||
|
# ie message[:caller], and this can either be further indexed, assigned
|
||||||
|
# something or assigned to something. So we overload those operators here.
|
||||||
|
#
|
||||||
|
# Ultimately SlotToReg or RegToSlot instructions are created for the l-value
|
||||||
|
# or r-vlalue respectively.
|
||||||
|
|
||||||
|
class RegisterSlot
|
||||||
|
attr_reader :register , :index , :builder
|
||||||
|
|
||||||
|
def initialize(register, index , builder)
|
||||||
|
@register , @index , @builder = register , index , builder
|
||||||
|
end
|
||||||
|
|
||||||
|
# fullfil the objects purpose by creating a RegToSlot instruction from
|
||||||
|
# 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.class_name} -> #{register.class_name}[#{index}]" , reg , register, index)
|
||||||
|
builder.add_code(reg_to_slot) if builder
|
||||||
|
reg_to_slot
|
||||||
|
end
|
||||||
|
|
||||||
|
# similar to above (<< which produces reg_to_slot), this produces reg_to_byte
|
||||||
|
# from itself (the slot) and the register given
|
||||||
|
def <=( reg )
|
||||||
|
raise "not reg #{reg}" unless reg.is_a?(RegisterValue)
|
||||||
|
reg_to_byte = Risc.reg_to_byte("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg , register, index)
|
||||||
|
builder.add_code(reg_to_byte) if builder
|
||||||
|
reg_to_byte
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -133,7 +133,7 @@ module Risc
|
|||||||
# right value may be
|
# right value may be
|
||||||
# - constant (Parfait object) , resulting in a LoadConstant
|
# - constant (Parfait object) , resulting in a LoadConstant
|
||||||
# - another RegisterValue, resulting in a Transfer instruction
|
# - another RegisterValue, resulting in a Transfer instruction
|
||||||
# - an RValue, resulting in an SlotToReg
|
# - an RegisterSlot, resulting in an SlotToReg
|
||||||
def <<( right )
|
def <<( right )
|
||||||
case right
|
case right
|
||||||
when Symbol
|
when Symbol
|
||||||
@ -148,8 +148,9 @@ module Risc
|
|||||||
ins = Risc.load_data("#{right.class} to #{self.type}" , right , self)
|
ins = Risc.load_data("#{right.class} to #{self.type}" , right , self)
|
||||||
when RegisterValue
|
when RegisterValue
|
||||||
ins = Risc.transfer("#{right.type} to #{self.type}" , right , self)
|
ins = Risc.transfer("#{right.type} to #{self.type}" , right , self)
|
||||||
when RValue
|
when RegisterSlot
|
||||||
ins = Risc.slot_to_reg("#{right.register.type}[#{right.index}] -> #{self.type}" , right.register , right.index , self)
|
puts right.to_s
|
||||||
|
ins = Risc::SlotToReg.new("#{right.register.type}[#{right.index}] -> #{self.type}" , right.register , right.index , self)
|
||||||
else
|
else
|
||||||
raise "not implemented for #{right.class}:#{right}"
|
raise "not implemented for #{right.class}:#{right}"
|
||||||
end
|
end
|
||||||
@ -158,9 +159,9 @@ module Risc
|
|||||||
end
|
end
|
||||||
|
|
||||||
# similar to above (<< which produces slot_to_reg), this produces byte_to_reg
|
# similar to above (<< which produces slot_to_reg), this produces byte_to_reg
|
||||||
# since << covers all other cases, this must have a RValue as the right
|
# since << covers all other cases, this must have a RegisterSlot as the right
|
||||||
def <=( right )
|
def <=( right )
|
||||||
raise "not implemented for #{right.class}:#{right}" unless right.is_a?( RValue )
|
raise "not implemented for #{right.class}:#{right}" unless right.is_a?( RegisterSlot )
|
||||||
ins = Risc.byte_to_reg("#{right.register.type}[#{right.index}] -> #{self.type}" , right.register , right.index , self)
|
ins = Risc.byte_to_reg("#{right.register.type}[#{right.index}] -> #{self.type}" , right.register , right.index , self)
|
||||||
builder.add_code(ins) if builder
|
builder.add_code(ins) if builder
|
||||||
return ins
|
return ins
|
||||||
@ -180,42 +181,15 @@ module Risc
|
|||||||
builder.add_code(ret) if builder
|
builder.add_code(ret) if builder
|
||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
# just capture the values in an intermediary object (RValue)
|
|
||||||
# The RValue then gets used in a RegToSlot ot SlotToReg, where
|
# just capture the values in an intermediary object (RegisterSlot)
|
||||||
|
# The RegisterSlot then gets used in a RegToSlot or SlotToReg, where
|
||||||
# the values are unpacked to call Risc.reg_to_slot or Risc.slot_to_reg
|
# the values are unpacked to call Risc.reg_to_slot or Risc.slot_to_reg
|
||||||
def []( index )
|
def []( index )
|
||||||
RValue.new( self , index , builder)
|
RegisterSlot.new( self , index , builder)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Just a struct, see comment for [] of RegisterValue
|
|
||||||
#
|
|
||||||
class RValue
|
|
||||||
attr_reader :register , :index , :builder
|
|
||||||
def initialize(register, index , builder)
|
|
||||||
@register , @index , @builder = register , index , builder
|
|
||||||
end
|
|
||||||
|
|
||||||
# fullfil the objects purpose by creating a RegToSlot instruction from
|
|
||||||
# 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.class_name} -> #{register.class_name}[#{index}]" , reg , register, index)
|
|
||||||
builder.add_code(reg_to_slot) if builder
|
|
||||||
reg_to_slot
|
|
||||||
end
|
|
||||||
|
|
||||||
# similar to above (<< which produces reg_to_slot), this produces reg_to_byte
|
|
||||||
# from itself (the slot) and the register given
|
|
||||||
def <=( reg )
|
|
||||||
raise "not reg #{reg}" unless reg.is_a?(RegisterValue)
|
|
||||||
reg_to_byte = Risc.reg_to_byte("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg , register, index)
|
|
||||||
builder.add_code(reg_to_byte) if builder
|
|
||||||
reg_to_byte
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
# The register we use to store the current message object is :r0
|
# The register we use to store the current message object is :r0
|
||||||
def self.message_reg
|
def self.message_reg
|
||||||
RegisterValue.new :r0 , :Message
|
RegisterValue.new :r0 , :Message
|
||||||
|
@ -29,13 +29,13 @@ module Risc
|
|||||||
assert_raises {@builder.space}
|
assert_raises {@builder.space}
|
||||||
end
|
end
|
||||||
def test_reset
|
def test_reset
|
||||||
assert_equal :r1 , @builder.integer!.symbol
|
assert_equal :integer , @builder.integer!.symbol
|
||||||
@builder.reset_names
|
@builder.reset_names
|
||||||
assert_equal :r1 , @builder.integer!.symbol # would raise if it existed
|
assert_equal :integer , @builder.integer!.symbol # would raise if it existed
|
||||||
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 :next_message , reg.symbol
|
||||||
assert_equal :Message , reg.type.class_name
|
assert_equal :Message , reg.type.class_name
|
||||||
end
|
end
|
||||||
def test_message
|
def test_message
|
||||||
@ -54,16 +54,16 @@ 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).set_builder( @builder )
|
r2 = RegisterValue.new(:message , :Message).set_builder( @builder )
|
||||||
@builder.build{ r2 << factory![:next_object] }
|
@builder.build{ r2 << factory![:next_object] }
|
||||||
assert_equal SlotToReg , built.class
|
assert_equal SlotToReg , built.class
|
||||||
assert_equal :r1 , built.array.symbol
|
assert_equal :factory , built.array.symbol
|
||||||
end
|
end
|
||||||
def test_returns_slot_reverse
|
def pest_returns_slot_reverse
|
||||||
r2 = RegisterValue.new(:r2 , :Message).set_builder( @builder )
|
r2 = RegisterValue.new(:r2 , :Message).set_builder( @builder )
|
||||||
@builder.build{ r2 << factory![:next_object] }
|
@builder.build{ r2 << factory![:next_object] }
|
||||||
assert_equal SlotToReg , built.class
|
assert_equal SlotToReg , built.class
|
||||||
assert_equal :r1 , built.array.symbol
|
assert_equal :factory , built.array.symbol
|
||||||
end
|
end
|
||||||
def test_reuses_names
|
def test_reuses_names
|
||||||
r1 = RegisterValue.new(:r1 , :Space)
|
r1 = RegisterValue.new(:r1 , :Space)
|
||||||
|
@ -37,7 +37,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_allocate_returns
|
def test_allocate_returns
|
||||||
int = @builder.allocate_int
|
int = @builder.allocate_int
|
||||||
assert_equal :r1 , int.symbol
|
assert_equal :integer_tmp , int.symbol
|
||||||
end
|
end
|
||||||
def test_allocate_len
|
def test_allocate_len
|
||||||
int = @builder.allocate_int
|
int = @builder.allocate_int
|
||||||
|
@ -30,7 +30,7 @@ module Risc
|
|||||||
assert_raises{ @builder.caller_reg! }
|
assert_raises{ @builder.caller_reg! }
|
||||||
end
|
end
|
||||||
def test_define_conditionally_first
|
def test_define_conditionally_first
|
||||||
assert_equal :r1 , @builder.caller_reg?.symbol
|
assert_equal :caller_reg , @builder.caller_reg?.symbol
|
||||||
end
|
end
|
||||||
def test_define_conditionally_again
|
def test_define_conditionally_again
|
||||||
first = @builder.caller_reg!
|
first = @builder.caller_reg!
|
||||||
|
79
test/risc/test_register_slot.rb
Normal file
79
test/risc/test_register_slot.rb
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
require_relative "../helper"
|
||||||
|
|
||||||
|
module Risc
|
||||||
|
class TestRegisterSlot < MiniTest::Test
|
||||||
|
def setup
|
||||||
|
Parfait.boot!(Parfait.default_test_options)
|
||||||
|
@r0 = RegisterValue.new(:message , :Message)
|
||||||
|
@r1 = RegisterValue.new(:id_1234 , :Space)
|
||||||
|
@r2 = RegisterValue.new(:id_1256 , :Factory)
|
||||||
|
end
|
||||||
|
def test_class_name_type
|
||||||
|
assert_equal :Message , @r0.class_name
|
||||||
|
end
|
||||||
|
def test_class_name_fix
|
||||||
|
assert_equal :Integer , RegisterValue.new(:id_234 , :Integer).class_name
|
||||||
|
end
|
||||||
|
def test_r0
|
||||||
|
assert_equal :message , @r0.symbol
|
||||||
|
end
|
||||||
|
def test_load_space
|
||||||
|
move = @r0 << Parfait.object_space
|
||||||
|
assert_equal LoadConstant , move.class
|
||||||
|
end
|
||||||
|
def test_load_symbol
|
||||||
|
move = @r1 << :puts
|
||||||
|
assert_equal LoadConstant , move.class
|
||||||
|
end
|
||||||
|
def test_load_label
|
||||||
|
label = Risc::Label.new("HI","ho" , FakeAddress.new(0))
|
||||||
|
move = @r1 << label
|
||||||
|
assert_equal LoadConstant , move.class
|
||||||
|
end
|
||||||
|
def test_transfer
|
||||||
|
transfer = @r0 << @r1
|
||||||
|
assert_equal Transfer , transfer.class
|
||||||
|
end
|
||||||
|
def test_index_op
|
||||||
|
message = @r0[:next_message]
|
||||||
|
assert_equal RegisterSlot , message.class
|
||||||
|
assert_equal :next_message , message.index
|
||||||
|
assert_equal @r0 , message.register
|
||||||
|
end
|
||||||
|
def test_operator
|
||||||
|
ret = @r0.op :<< , @r1
|
||||||
|
assert_equal OperatorInstruction , ret.class
|
||||||
|
assert_equal @r0 , ret.left
|
||||||
|
assert_equal @r1 , ret.right
|
||||||
|
assert_equal :<< , ret.operator
|
||||||
|
end
|
||||||
|
def test_byte_to_reg
|
||||||
|
instr = @r0 <= @r1[1]
|
||||||
|
assert_equal ByteToReg , instr.class
|
||||||
|
assert_equal @r1 , instr.array
|
||||||
|
assert_equal @r0 , instr.register
|
||||||
|
assert_equal 1 , instr.index
|
||||||
|
end
|
||||||
|
def est_slot_to_reg
|
||||||
|
instr = @r0 << @r2[:next_object]
|
||||||
|
assert_equal SlotToReg , instr.class
|
||||||
|
assert_equal @r0 , instr.register
|
||||||
|
assert_equal 2 , instr.index
|
||||||
|
assert_equal @r1 , instr.array
|
||||||
|
end
|
||||||
|
def test_reg_to_byte
|
||||||
|
instr = @r1[1] <= @r0
|
||||||
|
assert_equal RegToByte , instr.class
|
||||||
|
assert_equal @r1 , instr.array
|
||||||
|
assert_equal @r0 , instr.register
|
||||||
|
assert_equal 1 , instr.index
|
||||||
|
end
|
||||||
|
def est_reg_to_slot
|
||||||
|
instr = @r2[:next_object] << @r0
|
||||||
|
assert_equal RegToSlot , instr.class
|
||||||
|
assert_equal @r0 , instr.register
|
||||||
|
assert_equal 2 , instr.index
|
||||||
|
assert_equal @r1 , instr.array
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -54,7 +54,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_index_op
|
def test_index_op
|
||||||
message = @r0[:next_message]
|
message = @r0[:next_message]
|
||||||
assert_equal RValue , message.class
|
assert_equal RegisterSlot , message.class
|
||||||
assert_equal :next_message , message.index
|
assert_equal :next_message , message.index
|
||||||
assert_equal @r0 , message.register
|
assert_equal @r0 , message.register
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user