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 21:35:56 +02:00
|
|
|
def assemble
|
|
|
|
link
|
|
|
|
@stream = StringIO.new
|
|
|
|
assemble_object( @space )
|
|
|
|
puts "leng #{@stream.length}"
|
|
|
|
end
|
|
|
|
|
2014-08-27 16:23:50 +02:00
|
|
|
def link_object(object , at)
|
2014-08-26 21:35:56 +02:00
|
|
|
slot = @objects[object.object_id]
|
2014-08-27 16:23:50 +02:00
|
|
|
unless slot
|
|
|
|
slot = LinkSlot.new at
|
|
|
|
@objects[object.object_id] = slot
|
|
|
|
end
|
2014-08-26 21:35:56 +02:00
|
|
|
if object.is_a? Instruction
|
2014-08-27 16:23:50 +02:00
|
|
|
total_length = 4
|
2014-08-26 21:35:56 +02:00
|
|
|
else
|
|
|
|
clazz = object.class.name.split("::").last
|
2014-08-27 16:23:50 +02:00
|
|
|
object_length = 0 #link_self(object , at)
|
|
|
|
total_length = object_length + send("link_#{clazz}".to_sym , object , at + object_length)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2014-08-27 16:23:50 +02:00
|
|
|
slot.length = total_length
|
|
|
|
total_length
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-08-27 16:23:50 +02:00
|
|
|
def assemble_object object
|
2014-08-25 16:03:39 +02:00
|
|
|
slot = @objects[object.object_id]
|
2014-08-27 16:23:50 +02:00
|
|
|
raise "Object not linked #{object_id}" unless slot
|
2014-08-26 15:36:12 +02:00
|
|
|
if object.is_a? Instruction
|
2014-08-27 16:23:50 +02:00
|
|
|
object.assemble( @stream , self )
|
2014-08-26 21:35:56 +02:00
|
|
|
else
|
2014-08-26 15:36:12 +02:00
|
|
|
clazz = object.class.name.split("::").last
|
2014-08-27 16:23:50 +02:00
|
|
|
len = send("assemble_#{clazz}".to_sym , object)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-27 16:23:50 +02:00
|
|
|
def link_Array( array , at)
|
|
|
|
length = 0
|
|
|
|
array.each do |elem|
|
|
|
|
length += link_object(elem , at + length)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2014-08-27 16:23:50 +02:00
|
|
|
length
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_BootSpace(space , at)
|
|
|
|
length = link_Array( space.classes.values , at )
|
|
|
|
length + link_Array(space.objects , at + length)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble_BootSpace(space)
|
|
|
|
# assemble in the same order as linked
|
|
|
|
space.classes.values.each do |cl|
|
|
|
|
assemble_object(cl)
|
|
|
|
end
|
|
|
|
space.objects.each do |o|
|
|
|
|
assemble_object(o)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link_BootClass(clazz , at)
|
2014-08-27 16:23:50 +02:00
|
|
|
length = link_object(clazz.name , at)
|
|
|
|
length += link_object(clazz.super_class_name , at + length)
|
|
|
|
length + link_Array(clazz.instance_methods , at + length)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble_BootClass(clazz)
|
|
|
|
assemble_object(clazz.name)
|
|
|
|
assemble_object(clazz.super_class_name)
|
|
|
|
clazz.instance_methods.each do |meth|
|
|
|
|
assemble_object(meth)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link_MethodDefinition(method , at)
|
2014-08-27 16:23:50 +02:00
|
|
|
length = link_object(method.name ,at)
|
|
|
|
length + link_Array(method.blocks ,at + length)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble_MethodDefinition(method)
|
|
|
|
assemble_object(method.name)
|
|
|
|
method.blocks.each do |block|
|
|
|
|
assemble_object(block)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble_Block(block)
|
|
|
|
block.codes.each do |code|
|
|
|
|
assemble_object(code)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble_String( str )
|
|
|
|
@stream.write str
|
|
|
|
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-26 21:35:56 +02:00
|
|
|
def assemble_Symbol(sym)
|
|
|
|
return assemble_String(sym.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_StringConstant( sc , at)
|
|
|
|
return link_String(sc.string,at)
|
|
|
|
end
|
|
|
|
def assemble_StringConstant( sc)
|
|
|
|
return assemble_String(sc.string)
|
|
|
|
end
|
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
|