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-30 12:48:52 +02:00
|
|
|
def initialize o
|
2014-08-29 20:00:25 +02:00
|
|
|
@position = -1
|
|
|
|
@length = -1
|
2014-09-06 18:48:56 +02:00
|
|
|
@objekt = o
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
2014-09-06 18:48:56 +02:00
|
|
|
attr_reader :objekt
|
2014-08-29 16:20:59 +02:00
|
|
|
attr_accessor :position , :length , :layout
|
2014-09-05 19:56:05 +02:00
|
|
|
def position
|
2014-09-06 18:48:56 +02:00
|
|
|
raise "position accessed but not set at #{length} for #{self.objekt}" if @position == -1
|
2014-09-05 19:56:05 +02:00
|
|
|
@position
|
|
|
|
end
|
2014-08-29 16:20:59 +02:00
|
|
|
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
|
2014-09-09 12:28:07 +02:00
|
|
|
TYPE_REF = 0
|
|
|
|
TYPE_INT = 1
|
|
|
|
TYPE_BITS = 4
|
|
|
|
TYPE_LENGTH = 6
|
|
|
|
|
2014-08-25 16:03:39 +02:00
|
|
|
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-30 19:55:22 +02:00
|
|
|
attr_reader :objects
|
2014-08-25 16:03:39 +02:00
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link
|
2014-09-05 19:56:05 +02:00
|
|
|
collect_object(@space)
|
2014-08-29 20:00:25 +02:00
|
|
|
at = 0
|
|
|
|
@objects.each do |id , slot|
|
2014-09-06 18:48:56 +02:00
|
|
|
next unless slot.objekt.is_a? Virtual::CompiledMethod
|
2014-08-30 12:48:52 +02:00
|
|
|
slot.position = at
|
2014-09-06 18:48:56 +02:00
|
|
|
slot.objekt.set_position at
|
2014-08-30 12:48:52 +02:00
|
|
|
at += slot.length
|
|
|
|
end
|
|
|
|
@objects.each do |id , slot|
|
2014-09-06 18:48:56 +02:00
|
|
|
next if slot.objekt.is_a? Virtual::CompiledMethod
|
2014-08-29 20:00:25 +02:00
|
|
|
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
|
2014-08-30 12:48:52 +02:00
|
|
|
@objects.each do |id , slot|
|
2014-09-06 18:48:56 +02:00
|
|
|
next unless slot.objekt.is_a? Virtual::CompiledMethod
|
|
|
|
assemble_object( slot )
|
2014-08-30 12:48:52 +02:00
|
|
|
end
|
|
|
|
@objects.each do |id , slot|
|
2014-09-06 18:48:56 +02:00
|
|
|
next if slot.objekt.is_a? Virtual::CompiledMethod
|
|
|
|
assemble_object( slot )
|
2014-08-30 12:48:52 +02:00
|
|
|
end
|
2014-09-05 19:56:05 +02:00
|
|
|
puts "Assembled #{@stream.length.to_s(16)}"
|
2014-08-30 13:01:22 +02:00
|
|
|
return @stream.string
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_object(object)
|
2014-08-29 16:20:59 +02:00
|
|
|
slot = @objects[object.object_id]
|
2014-08-30 12:48:52 +02:00
|
|
|
return slot.length if slot
|
|
|
|
slot = LinkSlot.new object
|
|
|
|
@objects[object.object_id] = slot
|
|
|
|
slot.layout = layout_for(object)
|
2014-09-05 19:56:05 +02:00
|
|
|
collect_object(slot.layout[:names])
|
2014-08-29 20:00:25 +02:00
|
|
|
clazz = object.class.name.split("::").last
|
2014-09-05 19:56:05 +02:00
|
|
|
slot.length = send("collect_#{clazz}".to_sym , object)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-06 18:48:56 +02:00
|
|
|
def assemble_object slot
|
|
|
|
obj = slot.objekt
|
2014-09-07 16:31:40 +02:00
|
|
|
puts "Assemble #{obj.class}(#{obj.object_id}) at stream #{(@stream.length).to_s(16)} pos:#{slot.position.to_s(16)} , len:#{slot.length}"
|
|
|
|
raise "Assemble #{obj.class} at #{@stream.length.to_s(16)} not #{slot.position.to_s(16)}" if @stream.length != slot.position
|
2014-09-06 18:48:56 +02:00
|
|
|
clazz = obj.class.name.split("::").last
|
2014-08-30 12:48:52 +02:00
|
|
|
send("assemble_#{clazz}".to_sym , slot)
|
|
|
|
slot.position
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-09 12:28:07 +02:00
|
|
|
def type_word array
|
|
|
|
word = 0
|
|
|
|
array.each_with_index do |var , index|
|
|
|
|
type = (var.class == Integer) ? TYPE_INT : TYPE_REF
|
|
|
|
word += type << (index * TYPE_BITS)
|
|
|
|
end
|
|
|
|
word += ( (array.length + 1 ) / 8 ) << TYPE_LENGTH * TYPE_BITS
|
|
|
|
word
|
|
|
|
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-09-06 18:48:56 +02:00
|
|
|
raise "Object(#{object.object_id}) not linked #{object.inspect}" unless slot
|
2014-09-09 12:28:07 +02:00
|
|
|
type = type_word(variables)
|
|
|
|
@stream.write_uint32( type )
|
2014-08-29 14:49:59 +02:00
|
|
|
layout = slot.layout
|
2014-09-06 22:03:33 +02:00
|
|
|
write_ref_for(layout[:names] , slot )
|
2014-08-29 20:00:25 +02:00
|
|
|
variables.each do |var|
|
2014-09-06 22:03:33 +02:00
|
|
|
write_ref_for(var , slot)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2014-09-07 16:31:40 +02:00
|
|
|
pad_after( variables.length * 4 )
|
2014-08-29 20:00:25 +02:00
|
|
|
slot.position
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_Array( array )
|
2014-08-29 20:00:25 +02:00
|
|
|
# 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-09-05 19:56:05 +02:00
|
|
|
collect_object(elem)
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
2014-09-07 16:31:40 +02:00
|
|
|
padded_words(array.length)
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
|
|
|
|
2014-08-30 12:48:52 +02:00
|
|
|
def assemble_Array slot
|
2014-09-06 18:48:56 +02:00
|
|
|
array = slot.objekt
|
2014-08-29 20:00:25 +02:00
|
|
|
layout = slot.layout
|
2014-09-09 12:28:07 +02:00
|
|
|
type = type_word(array)
|
|
|
|
@stream.write_uint32( type )
|
2014-09-06 22:03:33 +02:00
|
|
|
write_ref_for(layout[:names],slot) #ref
|
2014-08-29 20:00:25 +02:00
|
|
|
array.each do |var|
|
2014-09-06 22:03:33 +02:00
|
|
|
write_ref_for(var,slot)
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
2014-09-07 16:31:40 +02:00
|
|
|
pad_after( array.length * 4 )
|
2014-08-29 20:00:25 +02:00
|
|
|
slot.position
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_Hash( hash )
|
2014-08-30 12:48:52 +02:00
|
|
|
slot = get_slot(hash)
|
|
|
|
#hook the key/values arrays into the layout (just because it was around)
|
2014-09-05 19:56:05 +02:00
|
|
|
collect_object(slot.layout[:keys])
|
|
|
|
collect_object(slot.layout[:values])
|
2014-09-07 16:31:40 +02:00
|
|
|
padded_words(2)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-30 12:48:52 +02:00
|
|
|
def assemble_Hash slot
|
|
|
|
# so here we can be sure to have _identical_ keys/values arrays
|
2014-09-06 18:48:56 +02:00
|
|
|
assemble_self( slot.objekt , [ slot.layout[:keys] , slot.layout[:values] ] )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_BootSpace(space)
|
|
|
|
collect_object(space.classes)
|
|
|
|
collect_object(space.objects)
|
2014-09-07 16:31:40 +02:00
|
|
|
padded_words( 2 )
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-30 12:48:52 +02:00
|
|
|
def assemble_BootSpace(slot)
|
2014-09-06 18:48:56 +02:00
|
|
|
space = slot.objekt
|
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-09-05 19:56:05 +02:00
|
|
|
def collect_BootClass(clazz)
|
|
|
|
collect_object(clazz.name )
|
|
|
|
collect_object(clazz.super_class_name)
|
|
|
|
collect_object(clazz.instance_methods)
|
2014-09-07 16:31:40 +02:00
|
|
|
padded_words(3)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-30 12:48:52 +02:00
|
|
|
def assemble_BootClass(slot)
|
2014-09-06 18:48:56 +02:00
|
|
|
clazz = slot.objekt
|
2014-08-30 12:48:52 +02:00
|
|
|
assemble_self( clazz , [clazz.name , clazz.super_class_name, clazz.instance_methods] )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_CompiledMethod(method)
|
2014-08-29 16:20:59 +02:00
|
|
|
# NOT an ARRAY, just a bag of bytes
|
2014-08-30 12:48:52 +02:00
|
|
|
length = method.blocks.inject(0) { |c , block| c += block.length }
|
2014-09-07 16:31:40 +02:00
|
|
|
padded(length)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-08-30 12:48:52 +02:00
|
|
|
def assemble_CompiledMethod(slot)
|
2014-09-06 18:48:56 +02:00
|
|
|
method = slot.objekt
|
2014-09-09 12:28:07 +02:00
|
|
|
count = method.blocks.inject(0) { |c , block| c += block.length }
|
|
|
|
word = (count+7) / 32 # all object are multiple of 8 words (7 for header)
|
|
|
|
raise "Method too long, splitting not implemented #{method.name}/#{count}" if word > 15
|
|
|
|
# first line is integers, convention is that following lines are the same
|
|
|
|
TYPE_LENGTH.times { word = ((word << TYPE_BITS) + TYPE_INT) }
|
|
|
|
puts "THE WORD is #{word.to_s(16)}"
|
|
|
|
@stream.write_uint32( word )
|
2014-09-06 22:03:33 +02:00
|
|
|
write_ref_for(slot.layout[:names] , slot) #ref of layout
|
2014-08-30 12:48:52 +02:00
|
|
|
# TODO the assembly may have to move to the object to be more extensible
|
2014-08-26 21:35:56 +02:00
|
|
|
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
|
2014-09-07 16:31:40 +02:00
|
|
|
pad_after( count )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_String( str)
|
2014-09-07 16:31:40 +02:00
|
|
|
return padded( str.length + 1 )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_Symbol(sym)
|
|
|
|
return collect_String(sym.to_s)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-05 19:56:05 +02:00
|
|
|
def collect_StringConstant(sc)
|
|
|
|
return collect_String(sc.string)
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-08-30 13:01:22 +02:00
|
|
|
def assemble_String( slot )
|
2014-09-06 18:48:56 +02:00
|
|
|
str = slot.objekt
|
2014-09-05 19:56:05 +02:00
|
|
|
str = str.string if str.is_a? Virtual::StringConstant
|
|
|
|
str = str.to_s if str.is_a? Symbol
|
2014-08-30 12:48:52 +02:00
|
|
|
layout = slot.layout
|
2014-09-09 12:28:07 +02:00
|
|
|
word = (str.length + 7) / 32 # all object are multiple of 8 words (7 for header)
|
|
|
|
raise "String too long (implement split string!) #{word}" if word > 15
|
|
|
|
# first line is integers, convention is that following lines are the same
|
|
|
|
TYPE_LENGTH.times { word = ((word << TYPE_BITS) + TYPE_INT) }
|
|
|
|
puts "THE WORD is #{word.to_s(16)}"
|
|
|
|
@stream.write_uint32( word )
|
2014-09-06 22:03:33 +02:00
|
|
|
write_ref_for( slot.layout[:names] , slot) #ref
|
2014-08-26 21:35:56 +02:00
|
|
|
@stream.write str
|
2014-09-07 16:31:40 +02:00
|
|
|
pad_after(str.length)
|
2014-09-06 18:48:56 +02:00
|
|
|
#puts "String (#{slot.length}) stream #{@stream.length.to_s(16)}"
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def assemble_Symbol(sym)
|
2014-08-30 13:01:22 +02:00
|
|
|
return assemble_String(sym)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def assemble_StringConstant( sc)
|
2014-08-30 13:01:22 +02:00
|
|
|
return assemble_String(sc)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2014-08-29 20:00:25 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
def get_slot(object)
|
2014-08-30 12:48:52 +02:00
|
|
|
slot = @objects[object.object_id]
|
|
|
|
return slot if slot
|
|
|
|
if(object.is_a? Array)
|
|
|
|
@objects.each do |k,slot|
|
2014-09-06 18:48:56 +02:00
|
|
|
next unless slot.objekt.is_a? Array
|
|
|
|
if(slot.objekt.length == object.length)
|
2014-08-30 12:48:52 +02:00
|
|
|
same = true
|
2014-09-06 18:48:56 +02:00
|
|
|
slot.objekt.each_with_index do |v,index|
|
2014-08-30 12:48:52 +02:00
|
|
|
same = false unless v == object[index]
|
|
|
|
end
|
2014-09-06 18:48:56 +02:00
|
|
|
puts slot.objekt.first.class if same
|
2014-08-30 12:48:52 +02:00
|
|
|
return slot if same
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
|
|
|
|
2014-09-06 22:03:33 +02:00
|
|
|
# write means we write the resulting address straight into the assembler stream (ie don't return it)
|
|
|
|
# ref means the object of which we write the address
|
|
|
|
# and we write the address into the self, given as second parameter
|
|
|
|
# because of position independence, references/addresses are relative to self
|
|
|
|
def write_ref_for object , self_slot
|
2014-08-29 20:00:25 +02:00
|
|
|
slot = get_slot(object)
|
2014-09-06 18:48:56 +02:00
|
|
|
raise "Object (#{object.object_id}) not linked #{object.inspect}" unless slot
|
2014-09-06 22:03:33 +02:00
|
|
|
pos = slot.position - self_slot.position
|
2014-09-09 12:28:07 +02:00
|
|
|
#puts "Writin address #{(pos).to_s(16)} = #{slot.position.to_s(16)} - #{self_slot.position.to_s(16)}"
|
2014-09-07 16:31:40 +02:00
|
|
|
@stream.write_sint32 pos
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
|
|
|
|
2014-09-07 16:31:40 +02:00
|
|
|
# objects only come in lengths of multiple of 8 words
|
|
|
|
# but there is a constant overhead of 2 words, 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 7
|
2014-08-29 20:00:25 +02:00
|
|
|
def padded len
|
2014-09-07 16:31:40 +02:00
|
|
|
a = 32 * (1 + (len + 7)/32 )
|
2014-09-09 12:28:07 +02:00
|
|
|
#puts "#{a} for #{len}"
|
2014-09-07 16:31:40 +02:00
|
|
|
a
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
|
|
|
|
2014-09-07 16:31:40 +02:00
|
|
|
def padded_words words
|
|
|
|
padded(words*4) # 4 == word length, a constant waiting for a home
|
|
|
|
end
|
|
|
|
|
|
|
|
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
|
|
|
|
def pad_after length
|
|
|
|
length += 8 # for header, type and layout
|
2014-09-05 19:56:05 +02:00
|
|
|
pad = padded(length) - length
|
|
|
|
pad.times do
|
2014-09-07 16:31:40 +02:00
|
|
|
@stream.write_uint8(0)
|
2014-09-05 19:56:05 +02:00
|
|
|
end
|
2014-09-09 12:28:07 +02:00
|
|
|
#puts "padded #{length} with #{pad} stream pos #{@stream.length.to_s(16)}"
|
2014-09-05 19:56:05 +02:00
|
|
|
end
|
2014-09-07 16:31:40 +02:00
|
|
|
|
2014-08-30 12:48:52 +02:00
|
|
|
# class variables to have _identical_ objects passed back (stops recursion)
|
|
|
|
@@ARRAY = { :names => [] , :types => []}
|
|
|
|
@@HASH = { :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
|
|
|
|
@@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
|
|
|
|
@@SPACE = { :names => [:classes,:objects] , :types => [Virtual::Reference,Virtual::Reference]}
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
def layout_for(object)
|
|
|
|
case object
|
|
|
|
when Array , Symbol , String , Virtual::CompiledMethod , Virtual::Block , Virtual::StringConstant
|
2014-08-30 12:48:52 +02:00
|
|
|
@@ARRAY
|
2014-08-29 20:00:25 +02:00
|
|
|
when Hash
|
2014-08-30 12:48:52 +02:00
|
|
|
@@HASH.merge :keys => object.keys , :values => object.values
|
2014-08-29 20:00:25 +02:00
|
|
|
when Virtual::BootClass
|
2014-08-30 12:48:52 +02:00
|
|
|
@@CLAZZ
|
2014-08-29 20:00:25 +02:00
|
|
|
when Virtual::BootSpace
|
2014-08-30 12:48:52 +02:00
|
|
|
@@SPACE
|
2014-08-29 20:00:25 +02:00
|
|
|
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
|