diff --git a/lib/mom/builtin.rb b/lib/mom/builtin.rb index 2148cb2a..f135a2f8 100644 --- a/lib/mom/builtin.rb +++ b/lib/mom/builtin.rb @@ -21,12 +21,12 @@ module Mom # classes have booted, now create a minimal set of functions # minimal means only that which can not be coded in ruby # Methods are grabbed from respective modules by sending the method name. - # This should return the implementation of the method (ie a method object), + # This should return the implementation of the method (ie a method compiler), # not actually try to implement it(as that's impossible in ruby) # - # When no main has been compiled, we will add an empty main (for testing) - # - def self.boot_functions(add_main = false) + # We create an empty main for init to jump to, if no code is compiled, that just returns + # See Builtin directory readme and module + def self.boot_functions() # TODO go through the virtual parfait layer and adjust function names # to what they really are compilers = [] @@ -37,8 +37,8 @@ module Mom end obj_type = space.get_type_by_class_name(:Object) - [ :get_internal_word , :set_internal_word , :_method_missing, - :exit , :__init__ ].each do |f| + [ :__init__ , :exit , :_method_missing, :get_internal_word , + :set_internal_word ].each do |f| compilers << compiler_for( obj_type , Object , f) end diff --git a/lib/mom/mom_collection.rb b/lib/mom/mom_collection.rb index 39f2447a..61b78f1c 100644 --- a/lib/mom/mom_collection.rb +++ b/lib/mom/mom_collection.rb @@ -17,13 +17,13 @@ module Mom # lazily instantiate the compilers for boot functions # (in the hope of only booting the functions once) def boot_compilers - @boot_compilers ||= Risc::Builtin.boot_functions + @boot_compilers ||= Mom::Builtin.boot_functions end # Return all compilers, namely the MethodCompilers passed in, plus the # boot_function's compilers (boot_compilers) def compilers - @method_compilers #+ boot_compilers + @method_compilers + boot_compilers end # collects constants from all compilers into one array @@ -37,8 +37,8 @@ module Mom self end - def to_risc( ) - riscs = method_compilers.collect do | mom_c | + def to_risc( ) + riscs = compilers.collect do | mom_c | mom_c.to_risc end # to_risc all compilers diff --git a/lib/risc/risc_collection.rb b/lib/risc/risc_collection.rb index 265532f6..bc3c024b 100644 --- a/lib/risc/risc_collection.rb +++ b/lib/risc/risc_collection.rb @@ -10,21 +10,9 @@ module Risc @method_compilers = compilers end - # lazily instantiate the compilers for boot functions - # (in the hope of only booting the functions once) - def boot_compilers - @boot_compilers ||= Risc::Builtin.boot_functions - end - - # Return all compilers, namely the MethodCompilers passed in, plus the - # boot_function's compilers (boot_compilers) - def compilers - @method_compilers #+ boot_compilers - end - # collects constants from all compilers into one array def constants - compilers.inject([]){|sum ,comp| sum + comp.constants } + method_compilers.inject([]){|sum ,comp| sum + comp.constants } end # Append another MomCompilers method_compilers to this one. @@ -47,7 +35,7 @@ module Risc # go through all methods and translate them to cpu, given the translator def translate_methods(translator) - compilers.collect do |compiler| + method_compilers.collect do |compiler| #log.debug "Translate method #{compiler.method.name}" translate_method(compiler , translator) end.flatten diff --git a/lib/rubyx/rubyx_compiler.rb b/lib/rubyx/rubyx_compiler.rb index d6249c0c..c3f28ce3 100644 --- a/lib/rubyx/rubyx_compiler.rb +++ b/lib/rubyx/rubyx_compiler.rb @@ -44,9 +44,9 @@ module RubyX # The biary the method name refers to is binary code in memory, or in BinaryCode # objects to be precise. def to_binary(platform) - linker = to_risc(platform) + linker = to_risc linker.position_all - linker.create_binary + linker.create_binary(platform) linker end diff --git a/test/mom/test_mom_collection.rb b/test/mom/test_mom_collection.rb index aa370d53..09cf9426 100644 --- a/test/mom/test_mom_collection.rb +++ b/test/mom/test_mom_collection.rb @@ -13,13 +13,13 @@ module Mom assert_equal MomCollection , @comp.class end def test_compilers - assert_equal 1 , @comp.compilers.length + assert_equal 22 , @comp.compilers.length end def test_boot_compilers -# assert_equal 22 , @comp.boot_compilers.length + assert_equal 21 , @comp.boot_compilers.length end def test_compilers_bare - assert_equal 0 , MomCollection.new.compilers.length + assert_equal 21 , MomCollection.new.compilers.length end def test_returns_constants assert_equal Array , @comp.constants.class @@ -50,7 +50,7 @@ module Mom end def test_has_risc_compiler assert_equal Risc::MethodCompiler, compiler.class - assert_equal 1, @collection.method_compilers.length + assert_equal 22, @collection.method_compilers.length end def test_has_risc_instructions assert_equal Risc::Label, compiler.risc_instructions.class diff --git a/test/risc/test_risc_collection.rb b/test/risc/test_risc_collection.rb new file mode 100644 index 00000000..bcf2af81 --- /dev/null +++ b/test/risc/test_risc_collection.rb @@ -0,0 +1,59 @@ +require_relative "helper" + +module Risc + class TestMomCompilerTranslate < MiniTest::Test + include MomCompile + + def setup + Parfait.boot!(Parfait.default_test_options) + @comp = compile_mom( "class Test ; def main(); main{return 'Ho'};return 'Hi'; end; end;") + @linker = @comp.to_risc.translate(:interpreter) + end + + def test_translate_class + assert_equal Linker , @linker.class + end + def test_linker_has_constants + assert_equal Array , @linker.constants.class + end + def test_linker_constants_not_empty + assert !@linker.constants.empty? + end + def test_linker_constants_contains_hi + assert @linker.constants.include?("Hi") + end + def test_linker_constants_contains_ho + assert @linker.constants.include?("Ho") + end + def test_translate_platform + assert_kind_of Platform , @linker.platform + end + def test_translate_assemblers + assert_equal Assembler , @linker.assemblers.first.class + end + def test_assembler_code + assert_equal Label , @linker.assemblers.first.instructions.class + end + def test_assembler_assembled + assert_equal LoadConstant , @linker.assemblers.first.instructions.next.class + end + def test_no_loops_in_chain + @linker.assemblers.each do |asm| + all = [] + asm.instructions.each do |ins| + assert !all.include?(ins) , "Double in #{asm.callable.name}:#{ins}" + all << ins + end + end + end + def test_no_risc + @linker.position_all + @linker.create_binary + @linker.assemblers.each do |asm| + asm.instructions.each do |ins| + ins.assemble(Util::DevNull.new) + end # risc instruction don't have an assemble + end + end + end +end diff --git a/test/risc/test_risc_compiler.rb b/test/risc/test_risc_compiler.rb index bcf2af81..9347477e 100644 --- a/test/risc/test_risc_compiler.rb +++ b/test/risc/test_risc_compiler.rb @@ -5,7 +5,6 @@ module Risc include MomCompile def setup - Parfait.boot!(Parfait.default_test_options) @comp = compile_mom( "class Test ; def main(); main{return 'Ho'};return 'Hi'; end; end;") @linker = @comp.to_risc.translate(:interpreter) end