2014-08-25 16:03:39 +02:00
|
|
|
module Register
|
2014-09-17 16:00:19 +02:00
|
|
|
class LinkException < Exception
|
|
|
|
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-16 17:50:44 +02:00
|
|
|
add_object(@space)
|
2014-10-07 11:01:33 +02:00
|
|
|
@space.init.set_position(0)
|
|
|
|
at = @space.init.mem_length # first jump instruction
|
2014-09-16 17:50:44 +02:00
|
|
|
# then all functions
|
|
|
|
@objects.each_value do | objekt|
|
2014-09-16 15:06:56 +02:00
|
|
|
next unless objekt.is_a? Virtual::CompiledMethod
|
2014-09-16 16:16:56 +02:00
|
|
|
objekt.set_position(at)
|
|
|
|
at += objekt.mem_length
|
2014-08-30 12:48:52 +02:00
|
|
|
end
|
2014-09-16 17:50:44 +02:00
|
|
|
#and then all data object
|
|
|
|
@objects.each_value do | objekt|
|
2014-09-16 15:06:56 +02:00
|
|
|
next if objekt.is_a? Virtual::CompiledMethod
|
2014-09-16 16:16:56 +02:00
|
|
|
objekt.set_position at
|
|
|
|
at += objekt.mem_length
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble
|
2014-09-17 16:00:19 +02:00
|
|
|
begin
|
|
|
|
link
|
|
|
|
@stream = StringIO.new
|
|
|
|
mid , main = @objects.find{|k,objekt| objekt.is_a?(Virtual::CompiledMethod) and (objekt.name == :__init__ )}
|
2014-10-07 11:01:33 +02:00
|
|
|
initial_jump = @space.init
|
|
|
|
initial_jump.codes.each do |code|
|
|
|
|
code.assemble( @stream )
|
|
|
|
end
|
2014-09-17 16:00:19 +02:00
|
|
|
@objects.each_value do |objekt|
|
|
|
|
next unless objekt.is_a? Virtual::CompiledMethod
|
|
|
|
assemble_object( objekt )
|
|
|
|
end
|
|
|
|
@objects.each_value do | objekt|
|
|
|
|
next if objekt.is_a? Virtual::CompiledMethod
|
|
|
|
assemble_object( objekt )
|
|
|
|
end
|
|
|
|
rescue LinkException => e
|
2014-09-18 16:05:22 +02:00
|
|
|
# knowing that we fix the problem, we hope to get away with retry.
|
2014-09-17 16:00:19 +02:00
|
|
|
retry
|
2014-08-30 12:48:52 +02:00
|
|
|
end
|
2014-09-18 16:05:22 +02:00
|
|
|
puts "Assembled 0x#{@stream.length.to_s(16)}/#{@stream.length} bytes"
|
2014-08-30 13:01:22 +02:00
|
|
|
return @stream.string
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-16 15:06:56 +02:00
|
|
|
def assemble_object obj
|
2014-09-18 16:05:22 +02:00
|
|
|
#puts "Assemble #{obj.class}(#{obj.object_id}) at stream #{(@stream.length).to_s(16)} pos:#{obj.position.to_s(16)} , len:#{obj.mem_length}"
|
2014-09-16 15:06:56 +02:00
|
|
|
raise "Assemble #{obj.class} at #{@stream.length.to_s(16)} not #{obj.position.to_s(16)}" if @stream.length != obj.position
|
2014-09-06 18:48:56 +02:00
|
|
|
clazz = obj.class.name.split("::").last
|
2014-09-16 15:06:56 +02:00
|
|
|
send("assemble_#{clazz}".to_sym , obj)
|
|
|
|
obj.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-09-16 15:06:56 +02:00
|
|
|
raise "Object(#{object.object_id}) not linked #{object.inspect}" unless @objects[object.object_id]
|
2014-09-09 12:28:07 +02:00
|
|
|
type = type_word(variables)
|
|
|
|
@stream.write_uint32( type )
|
2014-09-17 11:04:54 +02:00
|
|
|
write_ref_for(object.layout[:names] )
|
2014-08-29 20:00:25 +02:00
|
|
|
variables.each do |var|
|
2014-09-17 11:11:34 +02:00
|
|
|
raise object.class.name unless var
|
2014-09-17 11:04:54 +02:00
|
|
|
write_ref_for(var)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2014-09-07 16:31:40 +02:00
|
|
|
pad_after( variables.length * 4 )
|
2014-09-16 15:06:56 +02:00
|
|
|
object.position
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-16 15:06:56 +02:00
|
|
|
def assemble_Array array
|
2014-09-09 12:28:07 +02:00
|
|
|
type = type_word(array)
|
|
|
|
@stream.write_uint32( type )
|
2014-09-17 11:04:54 +02:00
|
|
|
write_ref_for(array.layout[:names]) #ref
|
2014-08-29 20:00:25 +02:00
|
|
|
array.each do |var|
|
2014-09-17 11:04:54 +02:00
|
|
|
write_ref_for(var)
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
2014-09-07 16:31:40 +02:00
|
|
|
pad_after( array.length * 4 )
|
2014-09-16 15:06:56 +02:00
|
|
|
array.position
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-16 15:06:56 +02:00
|
|
|
def assemble_Hash hash
|
2014-08-30 12:48:52 +02:00
|
|
|
# so here we can be sure to have _identical_ keys/values arrays
|
2014-09-17 11:11:34 +02:00
|
|
|
assemble_self( hash , [ hash.keys , hash.values ] )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-16 15:06:56 +02:00
|
|
|
def assemble_BootSpace(space)
|
2014-09-25 19:31:21 +02:00
|
|
|
assemble_self(space , [space.classes,space.objects, space.symbols,space.messages,space.next_message,space.next_frame] )
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2014-09-16 15:06:56 +02:00
|
|
|
def assemble_BootClass(clazz)
|
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-27 13:59:16 +02:00
|
|
|
def assemble_Message me
|
|
|
|
assemble_self(me , [])
|
|
|
|
end
|
|
|
|
def assemble_Frame me
|
|
|
|
assemble_self(me , [])
|
|
|
|
end
|
|
|
|
|
2014-09-16 15:06:56 +02:00
|
|
|
def assemble_CompiledMethod(method)
|
2014-09-17 11:04:54 +02:00
|
|
|
count = method.blocks.inject(0) { |c , block| c += block.mem_length }
|
2014-09-09 12:28:07 +02:00
|
|
|
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) }
|
|
|
|
@stream.write_uint32( word )
|
2014-09-17 11:04:54 +02:00
|
|
|
write_ref_for(method.layout[:names]) #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|
|
2014-09-16 16:16:56 +02:00
|
|
|
code.assemble( @stream )
|
2014-08-29 20:00:25 +02:00
|
|
|
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-16 15:06:56 +02:00
|
|
|
def assemble_String( str )
|
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-09-17 11:04:54 +02:00
|
|
|
word = (str.length + 7) / 32 # all object are multiple of 8 words (7 for header)
|
2014-09-09 12:28:07 +02:00
|
|
|
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) }
|
|
|
|
@stream.write_uint32( word )
|
2014-09-17 11:04:54 +02:00
|
|
|
write_ref_for( str.layout[:names] ) #ref
|
2014-08-26 21:35:56 +02:00
|
|
|
@stream.write str
|
2014-09-17 11:04:54 +02:00
|
|
|
pad_after(str.length)
|
2014-09-16 16:16:56 +02:00
|
|
|
#puts "String (#{slot.mem_length}) stream #{@stream.mem_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
|
|
|
|
2014-09-16 17:50:44 +02:00
|
|
|
def add_object(object)
|
|
|
|
return if @objects[object.object_id]
|
|
|
|
@objects[object.object_id] = object
|
|
|
|
add_object(object.layout[:names])
|
|
|
|
clazz = object.class.name.split("::").last
|
|
|
|
send("add_#{clazz}".to_sym , object)
|
2014-09-16 10:39:08 +02:00
|
|
|
end
|
2014-09-16 17:50:44 +02:00
|
|
|
|
2014-09-24 17:25:18 +02:00
|
|
|
def add_Message m
|
|
|
|
end
|
|
|
|
def add_Frame f
|
|
|
|
end
|
2014-09-16 17:50:44 +02:00
|
|
|
def add_Array( array )
|
|
|
|
# also array has constant overhead, the padded helper fixes it to multiple of 8
|
|
|
|
array.each do |elem|
|
|
|
|
add_object(elem)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_Hash( hash )
|
|
|
|
add_object(hash.keys)
|
|
|
|
add_object(hash.values)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_BootSpace(space)
|
2014-10-07 11:01:33 +02:00
|
|
|
add_object(space.main)
|
2014-09-16 17:50:44 +02:00
|
|
|
add_object(space.classes)
|
|
|
|
add_object(space.objects)
|
2014-09-19 18:11:30 +02:00
|
|
|
add_object(space.symbols)
|
|
|
|
add_object(space.messages)
|
2014-09-25 19:31:21 +02:00
|
|
|
add_object(space.next_message)
|
|
|
|
add_object(space.next_frame)
|
2014-09-16 17:50:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_BootClass(clazz)
|
|
|
|
add_object(clazz.name )
|
|
|
|
add_object(clazz.super_class_name)
|
|
|
|
add_object(clazz.instance_methods)
|
|
|
|
end
|
|
|
|
def add_CompiledMethod(method)
|
2014-09-17 11:04:54 +02:00
|
|
|
add_object(method.name)
|
2014-09-16 17:50:44 +02:00
|
|
|
end
|
|
|
|
def add_String( str)
|
|
|
|
end
|
|
|
|
def add_Symbol(sym)
|
|
|
|
end
|
|
|
|
def add_StringConstant(sc)
|
|
|
|
end
|
|
|
|
|
2014-08-29 20:00:25 +02:00
|
|
|
private
|
|
|
|
|
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)
|
2014-09-16 15:06:56 +02:00
|
|
|
# object means the object of which we write the address
|
2014-09-06 22:03:33 +02:00
|
|
|
# and we write the address into the self, given as second parameter
|
2014-09-17 11:04:54 +02:00
|
|
|
def write_ref_for object
|
2014-09-16 15:06:56 +02:00
|
|
|
@stream.write_sint32 object.position
|
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
|
2014-09-10 20:43:05 +02:00
|
|
|
pad = padded(length) - length - 8 # for header, type and layout
|
2014-09-05 19:56:05 +02:00
|
|
|
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-18 16:05:22 +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-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
|