rubyx/lib/register/linker.rb

96 lines
2.4 KiB
Ruby
Raw Normal View History

module Register
class LinkSlot
def initialize at
@position = position
@length = 0
end
attr_accessor :position , :length
end
class Linker
def initialize
@objects = {}
end
def get_slot(object)
@objects[object.object_id]
end
def link_object(object , at)
slot = @objects[object.object_id]
unless slot
slot = LinkSlot.new at
@objects[object.object_id] = slot
end
if object.is_a? Instruction
2014-08-28 07:08:11 +02:00
length = 4
else
clazz = object.class.name.split("::").last
2014-08-28 07:08:11 +02:00
length = send("link_#{clazz}".to_sym , object , at)
end
2014-08-28 07:08:11 +02:00
slot.length = length
length
end
def link_Array( array , at)
length = 0
array.each do |elem|
length += link_object(elem , at + length)
end
2014-08-28 07:08:11 +02:00
# also array has constant overhead, the members helper fixes it to multiple of 8
members(length)
end
def link_BootSpace(space , at)
2014-08-28 07:08:11 +02:00
length = members( 2 )
length += link_Array( space.classes.values , at + length )
length + link_Array(space.objects , at + length)
end
def link_BootClass(clazz , at)
2014-08-28 07:08:11 +02:00
length = members(3)
length += link_object(clazz.name , at + length)
length += link_object(clazz.super_class_name , at + length)
length + link_Array(clazz.instance_methods , at + length)
end
def link_CompiledMethod(method , at)
2014-08-28 07:08:11 +02:00
length = members(2)
length += link_object(method.name ,at + length)
# NOT an ARRAY, just a bag of bytes
method.blocks.each do |block|
length += link_object( block ,at + length)
end
length
end
def link_Block(block , at)
len = 0
# NOT linking as an array, as we need the strem that makes the method
block.codes.each do |code|
len += link_object(code , at + len)
end
len
end
def link_String( str , at)
2014-08-28 15:19:43 +02:00
return members(str.length / 4)
end
def link_Symbol(sym , at)
return link_String(sym.to_s , at)
end
def link_StringConstant( sc , at)
return link_String(sc.string,at)
end
2014-08-28 07:08:11 +02:00
# objects only come in lengths of multiple of 8
# but there is a constant overhead of 2, one for type, one for layout
# and as we would have to subtract 1 to make it work without overhead, we now have to add 1
def members len
8 * (1 + (len + 1) / 8)
end
end
end