Externalise register allocation into own class
On the way to the register allocation
This commit is contained in:
parent
fa144784fa
commit
685022a6e0
62
lib/risc/allocator.rb
Normal file
62
lib/risc/allocator.rb
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
module Risc
|
||||||
|
# just moved compilers register related stuff here
|
||||||
|
#
|
||||||
|
# Allocator keeps a list of registers and passes them out
|
||||||
|
# upon request. they must be returned in order
|
||||||
|
class Allocator
|
||||||
|
|
||||||
|
def initialize()
|
||||||
|
@regs = []
|
||||||
|
reset_regs
|
||||||
|
end
|
||||||
|
attr_reader :regs
|
||||||
|
|
||||||
|
def regs_empty?
|
||||||
|
@regs.empty?
|
||||||
|
end
|
||||||
|
def add_reg(reg)
|
||||||
|
raise "not reg #{reg.class}" unless reg.is_a?(RegisterValue)
|
||||||
|
@regs << reg
|
||||||
|
end
|
||||||
|
def pop
|
||||||
|
@regs.pop
|
||||||
|
end
|
||||||
|
def clear_regs
|
||||||
|
@regs.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
# require a (temporary) register. code must give this back with release_reg
|
||||||
|
# Second extra parameter may give extra info about the value, see RegisterValue
|
||||||
|
def use_reg( type , extra = {} )
|
||||||
|
raise "Not type #{type.inspect}:#{type.class}" unless type.is_a?(Symbol) or type.is_a?(Parfait::Type)
|
||||||
|
if @regs.empty?
|
||||||
|
reg = Risc.tmp_reg(type , extra)
|
||||||
|
else
|
||||||
|
reg = @regs.last.next_reg_use(type , extra)
|
||||||
|
end
|
||||||
|
@regs << reg
|
||||||
|
return reg
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy( reg , source )
|
||||||
|
copied = use_reg reg.type
|
||||||
|
add_code Register.transfer( source , reg , copied )
|
||||||
|
copied
|
||||||
|
end
|
||||||
|
|
||||||
|
# releasing a register (accuired by use_reg) makes it available for use again
|
||||||
|
# thus avoiding possibly using too many registers
|
||||||
|
def release_reg( reg )
|
||||||
|
last = @regs.pop
|
||||||
|
raise "released register in wrong order, expect #{last} but was #{reg}" if reg != last
|
||||||
|
end
|
||||||
|
|
||||||
|
# reset the registers to be used. Start at r4 for next usage.
|
||||||
|
# Every statement starts with this, meaning each statement may use all registers, but none
|
||||||
|
# get saved. Statements have affect on objects.
|
||||||
|
def reset_regs
|
||||||
|
@regs.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -18,10 +18,10 @@ module Risc
|
|||||||
def initialize( callable , slot_label)
|
def initialize( callable , slot_label)
|
||||||
raise "No method" unless callable
|
raise "No method" unless callable
|
||||||
@callable = callable
|
@callable = callable
|
||||||
@regs = []
|
@allocator = Allocator.new
|
||||||
@constants = []
|
@constants = []
|
||||||
@current = @risc_instructions = slot_label.risc_label(self)
|
@current = @risc_instructions = slot_label.risc_label(self)
|
||||||
reset_regs
|
@allocator.reset_regs
|
||||||
end
|
end
|
||||||
attr_reader :risc_instructions , :constants , :callable , :current
|
attr_reader :risc_instructions , :constants , :callable , :current
|
||||||
|
|
||||||
@ -54,14 +54,7 @@ module Risc
|
|||||||
# require a (temporary) register. code must give this back with release_reg
|
# require a (temporary) register. code must give this back with release_reg
|
||||||
# Second extra parameter may give extra info about the value, see RegisterValue
|
# Second extra parameter may give extra info about the value, see RegisterValue
|
||||||
def use_reg( type , extra = {} )
|
def use_reg( type , extra = {} )
|
||||||
raise "Not type #{type.inspect}" unless type.is_a?(Symbol) or type.is_a?(Parfait::Type)
|
@allocator.use_reg(type, extra)
|
||||||
if @regs.empty?
|
|
||||||
reg = Risc.tmp_reg(type , extra)
|
|
||||||
else
|
|
||||||
reg = @regs.last.next_reg_use(type , extra)
|
|
||||||
end
|
|
||||||
@regs << reg
|
|
||||||
return reg
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# resolve the type of the slot, by inferring from it's name, using the type
|
# resolve the type of the slot, by inferring from it's name, using the type
|
||||||
@ -106,7 +99,7 @@ module Risc
|
|||||||
# releasing a register (accuired by use_reg) makes it available for use again
|
# releasing a register (accuired by use_reg) makes it available for use again
|
||||||
# thus avoiding possibly using too many registers
|
# thus avoiding possibly using too many registers
|
||||||
def release_reg( reg )
|
def release_reg( reg )
|
||||||
last = @regs.pop
|
last = @allocator.pop
|
||||||
raise "released register in wrong order, expect #{last} but was #{reg}" if reg != last
|
raise "released register in wrong order, expect #{last} but was #{reg}" if reg != last
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -114,7 +107,7 @@ module Risc
|
|||||||
# Every statement starts with this, meaning each statement may use all registers, but none
|
# Every statement starts with this, meaning each statement may use all registers, but none
|
||||||
# get saved. Statements have affect on objects.
|
# get saved. Statements have affect on objects.
|
||||||
def reset_regs
|
def reset_regs
|
||||||
@regs.clear
|
@allocator.clear_regs
|
||||||
end
|
end
|
||||||
|
|
||||||
# Build with builder (see there), adding the created instructions
|
# Build with builder (see there), adding the created instructions
|
||||||
@ -150,3 +143,4 @@ module Risc
|
|||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
require_relative "allocator"
|
||||||
|
@ -18,6 +18,7 @@ module SlotMachine
|
|||||||
# the register returned
|
# the register returned
|
||||||
def to_register(compiler, source)
|
def to_register(compiler, source)
|
||||||
type = known_object.get_type
|
type = known_object.get_type
|
||||||
|
raise "not sym for #{known_object}" if type.is_a?(String)
|
||||||
right = compiler.use_reg( type )
|
right = compiler.use_reg( type )
|
||||||
const = Risc.load_constant(source, known_object , right)
|
const = Risc.load_constant(source, known_object , right)
|
||||||
compiler.add_code const
|
compiler.add_code const
|
||||||
|
34
test/risc/test_allocator.rb
Normal file
34
test/risc/test_allocator.rb
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
require_relative "../helper"
|
||||||
|
|
||||||
|
module Risc
|
||||||
|
class TestAllocator < MiniTest::Test
|
||||||
|
|
||||||
|
def setup
|
||||||
|
Parfait.boot!(Parfait.default_test_options)
|
||||||
|
@allocator = Allocator.new
|
||||||
|
end
|
||||||
|
def tmp_reg
|
||||||
|
Risc.tmp_reg(:Type)
|
||||||
|
end
|
||||||
|
def test_regs
|
||||||
|
assert_equal Array , @allocator.regs.class
|
||||||
|
end
|
||||||
|
def test_empty
|
||||||
|
assert @allocator.regs_empty?
|
||||||
|
end
|
||||||
|
def test_add_ok
|
||||||
|
assert_equal Array, @allocator.add_reg(tmp_reg).class
|
||||||
|
end
|
||||||
|
def test_add_fail
|
||||||
|
assert_raises{ @allocator.add_reg(1)}
|
||||||
|
end
|
||||||
|
def test_pop
|
||||||
|
@allocator.add_reg(tmp_reg)
|
||||||
|
assert_equal RegisterValue , @allocator.pop.class
|
||||||
|
end
|
||||||
|
def test_clear
|
||||||
|
@allocator.clear_regs
|
||||||
|
assert @allocator.regs_empty?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -9,7 +9,32 @@ module Risc
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
class TestCallableCompiler < MiniTest::Test
|
class TestCallableCompiler < MiniTest::Test
|
||||||
|
def setup
|
||||||
|
Parfait.boot!({})
|
||||||
|
label = SlotMachine::Label.new("hi","ho")
|
||||||
|
@compiler = CallableCompiler.new(FakeCallable.new , label)
|
||||||
|
end
|
||||||
|
def test_ok
|
||||||
|
assert @compiler
|
||||||
|
end
|
||||||
|
def test_current
|
||||||
|
assert @compiler.current
|
||||||
|
end
|
||||||
|
def test_current_label
|
||||||
|
assert_equal Label , @compiler.current.class
|
||||||
|
assert_equal "ho" , @compiler.current.name
|
||||||
|
end
|
||||||
|
def test_slot
|
||||||
|
assert @compiler.risc_instructions
|
||||||
|
end
|
||||||
|
def test_const
|
||||||
|
assert_equal Array , @compiler.constants.class
|
||||||
|
end
|
||||||
|
def test_use_reg
|
||||||
|
@compiler.use_reg(:Type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
class TestFakeCallableCompiler < MiniTest::Test
|
||||||
def setup
|
def setup
|
||||||
Parfait.boot!({})
|
Parfait.boot!({})
|
||||||
label = SlotMachine::Label.new("hi","ho")
|
label = SlotMachine::Label.new("hi","ho")
|
||||||
|
Loading…
Reference in New Issue
Block a user