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.
|
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.
|
|
|
|
|
2014-08-25 16:03:39 +02:00
|
|
|
class Assembler
|
2015-06-04 07:22:38 +02:00
|
|
|
include Padding
|
2015-11-04 10:48:51 +01:00
|
|
|
include Logging
|
2015-11-05 13:05:12 +01:00
|
|
|
log_level :info
|
2015-05-12 14:36:44 +02:00
|
|
|
|
2015-11-15 19:42:07 +01:00
|
|
|
MARKER = 0xA51AF00D
|
|
|
|
|
2015-05-31 12:02:29 +02:00
|
|
|
def initialize machine
|
|
|
|
@machine = machine
|
2015-06-27 14:17:15 +02:00
|
|
|
@load_at = 0x8054 # this is linux/arm
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
def assemble
|
2015-06-27 14:17:15 +02:00
|
|
|
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
|
2015-11-15 15:24:43 +01:00
|
|
|
# and then everything code
|
2016-12-11 13:48:12 +01:00
|
|
|
asseble_code_from( at )
|
|
|
|
end
|
|
|
|
|
|
|
|
def asseble_code_from( at )
|
2015-07-21 18:41:30 +02:00
|
|
|
@machine.objects.each do |id , objekt|
|
2016-12-12 22:38:55 +01:00
|
|
|
next unless objekt.is_a? Parfait::TypedMethod
|
2016-12-16 14:41:26 +01:00
|
|
|
binary = objekt.binary
|
2016-12-28 20:40:06 +01:00
|
|
|
binary.set_position at
|
2015-11-15 19:42:07 +01:00
|
|
|
objekt.instructions.set_position at + 12 # BinaryCode header
|
2015-11-03 15:24:12 +01:00
|
|
|
len = objekt.instructions.total_byte_length
|
2016-12-16 14:41:26 +01:00
|
|
|
log.debug "CODE #{objekt.name} at #{binary.position} len: #{len}"
|
|
|
|
binary.set_length(len , 0)
|
|
|
|
at += binary.padded_length
|
2015-05-26 19:17:43 +02:00
|
|
|
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
|
2014-08-29 16:20:59 +02:00
|
|
|
end
|
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_as_string
|
|
|
|
# must be same order as assemble
|
2014-09-17 16:00:19 +02:00
|
|
|
begin
|
2015-06-30 17:35:37 +02:00
|
|
|
return try_write
|
2015-05-12 14:36:44 +02:00
|
|
|
rescue LinkException
|
2014-09-18 16:05:22 +02:00
|
|
|
# knowing that we fix the problem, we hope to get away with retry.
|
2015-07-01 18:36:18 +02:00
|
|
|
retry
|
2015-06-30 17:35:37 +02:00
|
|
|
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
|
2015-07-21 18:41:30 +02:00
|
|
|
all = @machine.objects.values.sort{|a,b| a.position <=> b.position}
|
2015-06-30 17:35:37 +02:00
|
|
|
all.each do |objekt|
|
2015-11-04 09:34:58 +01:00
|
|
|
next if objekt.is_a?(Register::Label)
|
2015-11-04 10:48:51 +01:00
|
|
|
log.debug "Linked #{objekt.class}(#{objekt.object_id}) at #{objekt.position} / #{objekt.padded_length}"
|
2015-06-30 17:35:37 +02:00
|
|
|
objekt.position
|
|
|
|
end
|
2016-12-11 13:48:12 +01:00
|
|
|
end
|
2015-11-15 15:24:43 +01:00
|
|
|
|
2016-12-11 13:48:12 +01:00
|
|
|
def try_write_create_binary
|
2015-06-30 17:35:37 +02:00
|
|
|
# first we need to create the binary code for the methods
|
2015-07-21 18:41:30 +02:00
|
|
|
@machine.objects.each do |id , objekt|
|
2016-12-12 22:38:55 +01:00
|
|
|
next unless objekt.is_a? Parfait::TypedMethod
|
2015-06-30 17:35:37 +02:00
|
|
|
assemble_binary_method(objekt)
|
|
|
|
end
|
|
|
|
@stream = StringIO.new
|
2015-10-25 11:03:31 +01:00
|
|
|
@machine.init.assemble( @stream )
|
2015-06-30 17:35:37 +02:00
|
|
|
8.times do
|
|
|
|
@stream.write_uint8(0)
|
|
|
|
end
|
2016-12-11 13:48:12 +01:00
|
|
|
end
|
|
|
|
def try_write_objects
|
2015-11-15 15:24:43 +01:00
|
|
|
# then the objects , not code yet
|
2015-07-21 18:41:30 +02:00
|
|
|
@machine.objects.each do | id, objekt|
|
2015-06-30 17:35:37 +02:00
|
|
|
next if objekt.is_a? Parfait::BinaryCode
|
2015-11-04 09:34:58 +01:00
|
|
|
next if objekt.is_a? Register::Label # ignore
|
2015-06-30 17:35:37 +02:00
|
|
|
write_any( objekt )
|
2014-08-30 12:48:52 +02:00
|
|
|
end
|
2016-12-11 13:48:12 +01:00
|
|
|
end
|
2015-11-15 15:24:43 +01:00
|
|
|
|
2016-12-11 13:48:12 +01:00
|
|
|
def try_write_method
|
2015-11-15 15:24:43 +01:00
|
|
|
# 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
|
2015-05-26 19:17:43 +02:00
|
|
|
# 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}"
|
2015-10-25 09:54:19 +01:00
|
|
|
begin
|
2015-10-25 11:03:31 +01:00
|
|
|
#puts "assemble #{method.source.instructions}"
|
2015-10-28 13:33:38 +01:00
|
|
|
method.instructions.assemble_all( stream )
|
2015-10-25 09:54:19 +01:00
|
|
|
rescue => e
|
2015-11-04 10:48:51 +01:00
|
|
|
log.debug "Assembly error #{method.name}\n#{Sof.write(method.instructions).to_s[0...2000]}"
|
2015-10-25 09:54:19 +01:00
|
|
|
raise e
|
2015-05-26 19:17:43 +02:00
|
|
|
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)
|
2015-05-26 19:17:43 +02:00
|
|
|
index = 1
|
|
|
|
stream.each_byte do |b|
|
2015-11-14 14:04:04 +01:00
|
|
|
method.binary.set_char(index , b )
|
2015-05-26 19:17:43 +02:00
|
|
|
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
|
2015-05-31 13:45:28 +02:00
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
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
|
2015-11-14 14:04:04 +01:00
|
|
|
raise "Write #{obj.class} #{obj.object_id} at #{stream_position} not #{obj.position}"
|
2015-05-26 19:17:43 +02:00
|
|
|
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)
|
2015-06-09 11:37:32 +02:00
|
|
|
write_String obj
|
2015-06-08 12:24:28 +02:00
|
|
|
else
|
2015-06-09 11:37:32 +02:00
|
|
|
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
|
2015-06-09 11:37:32 +02:00
|
|
|
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)
|
2015-11-14 14:04:04 +01:00
|
|
|
log.debug "Write object #{object.class} #{object.inspect}"
|
2015-07-21 18:41:30 +02:00
|
|
|
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
|
2015-10-25 18:16:12 +01:00
|
|
|
if( object.is_a? Parfait::Indexed)
|
2015-06-08 12:19:53 +02:00
|
|
|
object.each do |inst|
|
|
|
|
write_ref_for(inst)
|
2015-11-04 09:34:58 +01:00
|
|
|
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
|
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_BinaryCode code
|
|
|
|
write_String code
|
2015-05-30 10:55:46 +02:00
|
|
|
end
|
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_String( string )
|
2015-11-14 14:04:04 +01:00
|
|
|
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
|
2015-11-14 14:04:04 +01:00
|
|
|
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)
|
2015-11-15 19:42:07 +01:00
|
|
|
@stream.write_sint32( MARKER )
|
2016-02-25 20:50:10 +01:00
|
|
|
write_ref_for( string.get_type ) #ref
|
2015-11-14 14:04:04 +01:00
|
|
|
@stream.write_sint32( str.length ) #int
|
2014-08-26 21:35:56 +02:00
|
|
|
@stream.write str
|
2016-02-25 20:50:10 +01:00
|
|
|
pad_after(str.length + 8 ) # type , length *4 == 12
|
2015-11-04 10:48:51 +01:00
|
|
|
log.debug "String (#{string.length}) stream #{@stream.length}"
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_Symbol(sym)
|
|
|
|
return write_String(sym)
|
2014-08-26 21:35:56 +02:00
|
|
|
end
|
|
|
|
|
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-10-16 16:13:08 +02:00
|
|
|
case object
|
|
|
|
when nil
|
2015-11-03 15:24:12 +01:00
|
|
|
@stream.write_sint32(0)
|
2015-10-16 16:13:08 +02:00
|
|
|
when Fixnum
|
2015-11-03 15:24:12 +01:00
|
|
|
@stream.write_sint32(object)
|
2015-06-08 12:19:53 +02:00
|
|
|
else
|
2015-11-03 15:24:12 +01:00
|
|
|
@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
|
|
|
|
|
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-07-02 12:49:33 +02:00
|
|
|
before = stream_position
|
2015-11-15 19:42:07 +01:00
|
|
|
pad = padding_for(length) - 4 # four is for the MARKER we write
|
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-07-02 12:49:33 +02:00
|
|
|
after = stream_position
|
2015-11-04 10:48:51 +01:00
|
|
|
log.debug "padded #{length} with #{pad} stream #{before}/#{after}"
|
2014-09-05 19:56:05 +02:00
|
|
|
end
|
2014-09-07 16:31:40 +02:00
|
|
|
|
2015-07-02 12:49:33 +02:00
|
|
|
# return the stream length as hex
|
2015-06-27 14:17:15 +02:00
|
|
|
def stream_position
|
2015-11-04 09:34:58 +01:00
|
|
|
@stream.length
|
2015-06-27 14:17:15 +02:00
|
|
|
end
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|
2014-08-26 15:36:12 +02:00
|
|
|
|
2015-10-22 17:16:29 +02:00
|
|
|
Sof::Volotile.add(Assembler , [:objects])
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|