diff --git a/lib/risc/builtin.rb b/lib/risc/builtin.rb index b9c48b36..75e763e5 100644 --- a/lib/risc/builtin.rb +++ b/lib/risc/builtin.rb @@ -1,4 +1,16 @@ -require_relative "builtin/compile_helper" +module Risc + module Builtin + module CompileHelper + + def compiler_for( clazz_name , method_name , arguments , locals = {}) + frame = Parfait::NamedList.type_for( locals ) + args = Parfait::NamedList.type_for( arguments ) + Mom::MethodCompiler.compiler_for_class(clazz_name , method_name , args, frame ) + end + end + end +end + require_relative "builtin/space" require_relative "builtin/integer" require_relative "builtin/object" @@ -25,22 +37,23 @@ module Risc end obj_type = space.get_type_by_class_name(:Object) - [ :get_internal_word , :set_internal_word , :_method_missing, - :exit , :__init__].each do |f| + [ :get_internal_word , #:set_internal_word , :_method_missing, + #:exit , :__init__ + ].each do |f| compilers << compiler_for( obj_type , Object , f) end word_type = space.get_type_by_class_name(:Word) [:putstring , :get_internal_byte , :set_internal_byte ].each do |f| - compilers << compiler_for( word_type , Word , f) + #compilers << compiler_for( word_type , Word , f) end int_type = space.get_type_by_class_name(:Integer) Risc.operators.each do |op| - compilers << operator_compiler( int_type , op) + #compilers << operator_compiler( int_type , op) end [:putint, :div4, :div10 , :<,:<= , :>=, :>].each do |f| #div4 is just a forward declaration - compilers << compiler_for( int_type , Integer , f) + #compilers << compiler_for( int_type , Integer , f) end compilers end diff --git a/lib/risc/builtin/compile_helper.rb b/lib/risc/builtin/compile_helper.rb deleted file mode 100644 index 43a2b93d..00000000 --- a/lib/risc/builtin/compile_helper.rb +++ /dev/null @@ -1,14 +0,0 @@ - -module Risc - module Builtin - module CompileHelper - - def compiler_for( clazz_name , method_name , arguments , locals = {}) - frame = Parfait::NamedList.type_for( locals ) - args = Parfait::NamedList.type_for( arguments ) - MethodCompiler.compiler_for_class(clazz_name , method_name , args, frame ) - end - - end - end -end diff --git a/lib/risc/builtin/object.rb b/lib/risc/builtin/object.rb index c55909eb..c8870a1b 100644 --- a/lib/risc/builtin/object.rb +++ b/lib/risc/builtin/object.rb @@ -8,17 +8,21 @@ module Risc # return is stored in return_value def get_internal_word( context ) compiler = compiler_for(:Object , :get_internal_word ,{at: :Integer}) - compiler.builder(compiler.source).build do - object! << message[:receiver] - integer! << message[:arguments] - integer << integer[Parfait::NamedList.type_length + 0] #"at" is at index 0 - integer.reduce_int - object << object[integer] - message[:return_value] << object - end - compiler.add_mom( Mom::ReturnSequence.new) + compiler.add_code GetInternalWord.new("get_internal_word") return compiler end + class GetInternalWord < ::Mom::Instruction + def to_risc(compiler) + compiler.builder(compiler.source).build do + object! << message[:receiver] + integer! << message[:arguments] + integer << integer[Parfait::NamedList.type_length + 0] #"at" is at index 0 + integer.reduce_int + object << object[integer] + message[:return_value] << object + end + end + end # self[index] = val basically. Index is the first arg , value the second # return the value passed in diff --git a/lib/risc/builtin/space.rb b/lib/risc/builtin/space.rb index c1c16095..ec638d0f 100644 --- a/lib/risc/builtin/space.rb +++ b/lib/risc/builtin/space.rb @@ -8,7 +8,6 @@ module Risc # defined here as empty, to be redefined def main(context) compiler = compiler_for(:Space , :main ,{args: :Integer}) - compiler.add_mom( Mom::ReturnSequence.new) return compiler end diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index 81f71aca..936175f8 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -34,7 +34,7 @@ module Risc # # return compiler_for_type with the resolved type # - def self.compiler_for_class( class_name , method_name , args , frame ) + def self.compiler_for_clazz( class_name , method_name , args , frame ) raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol clazz = Parfait.object_space.get_class_by_name! class_name compiler_for_type( clazz.instance_type , method_name , args , frame) @@ -53,7 +53,7 @@ module Risc # args a hash that will be converted to a type # the created method is set as the current and the given type too # return the compiler - def self.compiler_for_type( type , method_name , args , frame) + def self.compiler_for_typez( type , method_name , args , frame) raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type raise "Args must be Type #{args}" unless args.is_a?(Parfait::Type) raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol diff --git a/lib/rubyx.rb b/lib/rubyx.rb index 62cf4f0e..05e9450e 100644 --- a/lib/rubyx.rb +++ b/lib/rubyx.rb @@ -1,11 +1,12 @@ require "rx-file" require "util/logging" +require "util/list" require_relative "elf/object_writer" +require_relative "mom/mom" require_relative "risc" require_relative "arm/arm_machine" require_relative "arm/arm_platform" require_relative "vool/statement" require_relative "ruby" require_relative "rubyx/rubyx_compiler" -require_relative "mom/mom" diff --git a/test/risc/builtin/helper.rb b/test/risc/builtin/helper.rb index 68357f88..55dea0ce 100644 --- a/test/risc/builtin/helper.rb +++ b/test/risc/builtin/helper.rb @@ -7,5 +7,15 @@ module Risc def setup end end + class BootTest < MiniTest::Test + def setup + Parfait.boot!(Parfait.default_test_options) + @functions = Builtin.boot_functions + end + def get_compiler( name ) + @functions.each.find{|meth| + meth.callable.name == name} + end + end end end diff --git a/test/risc/builtin/test_int_cmp.rb b/test/risc/builtin/test_int_cmp.rb index e3ed2866..902637c0 100644 --- a/test/risc/builtin/test_int_cmp.rb +++ b/test/risc/builtin/test_int_cmp.rb @@ -1,5 +1,6 @@ require_relative "helper" +# TODO move these to interpreter dir module Risc module Builtin class IntCmp < BuiltinTest diff --git a/test/risc/builtin/test_object.rb b/test/risc/builtin/test_object.rb new file mode 100644 index 00000000..f09beb50 --- /dev/null +++ b/test/risc/builtin/test_object.rb @@ -0,0 +1,24 @@ +require_relative "helper" + +module Risc + module Builtin + class TestObjectFunction1 < BootTest + def setup + super + @method = get_compiler(:get_internal_word) + end + def test_has_get_internal + assert_equal Mom::MethodCompiler , @method.class + end + def test_mom_length + assert_equal 5 , @method.mom_instructions.length + end + def test_compile + assert_equal Risc::MethodCompiler , @method.to_risc.class + end + def test_risc_length + assert_equal 20 , @method.to_risc.risc_instructions.length + end + end + end +end diff --git a/test/risc/test_builtin.rb b/test/risc/test_builtin.rb index e1b4e020..9f50b286 100644 --- a/test/risc/test_builtin.rb +++ b/test/risc/test_builtin.rb @@ -5,18 +5,19 @@ module Risc def setup Parfait.boot!(Parfait.default_test_options) + @functions = Builtin.boot_functions end def test_has_boot_function - assert Builtin.boot_functions + assert @functions end def test_boot_function_type - assert_equal Array, Builtin.boot_functions.class + assert_equal Array, @functions.class end def test_boot_function_length - assert_equal 23, Builtin.boot_functions.length + assert_equal 2, @functions.length end def test_boot_function_first - assert_equal MethodCompiler, Builtin.boot_functions.first.class + assert_equal Mom::MethodCompiler, @functions.first.class end end end