From d5f89a4979e62db1b9675538b47e752ec8a4f749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Sat, 10 Aug 2019 12:42:47 +0300 Subject: [PATCH] compile from mom compiler to risc --- lib/mom/instruction/label.rb | 4 ++-- lib/mom/method_compiler.rb | 15 +++++++++++++ lib/mom/mom_collection.rb | 13 ----------- lib/risc/callable_compiler.rb | 8 +++---- lib/risc/method_compiler.rb | 6 +++-- test/mom/test_method_compiler.rb | 34 +++++++++++++++++++---------- test/risc/test_callable_compiler.rb | 8 +++---- test/risc/test_method_compiler.rb | 8 ------- 8 files changed, 51 insertions(+), 45 deletions(-) diff --git a/lib/mom/instruction/label.rb b/lib/mom/instruction/label.rb index ea1208d2..8958a7e2 100644 --- a/lib/mom/instruction/label.rb +++ b/lib/mom/instruction/label.rb @@ -33,9 +33,9 @@ module Mom end # generate the risc label lazily - def risc_label(comiler) + def risc_label(compiler) @risc_label ||= Risc.label(self,name) - comiler.add_constant(@risc_label.address) + compiler.add_constant(@risc_label.address) @risc_label end diff --git a/lib/mom/method_compiler.rb b/lib/mom/method_compiler.rb index e8c61b11..5765927b 100644 --- a/lib/mom/method_compiler.rb +++ b/lib/mom/method_compiler.rb @@ -27,6 +27,21 @@ module Mom @callable end + # drop down to risc + def to_risc + risc_comp = Risc::MethodCompiler.new(@callable , mom_instructions) + instruction = mom_instructions.next + while( instruction ) + raise "whats this a #{instruction}" unless instruction.is_a?(Mom::Instruction) + #puts "adding mom #{instruction.to_s}:#{instruction.next.to_s}" + instruction.to_risc( risc_comp ) + risc_comp.reset_regs + #puts "adding risc #{risc.to_s}:#{risc.next.to_s}" + instruction = instruction.next + end + risc_comp + end + # helper method for builtin mainly # the class_name is a symbol, which is resolved to the instance_type of that class # diff --git a/lib/mom/mom_collection.rb b/lib/mom/mom_collection.rb index f574b4f7..e980308f 100644 --- a/lib/mom/mom_collection.rb +++ b/lib/mom/mom_collection.rb @@ -45,19 +45,6 @@ module Mom Risc::RiscCollection.new(riscs) end - # convert the given mom instruction to_risc and then add it (see add_code) - # continue down the instruction chain unti depleted - # (adding moves the insertion point so the whole mom chain is added as a risc chain) - def add_mom( instruction ) - while( instruction ) - raise "whats this a #{instruction}" unless instruction.is_a?(Mom::Instruction) - #puts "adding mom #{instruction.to_s}:#{instruction.next.to_s}" - instruction.to_risc( self ) - reset_regs - #puts "adding risc #{risc.to_s}:#{risc.next.to_s}" - instruction = instruction.next - end - end end end diff --git a/lib/risc/callable_compiler.rb b/lib/risc/callable_compiler.rb index dacb24b2..c5e13530 100644 --- a/lib/risc/callable_compiler.rb +++ b/lib/risc/callable_compiler.rb @@ -11,14 +11,14 @@ module Risc # class CallableCompiler - # Must pass the callable (method/block) and the constants that were parsed + # Must pass the callable (method/block) # Also start instuction, usually a label is mandatory - def initialize( callable , constants , start) + def initialize( callable , mom_label) @callable = callable @regs = [] - @constants = constants + @constants = [] @block_compilers = [] - @current = @risc_instructions = start + @current = @risc_instructions = mom_label.risc_label(self) reset_regs end attr_reader :risc_instructions , :constants , :block_compilers , :callable , :current diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index 3037700f..81f71aca 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -5,8 +5,10 @@ module Risc class MethodCompiler < CallableCompiler - def initialize( method ) - super(method) + # Methods starts with a Label, both in risc and mom. + # Pass in the callable(method) and the mom label that the method starts with + def initialize( method , mom_label) + super(method , mom_label) end #include block_compilers constants diff --git a/test/mom/test_method_compiler.rb b/test/mom/test_method_compiler.rb index 5db946e6..9ccf5d1d 100644 --- a/test/mom/test_method_compiler.rb +++ b/test/mom/test_method_compiler.rb @@ -7,20 +7,30 @@ module Mom def setup end - def in_test_vool(str) - vool = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_vool(in_Test(str)) - vool.to_mom(nil) - vool - end - def create_method(body = "@ivar = 5") - in_test_vool("def meth; #{body};end") - test = Parfait.object_space.get_class_by_name(:Test) - test.get_method(:meth) + def in_test_vool(body = "@ivar = 5") + code = in_Test("def meth; #{body};end") + RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(code) end - def test_method_has_source - method = create_method - assert_equal Vool::IvarAssignment , method.source.class + def test_method + in_test_vool() + method = Parfait.object_space.get_class_by_name(:Test).get_method(:meth) + assert_equal Parfait::VoolMethod , method.class + end + def test_method_mom_col + mom = in_test_vool() + assert_equal Mom::MomCollection , mom.class + assert_equal Mom::MethodCompiler , mom.compilers.first.class + end + def test_compiles_risc + compiler = in_test_vool().compilers.first.to_risc + assert_equal Risc::MethodCompiler , compiler.class + assert_equal Risc::Label , compiler.risc_instructions.class + end + def test_compiles_all_risc + compiler = in_test_vool().compilers.first.to_risc + assert_equal Risc::LoadConstant , compiler.risc_instructions.next.class + assert_equal 17 , compiler.risc_instructions.length end end end diff --git a/test/risc/test_callable_compiler.rb b/test/risc/test_callable_compiler.rb index ea97349d..9d7022dd 100644 --- a/test/risc/test_callable_compiler.rb +++ b/test/risc/test_callable_compiler.rb @@ -3,8 +3,8 @@ module Risc class FakeCallable end class FakeCallableCompiler < CallableCompiler - def initialize(a,b,c) - super(a,b,c) + def initialize(a,c) + super(a,c) end def source_name "luke" @@ -14,8 +14,8 @@ module Risc def setup Parfait.boot!({}) - label = Risc.label("hi","ho") - @compiler = FakeCallableCompiler.new(FakeCallable.new , [] , label) + label = Mom::Label.new("hi","ho") + @compiler = FakeCallableCompiler.new(FakeCallable.new , label) end def test_ok assert @compiler diff --git a/test/risc/test_method_compiler.rb b/test/risc/test_method_compiler.rb index 1ef918bd..f2516592 100644 --- a/test/risc/test_method_compiler.rb +++ b/test/risc/test_method_compiler.rb @@ -67,14 +67,6 @@ module Risc assert_equal Mom::MethodCompiler , compiler.class compiler end - def test_has_method_constant - compiler = constant_setup("def meth; return 'Hi';end") - assert compiler.constants.include?("Hi") - end - def test_has_block_constant - compiler = constant_setup("def meth; meth{return 'Ho'};return 'Hi';end") - assert compiler.constants.include?("Ho") - end def test_return_label compiler = constant_setup("def meth; return 'Hi';end") assert_equal "return_label", compiler.return_label.name