start on insertion events and handling

This commit is contained in:
Torsten Ruger
2018-06-06 10:00:07 +03:00
parent 4789b63fcb
commit c22aff4c4f
7 changed files with 93 additions and 26 deletions

View File

@ -22,10 +22,9 @@ module Arm
end
def insert(instruction)
super
my_pos = Risc::Position.get(self)
listener = Risc::InstructionListener.new( instruction , my_pos.get_code )
my_pos.position_listener(listener)
ret = super
Risc::Position.get(self).trigger_inserted if Risc::Position.set?(self)
ret
end
end
end

View File

@ -19,6 +19,10 @@ module Risc
@binary = binary
end
# if the position of the instruction before us changes, we need to
# adjust the position of this instruction accordingly
# Taking into account that BinaryCodes only take 13 instructions,
# meaning that chain may have to be extended
def position_changed(position)
fix_binary
my_pos = Position.get(@instruction)
@ -34,6 +38,32 @@ module Risc
my_pos.set(next_pos)
end
# When this is called, only the actual insert has happened (keeping the
# position logic out of the instruction assembly).
#
# This event happens at the listener that was dependent on the position
# before the insert, ie:
# position : the arg is the first instruction in the chain where insert happened
# position.object.next : is the newly inserted instruction we need to setup
# @instruction : previously dependent on position, now on .next's position
#
# So we need to :
# - move the listener of @instruction to listen to the inserted instruction
# - add a listener for the new instruction (to listen to the arg)
# - set the new position (moving the chain along)
def position_inserted(position)
inserted = position.object.next
raise "uups" unless inserted ## TODO: remove
position.remove_position_listener(InstructionListener)
new_pos = Position.get(inserted)
new_pos.position_listener(old_listener)
my_pos = Position.get(@instruction)
listen = InstructionListener.new(position.object , @binary)
my_pos.position_listener(listen)
end
# check that the binary we use is the one where the current position falls
# if not move up and register/unregister (soon)
def fix_binary

View File

@ -43,6 +43,11 @@ module Risc
register_event(:position_changed , listener)
end
# When instruction get inserted, we have to move listeners around, remove given
def remove_position_listener(list)
unregister_event(:position_changed, list)
end
# utility to get all registered listeners to the :position_changed event
# returns an array
def position_listeners
@ -68,6 +73,10 @@ module Risc
int
end
def trigger_inserted
event_table[:position_changed].each { |handler| handler.position_inserted( self) }
end
def +(offset)
offset = offset.at if offset.is_a?(Position)
@at + offset