2014-08-25 16:03:39 +02:00
|
|
|
module Register
|
|
|
|
class LinkSlot
|
|
|
|
def initialize at
|
|
|
|
@position = position
|
2014-08-26 15:36:12 +02:00
|
|
|
@length = 0
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
attr_accessor :position , :length
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
class Assembler
|
|
|
|
|
|
|
|
def initialize space
|
|
|
|
@space = space
|
|
|
|
@objects = {}
|
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link
|
|
|
|
link_object(@space , 0)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link_object(object , at)
|
2014-08-25 16:03:39 +02:00
|
|
|
slot = @objects[object.object_id]
|
|
|
|
unless slot
|
|
|
|
slot = LinkSlot.new at
|
|
|
|
@objects[object.object_id] = slot
|
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
if object.is_a? Instruction
|
|
|
|
clazz = object.class.name.split("::").last
|
|
|
|
len = send("link_#{clazz}".to_sym , object , at)
|
|
|
|
else
|
|
|
|
len = 4
|
|
|
|
end
|
|
|
|
slot.length = len
|
|
|
|
len
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link_BootSpace(space , at)
|
|
|
|
len = 0
|
2014-08-25 16:03:39 +02:00
|
|
|
space.classes.values.each do |cl|
|
2014-08-26 15:36:12 +02:00
|
|
|
len += link_object(cl , at + len)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
len
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link_BootClass(clazz , at)
|
|
|
|
len = link_object(clazz.name , at)
|
|
|
|
len += link_object(clazz.super_class_name , at + len)
|
2014-08-25 16:03:39 +02:00
|
|
|
clazz.instance_methods.each do |meth|
|
2014-08-26 15:36:12 +02:00
|
|
|
len += link_object(meth , at + len)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
len
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
|
|
|
def link_MethodDefinition(method , at)
|
|
|
|
len = link_object method.name ,at
|
2014-08-25 16:03:39 +02:00
|
|
|
method.blocks.each do |block|
|
2014-08-26 15:36:12 +02:00
|
|
|
link_object( block , at + len)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
len
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
|
|
|
def link_Block(block , at)
|
|
|
|
len = 0
|
2014-08-25 16:03:39 +02:00
|
|
|
block.codes.each do |code|
|
2014-08-26 15:36:12 +02:00
|
|
|
len += link_object(code , at + len)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
len
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
|
|
|
def link_String( str , at)
|
|
|
|
return (str.length / 4) + 1 + 2
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
|
|
|
def link_Symbol(sym , at)
|
|
|
|
return link_String(sym.to_s , at)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
2014-08-26 10:50:43 +02:00
|
|
|
Sof::Volotile.add(Register::Assembler , [:objects])
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|