From 3bc35c2275f4fec0c824c4977af9ed9b149ed311 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 2 Jun 2018 17:29:38 +0300 Subject: [PATCH] rework binary code positioning setup dependency chain set up explicitly. Next have to react to events correctly --- lib/risc/machine.rb | 19 +++++++++++++------ lib/risc/position/code_listener.rb | 1 + lib/risc/position/object_position.rb | 4 ++++ test/risc/position/test_code_listener.rb | 8 ++++++-- test/risc/position/test_object_position.rb | 7 ++++++- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index a8912808..de36c2bf 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -117,12 +117,19 @@ module Risc # # start at code_start. def position_code(code_start) - first_method = Parfait.object_space.types.values.first.methods - before = code_start - Position.set( first_method.binary , code_start , first_method) - Position.set( first_method.cpu_instructions, code_start + Parfait::BinaryCode.byte_offset , first_method.binary) - log.debug "Method #{first_method.name}:#{before.to_s(16)} len: #{(code_start - before).to_s(16)}" - log.debug "Instructions #{first_method.cpu_instructions.object_id.to_s(16)}:#{(before+Parfait::BinaryCode.byte_offset).to_s(16)}" + prev = nil + Parfait.object_space.types.values.each do |type| + next unless type.methods + type.methods.each_method do |method| + last = Position::CodeListener.init(method.binary , code_start) + last.register_event(:position_changed , prev.object) if prev + prev = last + code_start = last.next_slot + end + end + #Position.set( first_method.cpu_instructions, code_start + Parfait::BinaryCode.byte_offset , first_method.binary) + #log.debug "Method #{first_method.name}:#{before.to_s(16)} len: #{(code_start - before).to_s(16)}" + #log.debug "Instructions #{first_method.cpu_instructions.object_id.to_s(16)}:#{(before+Parfait::BinaryCode.byte_offset).to_s(16)}" end # Create Binary code for all methods and the initial jump diff --git a/lib/risc/position/code_listener.rb b/lib/risc/position/code_listener.rb index da64fd38..a965bc68 100644 --- a/lib/risc/position/code_listener.rb +++ b/lib/risc/position/code_listener.rb @@ -72,6 +72,7 @@ module Risc at += code.padded_length unless at < 0 code = code.next end + position end end end diff --git a/lib/risc/position/object_position.rb b/lib/risc/position/object_position.rb index 68ba9eda..fb21ea75 100644 --- a/lib/risc/position/object_position.rb +++ b/lib/risc/position/object_position.rb @@ -40,6 +40,10 @@ module Risc trigger(:position_changed , self) true end + def next_slot + return -1 if at < 0 + at + object.byte_length + end def self.init(object , at = -1) position = ObjectPosition.new(object , at) Position.set_to( position , at) diff --git a/test/risc/position/test_code_listener.rb b/test/risc/position/test_code_listener.rb index 010055e6..780e2c45 100644 --- a/test/risc/position/test_code_listener.rb +++ b/test/risc/position/test_code_listener.rb @@ -12,8 +12,12 @@ module Risc end def test_has_init - CodeListener.init(@binary) - assert Position.get(@binary) + pos = CodeListener.init(@binary) + assert_equal pos, Position.get(@binary) + end + def test_init_returns_position + + assert_equal Position::ObjectPosition , CodeListener.init(@binary).class end def test_init_listner @binary.extend_one diff --git a/test/risc/position/test_object_position.rb b/test/risc/position/test_object_position.rb index 568ad54d..c202e4ee 100644 --- a/test/risc/position/test_object_position.rb +++ b/test/risc/position/test_object_position.rb @@ -6,7 +6,12 @@ module Risc class TestPositionBasic < MiniTest::Test def test_init - assert_equal ObjectPosition.init(self) + assert ObjectPosition.init(self , -1) + end + def test_next_slot + mov = Arm::ArmMachine.mov(:r1 , :r1) + position = ObjectPosition.new(mov , 0) + assert_equal 4, position.next_slot end def pest_creation_ok assert ObjectPosition.new(self,0)