rubyx/lib/register/assembler.rb

279 lines
8.7 KiB
Ruby
Raw Normal View History

module Register
class LinkException < Exception
end
# Assemble the object machine into a binary.
2015-06-09 11:38:03 +02:00
# Assemble first to get positions, then write
2015-05-30 11:20:39 +02:00
2015-06-09 11:38:03 +02:00
# The assemble function determines the length of an object and then actually
2015-05-30 11:20:39 +02:00
# 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.
class Assembler
include Padding
include Logging
log_level :debug
MARKER = 0xA51AF00D
def initialize machine
@machine = machine
@load_at = 0x8054 # this is linux/arm
end
def assemble
at = 0
2015-06-10 10:43:50 +02:00
#need the initial jump at 0 and then functions
2016-12-11 13:48:12 +01:00
@machine.init.set_position(0)
at = assemble_objects
# and then everything code
2016-12-11 13:48:12 +01:00
asseble_code_from( at )
end
def asseble_code_from( at )
@machine.objects.each do |id , objekt|
2016-12-12 22:38:55 +01:00
next unless objekt.is_a? Parfait::TypedMethod
2016-12-30 18:17:15 +01:00
log.debug "CODE1 #{objekt.name}"
2016-12-16 14:41:26 +01:00
binary = objekt.binary
binary.set_position( at )
objekt.instructions.set_position( at + 12 )# BinaryCode header
len = objekt.instructions.total_byte_length
2016-12-30 18:17:15 +01:00
log.debug "CODE2 #{objekt.name} at #{binary.position} len: #{len}"
2016-12-16 14:41:26 +01:00
binary.set_length(len , 0)
at += binary.padded_length
end
2016-12-11 13:48:12 +01:00
at
end
2016-12-11 15:12:39 +01:00
2016-12-11 13:48:12 +01:00
def assemble_objects
at = @machine.init.byte_length
at += 8 # thats the padding
# want to have the objects first in the executable
@machine.objects.each do | id , objekt|
next if objekt.is_a? Register::Label # will get assembled as method.instructions
next if objekt.is_a? Parfait::BinaryCode
2016-12-28 20:40:06 +01:00
objekt.set_position at
2016-12-11 13:48:12 +01:00
at += objekt.padded_length
end
at
end
def write_as_string
# must be same order as assemble
begin
return try_write
rescue LinkException
2014-09-18 16:05:22 +02:00
# knowing that we fix the problem, we hope to get away with retry.
retry
end
end
# private method to implement write_as_string. May throw link Exception in which
# case we try again. Once.
def try_write
assemble
2016-12-11 15:12:39 +01:00
try_write_debug
try_write_create_binary
try_write_objects
try_write_method
log.debug "Assembled #{stream_position} bytes"
return @stream.string
end
# debugging loop accesses all positions to force an error if it's not set
def try_write_debug
all = @machine.objects.values.sort{|a,b| a.position <=> b.position}
all.each do |objekt|
next if objekt.is_a?(Register::Label)
log.debug "Linked #{objekt.class}(#{objekt.object_id}) at #{objekt.position} / #{objekt.padded_length}"
objekt.position
end
2016-12-11 13:48:12 +01:00
end
2016-12-11 13:48:12 +01:00
def try_write_create_binary
# first we need to create the binary code for the methods
@machine.objects.each do |id , objekt|
2016-12-12 22:38:55 +01:00
next unless objekt.is_a? Parfait::TypedMethod
assemble_binary_method(objekt)
end
@stream = StringIO.new
2015-10-25 11:03:31 +01:00
@machine.init.assemble( @stream )
8.times do
@stream.write_uint8(0)
end
2016-12-11 13:48:12 +01:00
end
def try_write_objects
# then the objects , not code yet
@machine.objects.each do | id, objekt|
next if objekt.is_a? Parfait::BinaryCode
next if objekt.is_a? Register::Label # ignore
write_any( objekt )
end
2016-12-11 13:48:12 +01:00
end
2016-12-11 13:48:12 +01:00
def try_write_method
# then write the methods to file
@machine.objects.each do |id, objekt|
next unless objekt.is_a? Parfait::BinaryCode
write_any( objekt )
end
2014-08-26 21:35:56 +02:00
end
2015-07-03 19:13:03 +02:00
# assemble the MethodSource into a stringio
# and then plonk that binary data into the method.code array
def assemble_binary_method method
stream = StringIO.new
2015-10-25 11:03:31 +01:00
#puts "Method #{method.source.instructions.to_ac}"
begin
2015-10-25 11:03:31 +01:00
#puts "assemble #{method.source.instructions}"
method.instructions.assemble_all( stream )
rescue => e
log.debug "Assembly error #{method.name}\n#{Sof.write(method.instructions).to_s[0...2000]}"
raise e
end
2016-12-11 13:48:12 +01:00
write_binary_method_to_stream( method, stream)
end
def write_binary_method_to_stream(method, stream)
2016-12-11 15:48:01 +01:00
write_binary_method_checks(method,stream)
index = 1
stream.each_byte do |b|
method.binary.set_char(index , b )
index = index + 1
end
end
2016-12-11 15:48:01 +01:00
def write_binary_method_checks(method,stream)
stream.rewind
2016-12-16 14:41:26 +01:00
length = stream.length
binary = method.binary
total_byte_length = method.instructions.total_byte_length
log.debug "Assembled code #{method.name} with length #{length}"
raise "length error #{binary.char_length} != #{total_byte_length}" if binary.char_length != total_byte_length
raise "length error #{length} != #{total_byte_length}" if total_byte_length != length
2016-12-11 15:48:01 +01:00
end
def write_any obj
2016-12-11 15:48:01 +01:00
write_any_log( obj , "Write")
2015-07-02 12:49:33 +02:00
if @stream.length != obj.position
raise "Write #{obj.class} #{obj.object_id} at #{stream_position} not #{obj.position}"
end
2016-12-11 15:12:39 +01:00
write_any_out(obj)
2016-12-11 15:48:01 +01:00
write_any_log( obj , "Wrote")
2016-12-11 15:12:39 +01:00
obj.position
end
2016-12-11 15:48:01 +01:00
def write_any_log( obj , at)
log.debug "#{at} #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
end
2016-12-11 15:12:39 +01:00
def write_any_out(obj)
2015-06-08 12:24:28 +02:00
if obj.is_a?(Parfait::Word) or obj.is_a?(Symbol)
write_String obj
2015-06-08 12:24:28 +02:00
else
write_object obj
2015-06-08 12:24:28 +02:00
end
2014-09-09 12:28:07 +02:00
end
2016-02-25 21:03:11 +01:00
# write type of the instance, and the variables that are passed
2014-08-29 20:00:25 +02:00
# variables ar values, ie int or refs. For refs the object needs to save the object first
def write_object( object )
2016-12-11 15:48:01 +01:00
write_object_check(object)
obj_written = write_object_variables(object)
log.debug "instances=#{object.get_instance_variables.inspect} mem_len=#{object.padded_length}"
indexed_written = write_object_indexed(object)
log.debug "type #{obj_written} , total #{obj_written + indexed_written} (array #{indexed_written})"
log.debug "Len = #{object.get_length} , inst = #{object.get_type.instance_length}" if object.is_a? Parfait::Type
pad_after( obj_written + indexed_written )
object.position
end
def write_object_check(object)
log.debug "Write object #{object.class} #{object.inspect}"
unless @machine.objects.has_key? object.object_id
2015-05-30 11:20:39 +02:00
raise "Object(#{object.object_id}) not linked #{object.inspect}"
end
2016-12-11 13:48:12 +01:00
end
def write_object_indexed(object)
written = 0
if( object.is_a? Parfait::Indexed)
2015-06-08 12:19:53 +02:00
object.each do |inst|
write_ref_for(inst)
written += 4
2015-06-08 12:19:53 +02:00
end
end
2016-12-11 13:48:12 +01:00
written
end
def write_object_variables(object)
@stream.write_sint32( MARKER )
written = 0 # compensate for the "secrect" marker
object.get_instance_variables.each do |var|
inst = object.get_instance_variable(var)
#puts "Nil for #{object.class}.#{var}" unless inst
write_ref_for(inst)
written += 4
end
written
2014-08-29 14:49:59 +02:00
end
def write_BinaryCode code
write_String code
end
def write_String( string )
if string.is_a? Parfait::Word
str = string.to_string
raise "length mismatch #{str.length} != #{string.char_length}" if str.length != string.char_length
end
2015-06-01 07:33:23 +02:00
str = string.to_s if string.is_a? Symbol
log.debug "#{string.class} is #{string} at #{string.position} length #{string.length}"
2016-12-11 15:12:39 +01:00
write_checked_string(string , str)
end
def write_checked_string(string, str)
@stream.write_sint32( MARKER )
write_ref_for( string.get_type ) #ref
@stream.write_sint32( str.length ) #int
2014-08-26 21:35:56 +02:00
@stream.write str
pad_after(str.length + 8 ) # type , length *4 == 12
log.debug "String (#{string.length}) stream #{@stream.length}"
2014-08-26 21:35:56 +02:00
end
def write_Symbol(sym)
return write_String(sym)
2014-08-26 21:35:56 +02:00
end
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
# object means the object of which we write the address
def write_ref_for object
2015-10-16 16:13:08 +02:00
case object
when nil
@stream.write_sint32(0)
2015-10-16 16:13:08 +02:00
when Fixnum
@stream.write_sint32(object)
2015-06-08 12:19:53 +02:00
else
@stream.write_sint32(object.position + @load_at)
2015-06-08 12:19:53 +02:00
end
2014-08-29 20:00:25 +02:00
end
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
def pad_after length
2015-07-02 12:49:33 +02:00
before = stream_position
pad = padding_for(length) - 4 # four is for the MARKER we write
2014-09-05 19:56:05 +02:00
pad.times do
@stream.write_uint8(0)
2014-09-05 19:56:05 +02:00
end
2015-07-02 12:49:33 +02:00
after = stream_position
log.debug "padded #{length} with #{pad} stream #{before}/#{after}"
2014-09-05 19:56:05 +02:00
end
2015-07-02 12:49:33 +02:00
# return the stream length as hex
def stream_position
@stream.length
end
end
2015-10-22 17:16:29 +02:00
Sof::Volotile.add(Assembler , [:objects])
end