rubyx/lib/register/assembler.rb

214 lines
7.3 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
2015-11-05 13:05:12 +01:00
log_level :info
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
@machine.init.set_position(at)
2015-10-25 11:03:31 +01:00
at += @machine.init.byte_length
at += 8 # thats the padding
# want to have the methods first in the executable
@machine.objects.each do |id , objekt|
next unless objekt.is_a? Parfait::Method
objekt.binary.position = at
objekt.instructions.set_position at
len = objekt.instructions.total_byte_length
log.debug "CODE #{objekt.name} at #{objekt.binary.position} len: #{len}"
objekt.binary.set_length(len , 0)
at += objekt.binary.padded_length
end
# and then everything else
@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
objekt.position = at
at += objekt.padded_length
end
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
all = @machine.objects.values.sort{|a,b| a.position <=> b.position}
# debugging loop accesses all positions to force an error if it's not set
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
# first we need to create the binary code for the methods
@machine.objects.each do |id , objekt|
next unless objekt.is_a? Parfait::Method
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
# then write the methods to file
@machine.objects.each do |id, objekt|
next unless objekt.is_a? Parfait::BinaryCode
write_any( objekt )
end
# and then the rest of the object machine
@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
log.debug "Assembled #{stream_position} bytes"
2014-08-30 13:01:22 +02:00
return @stream.string
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
index = 1
2015-06-03 09:01:59 +02:00
stream.rewind
log.debug "Assembled code #{method.name} with length #{stream.length}"
raise "length error #{method.binary.char_length} != #{method.instructions.total_byte_length}" if method.binary.char_length != method.instructions.total_byte_length
raise "length error #{stream.length} != #{method.instructions.total_byte_length}" if method.instructions.total_byte_length != stream.length
stream.each_byte do |b|
method.binary.set_char(index , b )
index = index + 1
end
end
def write_any obj
log.debug "Write #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
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
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
log.debug "Wrote #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
obj.position
2014-09-09 12:28:07 +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 write_object( 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
written = 0
object.get_instance_variables.each do |var|
inst = object.get_instance_variable(var)
2015-06-30 09:22:54 +02:00
#puts "Nil for #{object.class}.#{var}" unless inst
write_ref_for(inst)
written += 4
2014-08-26 21:35:56 +02:00
end
lay_len = written
log.debug "instances=#{object.get_instance_variables.inspect} mem_len=#{object.padded_length}"
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
log.debug "layout #{lay_len} , total #{written} (array #{written - lay_len})"
log.debug "Len = #{object.get_length} , inst = #{object.get_layout.instance_length}" if object.is_a? Parfait::Layout
pad_after( written )
object.position
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}"
write_ref_for( string.get_layout ) #ref
@stream.write_sint32( str.length ) #int
2014-08-26 21:35:56 +02:00
@stream.write str
pad_after(str.length + 8)
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)
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