diff --git a/lib/mom/mom_compiler.rb b/lib/mom/mom_compiler.rb index 2c2e96d0..efdd1faf 100644 --- a/lib/mom/mom_compiler.rb +++ b/lib/mom/mom_compiler.rb @@ -3,7 +3,7 @@ module Mom attr_reader :clazz , :method_compilers def initialize(compilers = []) - @method_compilers = Builtin.boot_functions + compilers + @method_compilers = Risc::Builtin.boot_functions + compilers end # Translate code to whatever cpu is specified. @@ -14,7 +14,8 @@ module Mom def translate( platform_sym ) platform_sym = platform_sym.to_s.capitalize platform = Risc::Platform.for(platform_sym) - translate_methods( platform.translator ) + assemblers = translate_methods( platform.translator ) + Risc::Linker.new(platform , assemblers) #@cpu_init = risc_init.to_cpu(@platform.translator) end diff --git a/lib/risc/linker.rb b/lib/risc/linker.rb index 19d7c9b4..5e06620b 100644 --- a/lib/risc/linker.rb +++ b/lib/risc/linker.rb @@ -13,19 +13,20 @@ module Risc include Util::Logging log_level :info - def initialize(platform) + def initialize(platform , assemblers) if(platform.is_a?(Symbol)) platform = platform.to_s.capitalize platform = Risc::Platform.for(platform) end raise "Platform must be platform, not #{platform.class}" unless platform.is_a?(Platform) @platform = platform + @assemblers = assemblers @risc_init = nil @constants = [] end attr_reader :constants , :cpu_init - attr_reader :platform + attr_reader :platform , :assemblers # machine keeps a list of all objects and their positions. # this is lazily created with a collector @@ -38,12 +39,6 @@ module Risc def risc_init @risc_init ||= Branch.new( "__initial_branch__" , Parfait.object_space.get_init.risc_instructions ) end - # add a constant (which get created during compilation and need to be linked) - def add_constant(const) - raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object) - @constants << const - end - # To create binaries, objects (and labels) need to have a position # (so objects can be loaded and branches know where to jump) diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index fbf009e5..9cf4115c 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -61,7 +61,9 @@ module Risc end end + # add a constant (which get created during compilation and need to be linked) def add_constant(const) + raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object) @constants << const end diff --git a/lib/rubyx/rubyx_compiler.rb b/lib/rubyx/rubyx_compiler.rb index ce8531dc..aebbcc24 100644 --- a/lib/rubyx/rubyx_compiler.rb +++ b/lib/rubyx/rubyx_compiler.rb @@ -27,9 +27,7 @@ module RubyX def ruby_to_binary(platform = :arm) Parfait.boot! Risc.boot! - assemblers = ruby_to_mom - puts "Assemblers #{assemblers}" - linker = Risc::Linker.new(platform) + linker = ruby_to_risc(platform) linker.position_all linker.create_binary end diff --git a/test/mom/test_method_compiler.rb b/test/mom/test_mom_compiler.rb similarity index 65% rename from test/mom/test_method_compiler.rb rename to test/mom/test_mom_compiler.rb index e6c2026e..6bebe52e 100644 --- a/test/mom/test_method_compiler.rb +++ b/test/mom/test_mom_compiler.rb @@ -13,7 +13,7 @@ module Mom assert_equal MomCompiler , @comp.class end def test_compilers - assert_equal 1 , @comp.method_compilers.length + assert_equal 24 , @comp.method_compilers.length end def test_has_translate assert @comp.translate(:interpreter) @@ -29,16 +29,19 @@ module Mom end def test_translate_class - assert_equal Array , @trans.class + assert_equal Risc::Linker , @trans.class + end + def test_translate_platform + assert_kind_of Risc::Platform , @trans.platform end def test_translate_assemblers - assert_equal Risc::Assembler , @trans.first.class + assert_equal Risc::Assembler , @trans.assemblers.first.class end def test_assembler_code - assert_equal Risc::Label , @trans.first.instructions.class + assert_equal Risc::Label , @trans.assemblers.first.instructions.class end def test_assembler_assembled - assert_equal Risc::LoadConstant , @trans.first.instructions.next.class + assert_equal Risc::SlotToReg , @trans.assemblers.first.instructions.next.class end end end diff --git a/test/risc/test_linker.rb b/test/risc/test_linker.rb index c44ffe53..385b7233 100644 --- a/test/risc/test_linker.rb +++ b/test/risc/test_linker.rb @@ -6,27 +6,23 @@ module Risc def setup Parfait.boot! Risc.boot! - @machine = Linker.new(:arm) + @linker = Mom::MomCompiler.new.translate(:arm) end def test_objects - objects = @machine.object_positions + objects = @linker.object_positions assert_equal Hash , objects.class assert 350 < objects.length end def test_constant_fail assert_raises {@machine.add_constant( 1 )} end - def test_constant - assert @machine.add_constant( Parfait::Integer.new(5) ) - end end class TestMachinePos < MiniTest::Test def setup Parfait.boot! Risc.boot! - @linker = Linker.new(:arm) - @linker.translate - @machine.position_all + @linker = Mom::MomCompiler.new.translate(:arm) + @linker.position_all end def test_positions_set @machine.object_positions.each do |obj , position| diff --git a/test/rubyx/test_rubyx_compiler2.rb b/test/rubyx/test_rubyx_compiler2.rb index 57dc6ff3..9f43f506 100644 --- a/test/rubyx/test_rubyx_compiler2.rb +++ b/test/rubyx/test_rubyx_compiler2.rb @@ -5,11 +5,16 @@ module RubyX include ScopeHelper include RubyXHelper - def test_to_risc + def setup + super code = "class Space ; def main(arg);return arg;end; end" - risc = ruby_to_risc(code, :interpreter) - assert_equal Array , risc.class + @linker = ruby_to_risc(code, :interpreter) + end + def test_to_risc + assert_equal Risc::Linker , @linker.class + end + def test_method + assert_equal :main , @linker.assemblers.last.method.name end - end end diff --git a/test/vool/test_class_statement.rb b/test/vool/test_class_statement.rb index 97caf29e..5277e137 100644 --- a/test/vool/test_class_statement.rb +++ b/test/vool/test_class_statement.rb @@ -16,5 +16,10 @@ module Vool def test_has_compilers assert_equal Risc::MethodCompiler , @ret.method_compilers.first.class end + + def test_constant + assert @ret.method_compilers.first.add_constant( Parfait::Integer.new(5) ) + end + end end