start on redoing instruction positions

using insruction listeners (wip)
This commit is contained in:
Torsten Ruger 2018-06-02 21:20:15 +03:00
parent 3bc35c2275
commit 24f6e30b54
7 changed files with 36 additions and 21 deletions

View File

@ -25,7 +25,7 @@ module Arm
super super
my_pos = Risc::Position.get(self) my_pos = Risc::Position.get(self)
# set my position to set next according to rules # set my position to set next according to rules
Risc::Position.set( self , my_pos.at , my_pos.binary) Risc::Position::InstructionListener.init(instruction , my_pos.get_code)
end end
end end
end end

View File

@ -117,14 +117,15 @@ module Risc
# #
# start at code_start. # start at code_start.
def position_code(code_start) def position_code(code_start)
prev = nil prev_code = nil
Parfait.object_space.types.values.each do |type| Parfait.object_space.types.values.each do |type|
next unless type.methods next unless type.methods
type.methods.each_method do |method| type.methods.each_method do |method|
last = Position::CodeListener.init(method.binary , code_start) last_code = Position::CodeListener.init(method.binary , code_start)
last.register_event(:position_changed , prev.object) if prev Position::InstructionListener.init(method.cpu_instructions, method.binary)
prev = last last_code.register_event(:position_changed , prev_code.object) if prev_code
code_start = last.next_slot prev_code = last_code
code_start = last_code.next_slot
end end
end end
#Position.set( first_method.cpu_instructions, code_start + Parfait::BinaryCode.byte_offset , first_method.binary) #Position.set( first_method.cpu_instructions, code_start + Parfait::BinaryCode.byte_offset , first_method.binary)

View File

@ -14,11 +14,10 @@ module Risc
# When propagating positions we have to see that the next position assembles into # When propagating positions we have to see that the next position assembles into
# the same BinaryCode, or else move it and the code along # the same BinaryCode, or else move it and the code along
# #
class InstructionPosition < ObjectPosition class InstructionListener
attr_reader :instruction , :binary attr_reader :instruction , :binary
def initialize(instruction , binary) def initialize(instruction , binary)
pos = 0 pos = 0
super(instruction)
@instruction = instruction @instruction = instruction
@binary = binary @binary = binary
end end
@ -50,21 +49,26 @@ module Risc
# initialize the dependency graph for instructions # initialize the dependency graph for instructions
# #
# starting from the given instruction, create InstructionPositions # starting from the given instruction, create ObjectPositions
# for it and the whole chain # for it and the whole chain. Then attach InstructionListeners
# for dependency tracking. All positions are initialized with -1
# and so setting the first will trigger a chain reaction
# #
# Set the next created to be dependent on the previous # return the position for the first instruction which may be used to
# set all positions in the chain
def self.init( instruction , code ) def self.init( instruction , code )
first = nil
while(instruction) while(instruction)
position = InstructionPosition.new(instruction , code) position = ObjectPosition.new(instruction , -1)
first = position unless first
nekst = instruction.next nekst = instruction.next
if nekst if nekst
listener = InstructionListener.new( nekst ) listener = InstructionListener.new( nekst , code )
position.register_event(:position_changed , listener) position.register_event(:position_changed , listener)
end end
instruction = nekst instruction = nekst
end end
position first
end end
end end
end end

View File

@ -12,10 +12,17 @@ module Risc
# Give an integer as the actual position, where -1 # Give an integer as the actual position, where -1
# which means no legal position known # which means no legal position known
def initialize(object , pos ) def initialize(object , pos )
@at = 0 @at = pos
@object = object @object = object
Position.set_to(self , pos)
end end
#look for InstructionListener and return its code if found
def get_code
listener = event_table.find{|one| one.class == InstructionListener}
return nil unless listener
listener.code
end
def +(offset) def +(offset)
offset = offset.at if offset.is_a?(ObjectPosition) offset = offset.at if offset.is_a?(ObjectPosition)
@at + offset @at + offset

View File

@ -57,7 +57,7 @@ module Risc
end end
def self.set_to( position , to) def self.set_to( position , to)
postest = Position.positions[position.object] postest = Position.positions[position.object] unless to < 0
raise "Mismatch #{position}" if postest and postest != position raise "Mismatch #{position}" if postest and postest != position
@reverse_cache.delete(position.at) unless position.object.is_a? Label @reverse_cache.delete(position.at) unless position.object.is_a? Label
testing = self.at( position.at ) if position.at >= 0 testing = self.at( position.at ) if position.at >= 0
@ -73,5 +73,5 @@ module Risc
end end
require_relative "object_position" require_relative "object_position"
require_relative "object_listener" require_relative "object_listener"
require_relative "instruction_position" require_relative "instruction_listener"
require_relative "code_listener" require_relative "code_listener"

View File

@ -3,7 +3,7 @@ require_relative "helper"
module Risc module Risc
module Position module Position
# tests that require a boot and test propagation # tests that require a boot and test propagation
class TestInstructionPosition < MiniTest::Test class TestInstructionListener < MiniTest::Test
def setup def setup
Risc.machine.boot Risc.machine.boot
@binary = Parfait::BinaryCode.new(1) @binary = Parfait::BinaryCode.new(1)
@ -11,7 +11,7 @@ module Risc
@label = Risc.label("hi","ho") @label = Risc.label("hi","ho")
end end
def test_init def test_init
assert InstructionPosition.init(@label , nil) assert InstructionListener.init(@label , @binary)
end end
def pest_set_instr def pest_set_instr
pos = Position.set( @label , 8 , @binary) pos = Position.set( @label , 8 , @binary)

View File

@ -3,7 +3,7 @@ require_relative "helper"
module Risc module Risc
module Position module Position
# tests that do no require a boot and only test basic positioning # tests that do no require a boot and only test basic positioning
class TestPositionBasic < MiniTest::Test class TestObjectPosition < MiniTest::Test
def test_init def test_init
assert ObjectPosition.init(self , -1) assert ObjectPosition.init(self , -1)
@ -13,6 +13,9 @@ module Risc
position = ObjectPosition.new(mov , 0) position = ObjectPosition.new(mov , 0)
assert_equal 4, position.next_slot assert_equal 4, position.next_slot
end end
def test_has_get_code
assert_nil ObjectPosition.init(self , -1).get_code
end
def pest_creation_ok def pest_creation_ok
assert ObjectPosition.new(self,0) assert ObjectPosition.new(self,0)
end end