2018-06-02 20:59:41 +02:00
|
|
|
require "util/eventable"
|
|
|
|
|
2018-05-05 18:47:18 +02:00
|
|
|
module Risc
|
|
|
|
# Positions are very different during compilation and run-time.
|
|
|
|
# At run-time they are inherrent to the object, and fixed.
|
|
|
|
# While during compilation we can move things about, and do not use the
|
|
|
|
# objects memory position at all.
|
|
|
|
#
|
|
|
|
# Furthermore, there are differnet kind of positions during compilation.
|
|
|
|
# Off course the object position as hinted above, but also instruction
|
|
|
|
# positions, that do not reflect the position of the object, but of the
|
|
|
|
# assembled instruction in the binary.
|
|
|
|
#
|
2018-05-10 19:56:12 +02:00
|
|
|
# The Position module keeps a hash of all compile time positions.
|
2018-05-05 18:47:18 +02:00
|
|
|
#
|
2018-05-10 19:56:12 +02:00
|
|
|
# While the (different)Position objects transmit the change that (re) positioning
|
2018-05-05 18:47:18 +02:00
|
|
|
# entails to affected objects.
|
|
|
|
|
2018-06-02 20:59:41 +02:00
|
|
|
class Position
|
2018-05-20 14:52:13 +02:00
|
|
|
include Util::Logging
|
2018-05-24 13:27:53 +02:00
|
|
|
log_level :info
|
2018-05-20 14:52:13 +02:00
|
|
|
|
2018-06-15 21:00:49 +02:00
|
|
|
INVALID = -1
|
|
|
|
|
2018-06-02 20:59:41 +02:00
|
|
|
include Util::Eventable
|
|
|
|
|
|
|
|
attr_reader :at , :object
|
|
|
|
|
|
|
|
# initialize with a given object, first parameter
|
2018-06-15 21:00:49 +02:00
|
|
|
# The object will be the key in global position map
|
|
|
|
#
|
|
|
|
# The actual position starts as -1 (invalid)
|
|
|
|
def initialize(object)
|
|
|
|
@at = INVALID
|
2018-06-02 20:59:41 +02:00
|
|
|
@object = object
|
2018-07-02 08:35:57 +02:00
|
|
|
raise "Object may not be nil" if object.nil?
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
|
|
|
|
2018-06-02 22:02:59 +02:00
|
|
|
# utility to register events of type :position_changed
|
|
|
|
# can give an object and a PositionListener will be created for it
|
|
|
|
def position_listener(listener)
|
|
|
|
unless listener.class.name.include?("Listener")
|
|
|
|
listener = PositionListener.new(listener)
|
|
|
|
end
|
|
|
|
register_event(:position_changed , listener)
|
|
|
|
end
|
|
|
|
|
2018-06-06 09:00:07 +02:00
|
|
|
# When instruction get inserted, we have to move listeners around, remove given
|
|
|
|
def remove_position_listener(list)
|
|
|
|
unregister_event(:position_changed, list)
|
|
|
|
end
|
|
|
|
|
2018-06-02 22:02:59 +02:00
|
|
|
# utility to get all registered listeners to the :position_changed event
|
|
|
|
# returns an array
|
|
|
|
def position_listeners
|
|
|
|
event_table[:position_changed]
|
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
|
2018-06-02 20:59:41 +02:00
|
|
|
#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
|
|
|
|
|
2018-06-06 00:16:00 +02:00
|
|
|
def valid?
|
2018-06-15 21:00:49 +02:00
|
|
|
@at != INVALID
|
2018-06-06 00:16:00 +02:00
|
|
|
end
|
|
|
|
|
2018-06-02 22:48:12 +02:00
|
|
|
def set(int)
|
2018-06-05 17:11:25 +02:00
|
|
|
return int if int == self.at
|
2018-06-09 07:10:41 +02:00
|
|
|
trigger_changing( int )
|
2018-06-16 09:58:54 +02:00
|
|
|
Position.set_cache(self , int)
|
2018-06-02 22:48:12 +02:00
|
|
|
@at = int
|
2018-06-06 09:19:18 +02:00
|
|
|
trigger_changed
|
2018-06-15 21:00:49 +02:00
|
|
|
self
|
2018-06-02 22:48:12 +02:00
|
|
|
end
|
|
|
|
|
2018-06-09 07:10:41 +02:00
|
|
|
# helper to fire the event that the position is about to change
|
|
|
|
# the argument is the new position (as int)
|
|
|
|
def trigger_changing( to )
|
|
|
|
event_table[:position_changed].each { |handler| handler.position_changing( self , to) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# helper to fire the event that the position has changed
|
2018-06-06 09:19:18 +02:00
|
|
|
# Note: set checks if the position actually has changed, before fireing
|
|
|
|
# but during insert it is helpful to trigger just to set the next
|
|
|
|
def trigger_changed
|
|
|
|
trigger(:position_changed , self )
|
|
|
|
end
|
|
|
|
|
2018-06-06 09:00:07 +02:00
|
|
|
def trigger_inserted
|
|
|
|
event_table[:position_changed].each { |handler| handler.position_inserted( self) }
|
|
|
|
end
|
|
|
|
|
2018-06-02 20:59:41 +02:00
|
|
|
def +(offset)
|
|
|
|
offset = offset.at if offset.is_a?(Position)
|
|
|
|
@at + offset
|
|
|
|
end
|
|
|
|
|
|
|
|
def -(offset)
|
|
|
|
offset = offset.at if offset.is_a?(Position)
|
|
|
|
@at - offset
|
|
|
|
end
|
2018-06-05 23:53:41 +02:00
|
|
|
|
|
|
|
def <(right)
|
|
|
|
right = right.at if right.is_a?(Position)
|
|
|
|
@at < right
|
|
|
|
end
|
|
|
|
|
|
|
|
def >(right)
|
|
|
|
right = right.at if right.is_a?(Position)
|
|
|
|
@at > right
|
|
|
|
end
|
|
|
|
|
2018-06-02 20:59:41 +02:00
|
|
|
def to_s
|
|
|
|
"0x#{@at.to_s(16)}"
|
|
|
|
end
|
|
|
|
|
2018-06-07 18:27:44 +02:00
|
|
|
def object_class
|
|
|
|
return :object if @object.is_a?(Parfait::Object)
|
|
|
|
return :object if @object.class.name.include?("Test")
|
|
|
|
:instruction
|
|
|
|
end
|
|
|
|
|
2018-06-02 20:59:41 +02:00
|
|
|
def next_slot
|
|
|
|
return -1 if at < 0
|
2018-06-09 07:10:41 +02:00
|
|
|
slot = at + object.padded_length
|
|
|
|
self.log.debug "Next Slot @#{at.to_s(16)} for #{object.class}(#{object.padded_length.to_s(16)}) == #{(slot).to_s(16)}"
|
|
|
|
slot
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
## class level forward and reverse cache
|
2018-05-05 18:47:18 +02:00
|
|
|
@positions = {}
|
2018-05-24 13:27:53 +02:00
|
|
|
@reverse_cache = {}
|
2018-05-05 18:47:18 +02:00
|
|
|
|
|
|
|
def self.positions
|
|
|
|
@positions
|
|
|
|
end
|
|
|
|
|
2018-05-24 13:27:53 +02:00
|
|
|
def self.clear_positions
|
|
|
|
@positions = {}
|
|
|
|
@reverse_cache = {}
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
|
2018-05-17 19:13:33 +02:00
|
|
|
def self.at( int )
|
2018-05-24 13:27:53 +02:00
|
|
|
@reverse_cache[int]
|
2018-05-17 19:13:33 +02:00
|
|
|
end
|
|
|
|
|
2018-05-06 19:04:02 +02:00
|
|
|
def self.set?(object)
|
2018-06-19 16:35:00 +02:00
|
|
|
self.positions[object]
|
2018-05-06 19:04:02 +02:00
|
|
|
end
|
|
|
|
|
2018-06-05 17:11:25 +02:00
|
|
|
# get a position from the cache (object -> position)
|
|
|
|
# unless it's a label, then get the position of it's next
|
2018-05-06 19:04:02 +02:00
|
|
|
def self.get(object)
|
2018-05-05 18:47:18 +02:00
|
|
|
pos = self.positions[object]
|
|
|
|
if pos == nil
|
2018-06-02 20:59:41 +02:00
|
|
|
str = "position accessed but not initialized, "
|
2018-05-05 18:47:18 +02:00
|
|
|
str += "0x#{object.object_id.to_s(16)}\n"
|
2018-06-16 09:58:54 +02:00
|
|
|
str += "class: #{object.class} "
|
2018-05-30 09:29:38 +02:00
|
|
|
str += "byte_length #{object.byte_length}" if object.respond_to?(:byte_length)
|
2018-06-16 09:58:54 +02:00
|
|
|
str += " object: #{object.to_s[0...130]}"
|
2018-05-05 18:47:18 +02:00
|
|
|
raise str
|
|
|
|
end
|
|
|
|
pos
|
|
|
|
end
|
|
|
|
|
2018-06-15 21:00:49 +02:00
|
|
|
# creating means instantiating and caching
|
|
|
|
def self.create(object)
|
|
|
|
pos = Position.new(object)
|
|
|
|
self.positions[object] = pos
|
|
|
|
log.debug "Initialize for #{object.class} #{object.object_id.to_s(16)}"
|
|
|
|
pos
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_or_create(object)
|
|
|
|
if self.positions.has_key?(object)
|
|
|
|
pos = self.get(object)
|
|
|
|
else
|
|
|
|
pos = self.create(object)
|
|
|
|
end
|
|
|
|
return pos
|
|
|
|
end
|
|
|
|
|
2018-06-05 17:11:25 +02:00
|
|
|
# populate the position caches (forward and revese) with the given position
|
|
|
|
# forward caches object -> position
|
|
|
|
# reverse caches position.at > position
|
|
|
|
# Labels do not participatein reverse cache
|
2018-06-16 09:58:54 +02:00
|
|
|
def self.set_cache( position , to)
|
2018-06-02 20:59:41 +02:00
|
|
|
postest = Position.positions[position.object] unless to < 0
|
2018-06-02 15:12:01 +02:00
|
|
|
raise "Mismatch #{position}" if postest and postest != position
|
2018-06-05 17:11:25 +02:00
|
|
|
@reverse_cache.delete(position.at) unless position.object.is_a?(Label)
|
2018-06-02 20:59:41 +02:00
|
|
|
testing = self.at( position.at ) unless position.at < 0
|
2018-06-07 18:27:44 +02:00
|
|
|
if testing and testing.object_class != position.object_class
|
|
|
|
raise "Mismatch (at #{to.to_s(16)}) new:#{position} #{position.object.class} , was:#{testing}#{testing.object.class}"
|
2018-05-31 18:01:10 +02:00
|
|
|
end
|
2018-06-02 15:12:01 +02:00
|
|
|
self.positions[position.object] = position
|
2018-06-05 17:11:25 +02:00
|
|
|
@reverse_cache[to] = position unless position.object.is_a?(Label)
|
2018-06-15 21:00:49 +02:00
|
|
|
if to == INVALID
|
|
|
|
raise "Old style, change code"
|
2018-06-09 08:16:23 +02:00
|
|
|
else
|
|
|
|
log.debug "Set #{position} to 0x#{to.to_s(16)} for #{position.object.class} #{position.object.object_id.to_s(16)}"
|
|
|
|
end
|
2018-05-07 21:30:43 +02:00
|
|
|
position
|
2018-05-05 23:34:59 +02:00
|
|
|
end
|
2018-06-15 21:00:49 +02:00
|
|
|
|
|
|
|
def self.is_object(object)
|
|
|
|
case object
|
|
|
|
when Risc::Label , Parfait::BinaryCode
|
|
|
|
return false
|
|
|
|
when Parfait::Object , Symbol
|
|
|
|
return true
|
|
|
|
when Arm::Instruction , Risc::Branch
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
raise "Class #{object.class}"
|
|
|
|
end
|
|
|
|
end
|
2018-05-05 23:34:59 +02:00
|
|
|
end
|
2018-05-05 18:47:18 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
require_relative "position_listener"
|
2018-06-02 20:20:15 +02:00
|
|
|
require_relative "instruction_listener"
|
2018-06-17 21:25:38 +02:00
|
|
|
require_relative "branch_listener"
|
2018-06-02 15:34:44 +02:00
|
|
|
require_relative "code_listener"
|
2018-06-09 21:13:43 +02:00
|
|
|
require_relative "label_listener"
|