From 56f2742e894d7ed5fd4f10092299c134e23620e3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 2 Jul 2018 23:03:00 +0300 Subject: [PATCH] fix code listener logic bug next's position was only updated initially previous logic relied on pushing everything long rightly but it wasn't right, and no harm in setting position twice, as events only fire if there is real change --- lib/risc/linker.rb | 2 +- lib/risc/position/code_listener.rb | 11 ++++++----- lib/risc/position/instruction_listener.rb | 5 ++++- test/risc/position/helper.rb | 5 ++--- .../risc/position/test_instruction_listener1.rb | 2 +- test/risc/test_linker.rb | 14 -------------- test/risc/test_linker1.rb | 17 +++++++++++++++++ 7 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 test/risc/test_linker1.rb diff --git a/lib/risc/linker.rb b/lib/risc/linker.rb index 6b8d053e..4bced8ce 100644 --- a/lib/risc/linker.rb +++ b/lib/risc/linker.rb @@ -69,7 +69,7 @@ module Risc previous.position_listener(objekt) if previous previous = position at += objekt.padded_length - log.debug "Object #{objekt.class}:#{before.to_s(16)} len: #{(at - before).to_s(16)}" + log.debug "Object #{objekt.class}:0x#{before.to_s(16)} len: #{(at - before).to_s(16)}" end at end diff --git a/lib/risc/position/code_listener.rb b/lib/risc/position/code_listener.rb index f5f511fc..72e3d0c5 100644 --- a/lib/risc/position/code_listener.rb +++ b/lib/risc/position/code_listener.rb @@ -17,21 +17,22 @@ module Risc def position_inserted(position) Position.log.debug "extending one at #{position}" pos = CodeListener.init( position.object.next , @platform) + raise "HI #{position}" unless position.valid? return unless position.valid? - puts "insert #{position.object.next.object_id.to_s(16)}" unless position.valid? + Position.log.debug "insert #{position.object.next.object_id.to_s(16)}" pos.set( position + position.object.padded_length) set_jump_for(position) end def position_changed(position) + Position.log.debug "Code changed #{position}" nekst = position.object.next if( nekst ) nekst_pos = Position.get(nekst) - unless(nekst_pos.valid?) - nekst_pos.set(position + position.object.padded_length) - end + Position.log.debug "Code changed, setting next #{position}" + nekst_pos.set(position + position.object.padded_length) + set_jump_for(position) end - set_jump_for(position) end def position_changing(position , to) diff --git a/lib/risc/position/instruction_listener.rb b/lib/risc/position/instruction_listener.rb index 3c7f341a..d476031d 100644 --- a/lib/risc/position/instruction_listener.rb +++ b/lib/risc/position/instruction_listener.rb @@ -25,7 +25,7 @@ module Risc # Taking into account that BinaryCodes only take 13 instructions, # meaning that chain may have to be extended def position_changing(position , to) - Position.log.debug "Changing #{position} to 0x#{to.to_s(16)}, bin #{Position.get(@binary)}" + Position.log.debug "Instruction from #{position} to 0x#{to.to_s(16)}, bin #{Position.get(@binary)}" update_index(to) instruction = position.object return unless instruction.next @@ -43,8 +43,11 @@ module Risc index = (to - Position.get(@binary).at) / 4 raise "Invalid negative index #{index} , #{Position.get(@binary)}" if index < Parfait::BinaryCode.type_length while(index >= (Parfait::BinaryCode.memory_size - 1) ) + Position.log.debug "Binary Extend (#{index}) #{Position.get(@binary)}, 0x#{@binary.object_id.to_s(16)}" @binary = @binary.ensure_next + Position.log.debug "Binary Extended (#{index}) #{Position.get(@binary)}" index = (to - Position.get(@binary).at) / 4 + Position.log.debug "Binary Extended new index (#{index}) #{Position.get(@binary)}" end @index = index - 2 raise "Invalid negative index #{@index} , #{Position.get(@binary)}" if index < 0 diff --git a/test/risc/position/helper.rb b/test/risc/position/helper.rb index f2ba37fa..c33346d6 100644 --- a/test/risc/position/helper.rb +++ b/test/risc/position/helper.rb @@ -21,9 +21,8 @@ module Risc end class DummyPlatform def self.boot - machine = Risc.machine.boot - machine.set_platform( self.new ) - machine + Parfait.boot! + Risc.boot! end def translator DummyTranslator.new diff --git a/test/risc/position/test_instruction_listener1.rb b/test/risc/position/test_instruction_listener1.rb index 62efa837..a9649d8d 100644 --- a/test/risc/position/test_instruction_listener1.rb +++ b/test/risc/position/test_instruction_listener1.rb @@ -5,7 +5,7 @@ module Risc def setup DummyPlatform.boot @binary = Parfait::BinaryCode.new(1) - @bin_pos = CodeListener.init(@binary).set(0) + @bin_pos = CodeListener.init(@binary, :interpreter).set(0) @instruction = DummyInstruction.new 13.times {@instruction.last.insert(DummyInstruction.new) } @position = InstructionListener.init(@instruction , @binary) diff --git a/test/risc/test_linker.rb b/test/risc/test_linker.rb index 62660cab..7e07b036 100644 --- a/test/risc/test_linker.rb +++ b/test/risc/test_linker.rb @@ -17,19 +17,6 @@ module Risc assert_raises {@machine.add_constant( 1 )} end end - class TestMachinePos < MiniTest::Test - def setup - Parfait.boot! - Risc.boot! - @linker = Mom::MomCompiler.new.translate(:arm) - @linker.position_all - end - def test_positions_set - @linker.object_positions.each do |obj , position| - assert Position.get(obj).valid? , "#{Position.get(obj)} , #{obj.object_id.to_s(16)}" - end - end - end class TestMachineInit < MiniTest::Test def setup Parfait.boot! @@ -60,6 +47,5 @@ module Risc assert position.valid? , "#{position} , #{obj.object_id.to_s(16)}" end end - end end diff --git a/test/risc/test_linker1.rb b/test/risc/test_linker1.rb new file mode 100644 index 00000000..79461f67 --- /dev/null +++ b/test/risc/test_linker1.rb @@ -0,0 +1,17 @@ +require_relative "helper" + +module Risc + class TestMachinePos < MiniTest::Test + def setup + Parfait.boot! + Risc.boot! + @linker = RubyX::RubyXCompiler.new("class Space; def main(arg);a = 1;return a;end;end").ruby_to_risc(:arm) + @linker.position_all + end + def test_positions_set + @linker.object_positions.each do |obj , position| + assert Position.get(obj).valid? , "#{Position.get(obj)} , #{obj.object_id.to_s(16)}" + end + end + end +end