From 16c8fcbf6649e37b423e7efcaf3068b426afdbe3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 17 Mar 2018 11:13:44 +0530 Subject: [PATCH] first local assignment risc test comes with casualties slot_load needs more work --- lib/mom/instruction/instruction.rb | 3 +++ lib/mom/instruction/slot_load.rb | 8 ++++---- lib/risc/method_compiler.rb | 4 ++-- test/mom/helper.rb | 21 +++++++++++++++------ test/mom/test_assignment.rb | 23 +++-------------------- test/mom/test_local_assignment.rb | 27 +++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 test/mom/test_local_assignment.rb diff --git a/lib/mom/instruction/instruction.rb b/lib/mom/instruction/instruction.rb index 56f3d23d..a1c48918 100644 --- a/lib/mom/instruction/instruction.rb +++ b/lib/mom/instruction/instruction.rb @@ -4,6 +4,9 @@ module Mom class Instruction include Common::List + def to_risc(m) + Risc::Label.new(self.class.name, self.class.name + "_todo") + end # implement flatten as noop to avoid condition def flatten( options = {} ) return self diff --git a/lib/mom/instruction/slot_load.rb b/lib/mom/instruction/slot_load.rb index 32dfb7e4..f995aa58 100644 --- a/lib/mom/instruction/slot_load.rb +++ b/lib/mom/instruction/slot_load.rb @@ -32,11 +32,11 @@ module Mom @left , @right = left , right end - def to_risc_load(context) - reg = context.use_reg( @right.ct_type) + def to_risc(compiler) + reg = compiler.use_reg( @right.ct_type) const = Risc.load_constant(self, @right , reg) - const.set_next Risc.reg_to_slot(self, reg , @left.known_object, @left.slots.first) - context.release_reg(reg) + const << Risc.reg_to_slot(self, reg , @left.known_object, @left.slots.first) + compiler.release_reg(reg) return const end diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index a027b5d7..128aa987 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -94,8 +94,8 @@ module Risc # (adding moves the insertion point so the whole mom chain is added as a risc chain) def add_mom( instruction ) raise "whats this a #{instruction}" unless instruction.is_a?(Mom::Instruction) - return while( instruction ) + #puts "adding #{instruction.to_s}:#{instruction.next.to_s}" risc = instruction.to_risc( self ) add_code(risc) instruction = instruction.next @@ -104,7 +104,7 @@ module Risc # add a risc instruction after the current (insertion point) # the added instruction will become the new insertion point def add_code( instruction ) - raise instruction.to_s unless instruction.is_a?(Risc::Instruction) + raise "Not an instruction:#{instruction.to_s}" unless instruction.is_a?(Risc::Instruction) raise instruction.to_s if( instruction.class.name.split("::").first == "Arm") @current.insert(instruction) #insert after current @current = instruction diff --git a/test/mom/helper.rb b/test/mom/helper.rb index 0de3ac50..97792477 100644 --- a/test/mom/helper.rb +++ b/test/mom/helper.rb @@ -21,11 +21,18 @@ module Risc class_type = Parfait.object_space.get_class_by_name(:Space).instance_type class_type.send(:private_add_instance_variable, name , type) end - - def check_nil + def produce_body + produced = produce_instructions + preamble.each{ produced = produced.next } + produced + end + def produce_instructions assert @expect , "No output given" Vool::VoolCompiler.ruby_to_vool "class Space; def main(arg);#{@input};end;end" - produced = Parfait.object_space.get_main.instructions + Parfait.object_space.get_main.instructions + end + def check_nil + produced = produce_instructions compare_instructions produced , @expect end def check_return @@ -33,7 +40,9 @@ module Risc raise was if was Parfait.object_space.get_main.instructions end - + def real_index(index) + index - preamble.length + 1 + end def compare_instructions( instruction , expect ) index = 0 all = instruction.to_arr @@ -42,8 +51,8 @@ module Risc begin should = full_expect[index] return "No instruction at #{index}\n#{should(all)}" unless should - return "Expected at #{index+1}\n#{should(all)}" unless instruction.class == should - #puts instruction.to_s + return "Expected at #{real_index(index)}\n#{should(all)} was #{instruction.to_s}" unless instruction.class == should + #puts instruction.to_s if (index > preamble.length) and (index + postamble.length <= full_expect.length) index += 1 instruction = instruction.next end while( instruction ) diff --git a/test/mom/test_assignment.rb b/test/mom/test_assignment.rb index c1f1140a..37aa22b5 100644 --- a/test/mom/test_assignment.rb +++ b/test/mom/test_assignment.rb @@ -4,33 +4,16 @@ module Risc class TestAssignStatement < MiniTest::Test include Statements - def pest_assign_local_assign - Parfait.object_space.get_main.add_local(:r , :Integer) - @input = "r = 5" - @expect = [LoadConstant, RegToSlot] - assert_nil msg = check_nil , msg - end - def pest_assign_op Parfait.object_space.get_main.add_local(:r , :Integer) @input = "r = 10.mod4" - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] - assert_nil msg = check_nil , msg - end - - def pest_assign_ivar_notpresent - @input = "@r = 5" - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + @expect = [Label, Label, Label, Label, Label] assert_nil msg = check_nil , msg end def pest_assign_ivar - add_space_field(:r , :Integer) - - @input =s(:statements, s(:i_assignment, s(:ivar, :r), s(:int, 5))) - - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg , - RegToSlot, Label, FunctionReturn] + @input = "@r = 5" + @expect = [Label] assert_nil msg = check_nil , msg end diff --git a/test/mom/test_local_assignment.rb b/test/mom/test_local_assignment.rb new file mode 100644 index 00000000..7262bfd3 --- /dev/null +++ b/test/mom/test_local_assignment.rb @@ -0,0 +1,27 @@ +require_relative 'helper' + +module Risc + class TestLocalAssign < MiniTest::Test + include Statements + + def setup + super + @input = "r = 5" + @expect = [LoadConstant, RegToSlot] + end + def test_local_assign_instructions + assert_nil msg = check_nil , msg + end + + def test_constant_load + produced = produce_body + assert_equal 5 , produced.constant.value + end + + def test_slot_move + produced = produce_body + assert_equal produced.next.register , produced.register + end + + end +end