2014-08-25 16:03:39 +02:00
|
|
|
module Register
|
2014-09-17 16:00:19 +02:00
|
|
|
class LinkException < Exception
|
|
|
|
end
|
2015-05-31 12:02:29 +02:00
|
|
|
# Assemble the object machine into a binary.
|
2014-08-29 20:00:25 +02:00
|
|
|
# Link first to get positions, then assemble
|
2015-05-30 11:20:39 +02:00
|
|
|
|
|
|
|
# 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
|
2015-06-04 07:22:38 +02:00
|
|
|
include Padding
|
2014-09-09 12:28:07 +02:00
|
|
|
TYPE_REF = 0
|
|
|
|
TYPE_INT = 1
|
|
|
|
TYPE_BITS = 4
|
|
|
|
TYPE_LENGTH = 6
|
2015-05-12 14:36:44 +02:00
|
|
|
|
2015-05-31 12:02:29 +02:00
|
|
|
def initialize machine
|
|
|
|
@machine = machine
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2014-08-26 15:36:12 +02:00
|
|
|
def link
|
2015-05-26 19:17:43 +02:00
|
|
|
# want to have the methods first in the executable
|
|
|
|
# so first we determine the code length for the methods and set the
|
2015-05-28 20:10:27 +02:00
|
|
|
# binary code (array) to right length
|
2015-05-31 12:02:29 +02:00
|
|
|
@machine.objects.each do |objekt|
|
2015-05-26 19:17:43 +02:00
|
|
|
next unless objekt.is_a? Parfait::Method
|
2015-06-05 08:20:43 +02:00
|
|
|
objekt.code.set_length(objekt.info.byte_length , 0)
|
2015-05-26 19:17:43 +02:00
|
|
|
end
|
|
|
|
at = 0
|
|
|
|
# then we make sure we really get the binary codes first
|
2015-05-31 12:02:29 +02:00
|
|
|
@machine.objects.each do |objekt|
|
2015-05-26 19:17:43 +02:00
|
|
|
next unless objekt.is_a? Parfait::BinaryCode
|
|
|
|
objekt.set_position at
|
2015-05-31 13:45:28 +02:00
|
|
|
# puts "CODE #{objekt.name} at #{objekt.position}"
|
2015-06-05 08:20:43 +02:00
|
|
|
at += objekt.word_length
|
2015-05-26 19:17:43 +02:00
|
|
|
end
|
|
|
|
# and then everything else
|
2015-05-31 12:02:29 +02:00
|
|
|
@machine.objects.each do | objekt|
|
2015-05-28 20:10:27 +02:00
|
|
|
# have to tell the code that will be assembled where it is to
|
|
|
|
# get the jumps/calls right
|
|
|
|
if objekt.is_a? Parfait::Method
|
|
|
|
objekt.info.set_position( objekt.code.position )
|
|
|
|
end
|
2015-05-26 19:17:43 +02:00
|
|
|
next if objekt.is_a? Parfait::BinaryCode
|
2014-09-16 16:16:56 +02:00
|
|
|
objekt.set_position at
|
2015-06-05 08:20:43 +02:00
|
|
|
at += objekt.word_length
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 21:35:56 +02:00
|
|
|
def assemble
|
2015-05-31 13:45:28 +02:00
|
|
|
# must be same order as link
|
2015-06-04 07:22:38 +02:00
|
|
|
|
2014-09-17 16:00:19 +02:00
|
|
|
begin
|
|
|
|
link
|
2015-06-04 07:22:38 +02:00
|
|
|
all= @machine.objects.sort{|a,b| a.position <=> b.position}
|
2015-06-08 11:52:56 +02:00
|
|
|
# debugging loop accesses all positions to force an error if it's not set
|
2015-06-04 07:22:38 +02:00
|
|
|
all.each do |objekt|
|
2015-06-05 08:20:43 +02:00
|
|
|
puts "Linked #{objekt.class}(#{objekt.object_id.to_s(16)}) at #{objekt.position.to_s(16)} / #{objekt.word_length.to_s(16)}"
|
2015-06-01 07:33:23 +02:00
|
|
|
objekt.position
|
|
|
|
end
|
2015-05-26 19:17:43 +02:00
|
|
|
# first we need to create the binary code for the methods
|
2015-05-31 12:02:29 +02:00
|
|
|
@machine.objects.each do |objekt|
|
2015-05-26 19:17:43 +02:00
|
|
|
next unless objekt.is_a? Parfait::Method
|
|
|
|
assemble_binary_method(objekt)
|
|
|
|
end
|
2014-09-17 16:00:19 +02:00
|
|
|
@stream = StringIO.new
|
2015-05-12 14:36:44 +02:00
|
|
|
#TODOmid , main = @objects.find{|k,objekt| objekt.is_a?(Virtual::CompiledMethod) and (objekt.name == :__init__ )}
|
2015-05-31 12:02:29 +02:00
|
|
|
# initial_jump = @machine.init
|
2015-05-26 19:17:43 +02:00
|
|
|
# initial_jump.codes.each do |code|
|
|
|
|
# code.assemble( @stream )
|
|
|
|
# end
|
|
|
|
# then write the methods to file
|
2015-05-31 12:02:29 +02:00
|
|
|
@machine.objects.each do |objekt|
|
2015-05-26 19:17:43 +02:00
|
|
|
next unless objekt.is_a? Parfait::BinaryCode
|
|
|
|
assemble_any( objekt )
|
2014-09-17 16:00:19 +02:00
|
|
|
end
|
2015-05-31 12:02:29 +02:00
|
|
|
# and then the rest of the object machine
|
|
|
|
@machine.objects.each do | objekt|
|
2015-05-26 19:17:43 +02:00
|
|
|
next if objekt.is_a? Parfait::BinaryCode
|
|
|
|
assemble_any( objekt )
|
2014-09-17 16:00:19 +02:00
|
|
|
end
|
2015-05-12 14:36:44 +02:00
|
|
|
rescue LinkException
|
2015-06-04 07:22:38 +02:00
|
|
|
puts "RELINK"
|
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
|
2015-06-06 18:46:53 +02:00
|
|
|
puts "Assembled 0x#{@stream.length.to_s(16)}/#{@stream.length.to_s(16)} bytes"
|
2014-08-30 13:01:22 +02:00
|
|
|
return @stream.string
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2015-05-26 19:17:43 +02:00
|
|
|
# assemble the CompiledMethodInfo into a stringio
|
|
|
|
# and then plonk that binary data into the method.code array
|
|
|
|
def assemble_binary_method method
|
|
|
|
stream = StringIO.new
|
|
|
|
method.info.blocks.each do |block|
|
|
|
|
block.codes.each do |code|
|
2015-06-01 07:33:23 +02:00
|
|
|
begin
|
2015-05-26 19:17:43 +02:00
|
|
|
code.assemble( stream )
|
2015-06-01 07:33:23 +02:00
|
|
|
rescue => e
|
|
|
|
puts "Method error #{method.name}\n#{Sof::Writer.write(method.info.blocks).to_s[0...2000]}"
|
|
|
|
puts Sof::Writer.write(code)
|
|
|
|
raise e
|
|
|
|
end
|
2015-05-26 19:17:43 +02:00
|
|
|
end
|
|
|
|
end
|
2015-05-28 20:10:27 +02:00
|
|
|
method.code.fill_with 0
|
2015-05-26 19:17:43 +02:00
|
|
|
index = 1
|
2015-06-03 09:01:59 +02:00
|
|
|
stream.rewind
|
|
|
|
puts "Assembled #{method.name} with length #{stream.length}"
|
2015-06-05 08:20:43 +02:00
|
|
|
raise "length error #{method.code.length} != #{method.info.byte_length}" if method.code.length != method.info.byte_length
|
|
|
|
raise "length error #{stream.length} != #{method.info.byte_length}" if method.info.byte_length - stream.length > 32
|
2015-05-26 19:17:43 +02:00
|
|
|
stream.each_byte do |b|
|
2015-06-03 09:01:59 +02:00
|
|
|
method.code.set_char(index , b )
|
2015-05-26 19:17:43 +02:00
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
end
|
2015-05-31 13:45:28 +02:00
|
|
|
|
2015-05-26 19:17:43 +02:00
|
|
|
def assemble_any obj
|
2015-06-06 18:46:53 +02:00
|
|
|
puts "Assemble #{obj.class}(#{obj.object_id.to_s(16)}) at stream #{@stream.length.to_s(16)} pos:#{obj.position.to_s(16)} , len:#{obj.word_length.to_s(16)}"
|
2015-05-26 19:17:43 +02:00
|
|
|
if @stream.length != obj.position
|
2015-06-04 07:22:38 +02:00
|
|
|
raise "Assemble #{obj.class} #{obj.object_id.to_s(16)} at #{@stream.length.to_s(16)} not #{obj.position.to_s(16)}"
|
2015-05-26 19:17:43 +02:00
|
|
|
end
|
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
|
2015-05-31 13:45:28 +02:00
|
|
|
index = 0
|
|
|
|
array.each do |var |
|
|
|
|
#type = (var.class == Integer) ? TYPE_INT : TYPE_REF
|
|
|
|
#TODO
|
|
|
|
type = TYPE_REF
|
|
|
|
word += type << (index * TYPE_BITS)
|
|
|
|
index = index + 1
|
2014-09-09 12:28:07 +02:00
|
|
|
end
|
2015-05-31 13:45:28 +02:00
|
|
|
word += ( (array.get_length + 1 ) / 8 ) << TYPE_LENGTH * TYPE_BITS
|
2014-09-09 12:28:07 +02:00
|
|
|
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
|
2015-05-31 13:45:28 +02:00
|
|
|
def assemble_object( object )
|
|
|
|
unless @machine.objects.include? object
|
2015-05-30 11:20:39 +02:00
|
|
|
raise "Object(#{object.object_id}) not linked #{object.inspect}"
|
|
|
|
end
|
2015-05-31 13:45:28 +02:00
|
|
|
layout = object.get_layout
|
|
|
|
type = type_word(layout)
|
2014-09-09 12:28:07 +02:00
|
|
|
@stream.write_uint32( type )
|
2015-05-31 13:45:28 +02:00
|
|
|
write_ref_for(layout )
|
|
|
|
layout.each do |var|
|
|
|
|
inst = object.instance_variable_get "@#{var}".to_sym
|
|
|
|
puts "Nil for #{object.class}.#{var}" unless inst
|
|
|
|
write_ref_for(inst)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
2015-06-06 18:46:53 +02:00
|
|
|
puts "layout length=#{layout.get_length.to_s(16)} mem_len=#{layout.word_length.to_s(16)}"
|
|
|
|
l = layout.get_length
|
2015-06-08 12:19:53 +02:00
|
|
|
if( object.is_a? Parfait::List)
|
|
|
|
object.each do |inst|
|
|
|
|
write_ref_for(inst)
|
|
|
|
end
|
|
|
|
l += object.get_length
|
|
|
|
end
|
2015-06-06 18:46:53 +02:00
|
|
|
pad_after( l * 4)
|
2014-09-16 15:06:56 +02:00
|
|
|
object.position
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2015-05-26 19:17:43 +02:00
|
|
|
def assemble_List array
|
2015-06-08 12:19:53 +02:00
|
|
|
assemble_object array
|
|
|
|
return
|
|
|
|
|
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
|
|
|
|
|
2015-06-07 10:06:08 +02:00
|
|
|
def assemble_Layout layout
|
|
|
|
assemble_object(layout)
|
|
|
|
end
|
2015-05-26 19:17:43 +02:00
|
|
|
def assemble_Dictionary hash
|
2014-08-30 12:48:52 +02:00
|
|
|
# so here we can be sure to have _identical_ keys/values arrays
|
2015-06-08 12:19:53 +02:00
|
|
|
assemble_object( hash )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2015-05-31 13:45:28 +02:00
|
|
|
def assemble_Space(space)
|
|
|
|
assemble_object(space )
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2015-05-12 14:36:44 +02:00
|
|
|
def assemble_Class(clazz)
|
2015-05-31 13:45:28 +02:00
|
|
|
assemble_object( clazz )
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2014-09-27 13:59:16 +02:00
|
|
|
def assemble_Message me
|
2015-05-31 13:45:28 +02:00
|
|
|
assemble_object(me)
|
2014-09-27 13:59:16 +02:00
|
|
|
end
|
|
|
|
def assemble_Frame me
|
2015-05-31 13:45:28 +02:00
|
|
|
assemble_object(me )
|
2014-09-27 13:59:16 +02:00
|
|
|
end
|
|
|
|
|
2015-05-26 19:17:43 +02:00
|
|
|
def assemble_Method(method)
|
2015-06-08 12:19:53 +02:00
|
|
|
assemble_object(method)
|
|
|
|
return
|
2015-06-05 08:20:43 +02:00
|
|
|
count = method.info.blocks.inject(0) { |c , block| c += block.word_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 )
|
2015-05-26 19:17:43 +02:00
|
|
|
write_ref_for(method.get_layout()) #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
|
|
|
|
|
2015-05-30 10:55:46 +02:00
|
|
|
def assemble_BinaryCode code
|
|
|
|
assemble_String code
|
|
|
|
end
|
|
|
|
|
|
|
|
def assemble_String( string )
|
2015-06-03 09:01:59 +02:00
|
|
|
str = string.to_string if string.is_a? Parfait::Word
|
2015-06-01 07:33:23 +02:00
|
|
|
str = string.to_s if string.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 )
|
2015-06-06 18:46:53 +02:00
|
|
|
puts "String is #{string} at #{string.position.to_s(16)} length #{string.length.to_s(16)}"
|
2015-05-30 10:55:46 +02:00
|
|
|
write_ref_for( string.get_layout ) #ref
|
2014-08-26 21:35:56 +02:00
|
|
|
@stream.write str
|
2014-09-17 11:04:54 +02:00
|
|
|
pad_after(str.length)
|
2015-06-05 08:20:43 +02:00
|
|
|
#puts "String (#{slot.word_length}) stream #{@stream.word_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
|
|
|
|
2015-05-12 14:36:44 +02:00
|
|
|
private
|
2014-08-29 20:00:25 +02:00
|
|
|
|
2015-05-30 11:20:39 +02:00
|
|
|
# write means we write the resulting address straight into the assembler stream
|
2014-09-16 15:06:56 +02:00
|
|
|
# object means the object of which we write the address
|
2014-09-17 11:04:54 +02:00
|
|
|
def write_ref_for object
|
2015-06-08 12:19:53 +02:00
|
|
|
if object.nil?
|
|
|
|
pos = 0
|
|
|
|
else
|
|
|
|
pos = object.position
|
|
|
|
end
|
|
|
|
@stream.write_sint32 pos
|
2014-08-29 20:00:25 +02:00
|
|
|
end
|
|
|
|
|
2014-09-07 16:31:40 +02:00
|
|
|
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
|
|
|
|
def pad_after length
|
2015-06-06 18:46:53 +02:00
|
|
|
before = @stream.length.to_s(16)
|
2015-06-05 08:20:43 +02:00
|
|
|
pad = padding_for(length)
|
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
|
2015-06-06 18:46:53 +02:00
|
|
|
after = @stream.length.to_s(16)
|
|
|
|
puts "padded #{length.to_s(16)} with #{pad.to_s(16)} stream #{before}/#{after}"
|
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
|