2014-08-27 20:44:23 +02:00
|
|
|
|
2014-08-25 16:03:39 +02:00
|
|
|
module Register
|
2014-08-29 16:20:59 +02:00
|
|
|
class LinkSlot
|
2014-08-29 20:00:25 +02:00
|
|
|
def initialize
|
|
|
|
@position = -1
|
|
|
|
@length = -1
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
attr_accessor :position , :length , :layout
|
|
|
|
end
|
2014-08-29 20:00:25 +02:00
|
|
|
|
|
|
|
# Assmble the object space into a binary.
|
|
|
|
# Link first to get positions, then assemble
|
|
|
|
# link and assemble functions for each class are close to each other, so to get them the same.
|
|
|
|
# meaning: as the link function determines the length of an object and the assemble actually writes the bytes
|
|
|
|
# they are pretty much dependant. In an earlier version they were functions on the objects, but now it
|
|
|
|
# has gone to a visitor pattern.
|
2014-08-25 16:03:39 +02:00
|
|
|
class Assembler
|
|
|
|
|
|
|
|
def initialize space
|
|
|
|
@space = space
|
2014-08-29 16:20:59 +02:00
|
|
|
@objects = {}
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link
|
2014-08-29 20:00:25 +02:00
|
|
|
link_object(@space)
|
|
|
|
at = 0
|
|
|
|
@objects.each do |id , slot|
|
|
|
|
slot.position = at
|
|
|
|
at += slot.length
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
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-29 20:00:25 +02:00
|
|
|
def link_object(object)
|
2014-08-29 16:20:59 +02:00
|
|
|
slot = @objects[object.object_id]
|
|
|
|
unless slot
|
2014-08-29 20:00:25 +02:00
|
|
|
slot = LinkSlot.new
|
2014-08-29 16:20:59 +02:00
|
|
|
@objects[object.object_id] = slot
|
|
|
|
end
|
2014-08-29 20:00:25 +02:00
|
|
|
slot.layout = layout_for(object)
|
|
|
|
clazz = object.class.name.split("::").last
|
|
|
|
slot.length = send("link_#{clazz}".to_sym , object)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-27 16:23:50 +02:00
|
|
|
def assemble_object object
|
2014-08-27 20:44:23 +02:00
|
|
|
slot = get_slot(object)
|
2014-08-29 14:49:59 +02:00
|
|
|
raise "Object not linked #{object_id}=>#{object.class}" unless slot
|
2014-08-29 20:00:25 +02:00
|
|
|
clazz = object.class.name.split("::").last
|
|
|
|
send("assemble_#{clazz}".to_sym , object)
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_layout(object)
|
|
|
|
slot = get_slot(object)
|
2014-08-29 16:20:59 +02:00
|
|
|
layout = slot.layout
|
2014-08-29 20:00:25 +02:00
|
|
|
length = link_object(layout[:names])
|
|
|
|
padded( layout[:names].length)
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
# write type and layout of the instance, and the variables that are passed
|
|
|
|
# variables ar values, ie int or refs. For refs the object needs to save the object first
|
|
|
|
def assemble_self( object , variables )
|
2014-08-29 16:20:59 +02:00
|
|
|
slot = get_slot(object)
|
2014-08-29 14:49:59 +02:00
|
|
|
raise "Object not linked #{object.inspect}" unless slot
|
|
|
|
layout = slot.layout
|
2014-08-29 20:00:25 +02:00
|
|
|
@stream.write_uint32( 0 ) #TODO types
|
|
|
|
@stream.write_uint32( assemble_object(layout[:names]) ) #ref
|
|
|
|
variables.each do |var|
|
|
|
|
@stream.write_uint32 var
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2014-08-29 20:00:25 +02:00
|
|
|
## padding to the nearest 8
|
|
|
|
((padded(variables.length) - variables)/4).times do
|
|
|
|
@stream.write_uint32 0
|
|
|
|
end
|
|
|
|
slot.position
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_Array( array )
|
|
|
|
# also array has constant overhead, the padded helper fixes it to multiple of 8
|
2014-08-29 16:20:59 +02:00
|
|
|
array.each do |elem|
|
2014-08-29 20:00:25 +02:00
|
|
|
link_object(elem)
|
|
|
|
end
|
|
|
|
padded(array.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assemble_Array array
|
|
|
|
slot = get_slot(array)
|
|
|
|
raise "Array not linked #{object.inspect}" unless slot
|
|
|
|
layout = slot.layout
|
|
|
|
@stream.write_uint32( 0 ) #TODO types
|
|
|
|
@stream.write_uint32( assemble_object(layout[:names]) ) #ref
|
|
|
|
array.each do |var|
|
|
|
|
write_ref(var)
|
|
|
|
end
|
|
|
|
## padding to the nearest 8
|
|
|
|
((padded(array.length) - array.length)/4).times do
|
|
|
|
@stream.write_uint32 0
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
2014-08-29 20:00:25 +02:00
|
|
|
slot.position
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_Hash( hash )
|
|
|
|
link_object(hash.keys)
|
|
|
|
link_object(hash.values)
|
|
|
|
link_layout(hash)
|
|
|
|
padded(2)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 14:49:59 +02:00
|
|
|
def assemble_Hash hash
|
2014-08-29 20:00:25 +02:00
|
|
|
assemble_self( hash , [ hash.keys , hash.values] )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_BootSpace(space)
|
|
|
|
link_object( space.classes)
|
|
|
|
link_object(space.objects)
|
|
|
|
padded( 2 )
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 14:49:59 +02:00
|
|
|
def assemble_BootSpace(space)
|
2014-08-29 20:00:25 +02:00
|
|
|
assemble_self(space , [space.classes,space.objects] )
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_BootClass(clazz)
|
|
|
|
link_object(clazz.name )
|
|
|
|
link_object(clazz.super_class_name)
|
|
|
|
link_object(clazz.instance_methods)
|
|
|
|
padded(3)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
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-29 20:00:25 +02:00
|
|
|
def link_CompiledMethod(method)
|
|
|
|
length = 0
|
2014-08-29 16:20:59 +02:00
|
|
|
# NOT an ARRAY, just a bag of bytes
|
|
|
|
method.blocks.each do |block|
|
2014-08-29 20:00:25 +02:00
|
|
|
block.codes.each do |code|
|
|
|
|
length += code.length
|
|
|
|
end
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
2014-08-29 20:00:25 +02:00
|
|
|
padded(length)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-08-28 07:10:33 +02:00
|
|
|
def assemble_CompiledMethod(method)
|
2014-08-26 21:35:56 +02:00
|
|
|
assemble_object(method.name)
|
|
|
|
method.blocks.each do |block|
|
2014-08-29 20:00:25 +02:00
|
|
|
block.codes.each do |code|
|
|
|
|
code.assemble( @stream , self )
|
|
|
|
end
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_String( str)
|
|
|
|
return padded(str.length / 4)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_Symbol(sym)
|
|
|
|
return link_String(sym.to_s)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def link_StringConstant( sc)
|
|
|
|
return link_String(sc.string)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble_String( str )
|
|
|
|
@stream.write str
|
|
|
|
end
|
|
|
|
|
|
|
|
def assemble_Symbol(sym)
|
|
|
|
return assemble_String(sym.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assemble_StringConstant( sc)
|
|
|
|
return assemble_String(sc.string)
|
|
|
|
end
|
2014-08-29 20:00:25 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
def get_slot(object)
|
|
|
|
@objects[object.object_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_ref object
|
|
|
|
slot = get_slot(object)
|
|
|
|
raise "Object not linked #{object.inspect}" unless slot
|
|
|
|
@stream.write_uint32 slot.position
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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 padded len
|
|
|
|
8 * (1 + (len + 1) / 8)
|
|
|
|
end
|
|
|
|
|
|
|
|
def layout_for(object)
|
|
|
|
case object
|
|
|
|
when Array , Symbol , String , Virtual::CompiledMethod , Virtual::Block , Virtual::StringConstant
|
|
|
|
{ :names => [] , :types => []}
|
|
|
|
when Hash
|
|
|
|
{ :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
|
|
|
|
when Virtual::BootClass
|
|
|
|
{ :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
|
|
|
|
when Virtual::BootSpace
|
|
|
|
{ :names => [:classes,:objects] , :types => [Virtual::Reference,Virtual::Reference]}
|
|
|
|
else
|
|
|
|
raise "linker encounters unknown class #{object.class}"
|
|
|
|
end
|
|
|
|
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
|