fold position module and object position
simpler that way, aslo code is moving to listners
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
require "util/eventable"
|
||||
|
||||
module Risc
|
||||
# Positions are very different during compilation and run-time.
|
||||
# At run-time they are inherrent to the object, and fixed.
|
||||
@ -14,10 +16,50 @@ module Risc
|
||||
# While the (different)Position objects transmit the change that (re) positioning
|
||||
# entails to affected objects.
|
||||
|
||||
module Position
|
||||
class Position
|
||||
include Util::Logging
|
||||
log_level :info
|
||||
|
||||
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?(Position)
|
||||
@at + offset
|
||||
end
|
||||
|
||||
def -(offset)
|
||||
offset = offset.at if offset.is_a?(Position)
|
||||
@at - offset
|
||||
end
|
||||
def to_s
|
||||
"0x#{@at.to_s(16)}"
|
||||
end
|
||||
|
||||
def next_slot
|
||||
return -1 if at < 0
|
||||
at + object.byte_length
|
||||
end
|
||||
|
||||
## class level forward and reverse cache
|
||||
@positions = {}
|
||||
@reverse_cache = {}
|
||||
|
||||
@ -41,7 +83,7 @@ module Risc
|
||||
def self.get(object)
|
||||
pos = self.positions[object]
|
||||
if pos == nil
|
||||
str = "position accessed but not set, "
|
||||
str = "position accessed but not initialized, "
|
||||
str += "0x#{object.object_id.to_s(16)}\n"
|
||||
str += "for #{object.class} "
|
||||
str += "byte_length #{object.byte_length}" if object.respond_to?(:byte_length)
|
||||
@ -51,16 +93,11 @@ module Risc
|
||||
pos
|
||||
end
|
||||
|
||||
def self.reset(obj)
|
||||
old = self.get(obj)
|
||||
old.reset_to( old.at )
|
||||
end
|
||||
|
||||
def self.set_to( position , to)
|
||||
postest = Position.positions[position.object] unless to < 0
|
||||
postest = Position.positions[position.object] unless to < 0
|
||||
raise "Mismatch #{position}" if postest and postest != position
|
||||
@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 ) unless position.at < 0
|
||||
if testing and testing.class != position.class
|
||||
raise "Mismatch (at #{pos.to_s(16)}) was:#{position} #{position.class} #{position.object} , should #{testing}#{testing.class}"
|
||||
end
|
||||
@ -71,7 +108,6 @@ module Risc
|
||||
end
|
||||
end
|
||||
end
|
||||
require_relative "object_position"
|
||||
require_relative "object_listener"
|
||||
require_relative "instruction_listener"
|
||||
require_relative "code_listener"
|
||||
|
Reference in New Issue
Block a user