diff --git a/lib/risc.rb b/lib/risc.rb index 257a5532..334c2493 100644 --- a/lib/risc.rb +++ b/lib/risc.rb @@ -29,6 +29,7 @@ require_relative "risc/linker" require_relative "risc/callable_compiler" require_relative "risc/method_compiler" require_relative "risc/block_compiler" +require_relative "risc/allocator" require_relative "risc/assembler" require_relative "risc/risc_collection" diff --git a/lib/risc/allocator.rb b/lib/risc/allocator.rb index 84fc0317..5a7e5302 100644 --- a/lib/risc/allocator.rb +++ b/lib/risc/allocator.rb @@ -5,11 +5,13 @@ module Risc # upon request. they must be returned in order class Allocator - def initialize() + def initialize( compiler , platform) + @compiler = compiler + @platform = platform @regs = [] reset_regs end - attr_reader :regs + attr_reader :regs , :compiler , :platform def regs_empty? @regs.empty? diff --git a/lib/risc/callable_compiler.rb b/lib/risc/callable_compiler.rb index 6bbeb0cb..f6a4423d 100644 --- a/lib/risc/callable_compiler.rb +++ b/lib/risc/callable_compiler.rb @@ -18,10 +18,8 @@ module Risc def initialize( callable , slot_label) raise "No method" unless callable @callable = callable - @allocator = Allocator.new @constants = [] @current = @risc_instructions = slot_label.risc_label(self) - @allocator.reset_regs end attr_reader :risc_instructions , :constants , :callable , :current @@ -137,11 +135,24 @@ module Risc # translate this method, which means the method itself and all blocks inside it # returns the array (of assemblers) that you pass in as collection - def translate_method( translator , collection) - collection << translate_cpu( translator ) + # first arg is the platform object representing the platform that we + # translate to + # + # This calls allocate_regs first, to change register naming to the platform + # + def translate_method( platform , collection) + allocate_regs( platform ) + collection << translate_cpu( platform.translator ) collection end + # allocate registers to the platform specific names (and amount) + # This is actually done by the Allocator , with the help of the Platform + # The Platform specifies how many registers there are, and the + # Allocator changes SSA names to allocated names + def allocate_regs(platform) + allocator = Allocator.new(self , platform) + end end end require_relative "allocator" diff --git a/lib/risc/parfait_boot.rb b/lib/risc/parfait_boot.rb index e8009f71..76989cb2 100644 --- a/lib/risc/parfait_boot.rb +++ b/lib/risc/parfait_boot.rb @@ -122,7 +122,7 @@ module Parfait List: {indexed_length: :Integer , next_list: :List} , Message: { next_message: :Message, receiver: :Object, frame: :Object , return_address: :Integer, return_value: :Object, - caller: :Message , method: :TypedMethod , + caller: :Message , method: :CallableMethod , arguments_given: :Integer , arg1: :Object , arg2: :Object, arg3: :Object, arg4: :Object, arg5: :Object, arg6: :Object, diff --git a/lib/risc/register_value.rb b/lib/risc/register_value.rb index 8f736042..8b5800b5 100644 --- a/lib/risc/register_value.rb +++ b/lib/risc/register_value.rb @@ -29,7 +29,7 @@ module Risc end def class_name - return :fixnum unless @type + return :Integer unless @type @type.class_name end diff --git a/lib/risc/risc_collection.rb b/lib/risc/risc_collection.rb index f654af9f..6966bdab 100644 --- a/lib/risc/risc_collection.rb +++ b/lib/risc/risc_collection.rb @@ -42,7 +42,7 @@ module Risc platform = Platform.for(platform_sym) assemblers = [] @method_compilers.each_compiler do |compiler| - compiler.translate_method( platform.translator , assemblers) + compiler.translate_method( platform , assemblers) end Risc::Linker.new(platform , assemblers , constants) end diff --git a/test/risc/binary/test_linker.rb b/test/risc/binary/test_linker.rb index 8a254c93..f276ef3d 100644 --- a/test/risc/binary/test_linker.rb +++ b/test/risc/binary/test_linker.rb @@ -1,4 +1,4 @@ -require_relative "helper" +require_relative "../helper" module Risc class TestLinkerObjects < MiniTest::Test diff --git a/test/risc/binary/test_linker1.rb b/test/risc/binary/test_linker1.rb index 13529621..355c5019 100644 --- a/test/risc/binary/test_linker1.rb +++ b/test/risc/binary/test_linker1.rb @@ -1,4 +1,4 @@ -require_relative "helper" +require_relative "../helper" module Risc class TestMachinePos < MiniTest::Test diff --git a/test/risc/test_allocator.rb b/test/risc/test_allocator.rb index 115c6cbb..10cbc912 100644 --- a/test/risc/test_allocator.rb +++ b/test/risc/test_allocator.rb @@ -5,7 +5,7 @@ module Risc def setup Parfait.boot!(Parfait.default_test_options) - @allocator = Allocator.new + @allocator = Allocator.new(Risc.test_compiler , Platform.for(:arm)) end def tmp_reg Risc.tmp_reg(:Type) @@ -16,6 +16,13 @@ module Risc def test_empty assert @allocator.regs_empty? end + def test_compiler + assert_equal CallableCompiler , @allocator.compiler.class + assert_equal :fake_name , @allocator.compiler.callable.name + end + def test_platform + assert_equal Arm::ArmPlatform , @allocator.platform.class + end def test_add_ok assert_equal Array, @allocator.add_reg(tmp_reg).class end diff --git a/test/risc/test_callable_compiler1.rb b/test/risc/test_callable_compiler1.rb new file mode 100644 index 00000000..4c87b40d --- /dev/null +++ b/test/risc/test_callable_compiler1.rb @@ -0,0 +1,21 @@ +require_relative "../helper" + +module Risc + class TestCallableCompiler1 < MiniTest::Test + include SolCompile + + def setup + @compiler = compile_main("return 5").to_risc + @platform = Platform.for(:arm) + end + def test_init + @compiler.risc_instructions.each do |ins| + puts ins.to_s + end + end + + def test_1 + @compiler.translate_method( @platform , []) + end + end +end diff --git a/test/support/compiling.rb b/test/support/compiling.rb index 35403cab..a4c8ba9f 100644 --- a/test/support/compiling.rb +++ b/test/support/compiling.rb @@ -4,7 +4,7 @@ module ScopeHelper def compiler_with_main(options = {}) compiler = RubyX::RubyXCompiler.new(RubyX.default_test_options.merge(options)) - compiler.ruby_to_sol( "class Space;def main(arg);return;end;end" ) + compiler.ruby_to_sol( in_Space( as_main("return")) ) compiler end def in_Test(statements)