rubyx/lib/risc/position.rb

138 lines
3.6 KiB
Ruby
Raw Normal View History

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.
#
# The Position class keeps a hash of all compile time positions.
#
# While the Position objects transmit the change that (re) positioning
# entails to affected objects.
class Position
@positions = {}
attr_reader :at
def initialize( at )
@at = at
raise "not int #{self}-#{at}" unless @at.is_a?(Integer)
end
def +(offset)
2018-05-05 19:25:10 +02:00
offset = offset.at if offset.is_a?(Position)
@at + offset
end
def -(offset)
2018-05-05 19:25:10 +02:00
offset = offset.at if offset.is_a?(Position)
@at - offset
end
def to_s
2018-05-05 19:25:10 +02:00
"0x#{@at.to_s(16)}"
end
# just a callback after creation AND insertion
def init(pos)
end
2018-05-05 23:34:59 +02:00
def reset_to(pos)
return false if pos == at
2018-05-05 23:34:59 +02:00
if((at - pos).abs > 1000)
raise "position set too far off #{pos}!=#{at} for #{object}:#{object.class}"
end
@at = pos
true
2018-05-05 23:34:59 +02:00
end
def self.positions
@positions
end
2018-05-06 19:04:02 +02:00
def self.set?(object)
self.positions.has_key?(object)
end
def self.get(object)
pos = self.positions[object]
if pos == nil
str = "position accessed but not set, "
str += "0x#{object.object_id.to_s(16)}\n"
str += "for #{object.class} byte_length #{object.byte_length if object.respond_to?(:byte_length)} for #{object.inspect[0...130]}"
raise str
end
pos
end
2018-05-06 19:04:02 +02:00
def self.set( object , pos , extra = nil)
# resetting of position used to be error, but since relink and dynamic instruction size it is ok.
# in measures (of 32)
#puts "Setting #{pos} for #{self.class}"
old = Position.positions[object]
if old != nil
old.reset_to(pos)
return old
end
position = for_at( object , pos , extra)
self.positions[object] = position
position.init(pos)
position
2018-05-05 23:34:59 +02:00
end
def self.for_at(object , at , extra)
case object
when Parfait::BinaryCode
BPosition.new(object,at , extra)
when Arm::Instruction , Risc::Label
IPosition.new(object,at , extra)
2018-05-05 23:34:59 +02:00
else
Position.new(at)
end
2018-05-05 23:34:59 +02:00
end
end
# handle event propagation
class IPosition < Position
attr_reader :instruction , :binary
def initialize(instruction, pos , binary)
raise "not set " unless binary
2018-05-05 23:34:59 +02:00
super(pos)
@instruction = instruction
@binary = binary
end
def init(at)
return unless instruction.next
at += instruction.byte_length
bin = binary
if( 12 == at % 60)
at = 12
bin = binary.next
end
Position.set(instruction.next, at , binary)
end
def reset_to(pos)
changed = super(pos)
#puts "Reset (#{changed}) #{instruction}"
return unless changed
return unless instruction.next
instruction.next.set_position( pos + instruction.byte_length , 0)
2018-05-05 23:34:59 +02:00
end
2018-05-05 23:34:59 +02:00
end
class BPosition < Position
attr_reader :code , :method
def initialize(code, pos , method)
super(pos)
@code = code
@method = method
end
def init(at)
return unless code.next
Position.set(code.next , at + code.padded_length, method)
end
end
end