rubyx/lib/risc/position/object_position.rb
Torsten Ruger 24f6e30b54 start on redoing instruction positions
using insruction listeners (wip)
2018-06-02 21:20:15 +03:00

61 lines
1.6 KiB
Ruby

require "util/eventable"
module Risc
module Position
class ObjectPosition
include Util::Eventable
attr_reader :at , :object
# initialize with a given object, first parameter
# The object ill be the key in global position map
# Give an integer as the actual position, where -1
# which means no legal position known
def initialize(object , pos )
@at = pos
@object = object
Position.set_to(self , pos)
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)
offset = offset.at if offset.is_a?(ObjectPosition)
@at + offset
end
def -(offset)
offset = offset.at if offset.is_a?(ObjectPosition)
@at - offset
end
def to_s
"0x#{@at.to_s(16)}"
end
# just a callback after creation AND insertion
def init(pos , is_nil)
end
def reset_to(pos , guaranteed_nil )
return false if pos == at
if((at - pos).abs > 1000)
raise "position set too far off #{pos}!=#{at} for #{object}:#{object.class}"
end
@at = pos
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)
end
end
end
end