2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
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-11-04 10:48:51 +01:00
|
|
|
include Logging
|
2016-12-31 14:08:32 +01:00
|
|
|
log_level :info
|
2015-05-12 14:36:44 +02:00
|
|
|
|
2015-11-15 19:42:07 +01:00
|
|
|
MARKER = 0xA51AF00D
|
|
|
|
|
2016-12-31 17:46:17 +01:00
|
|
|
def initialize( machine , objects)
|
2015-05-31 12:02:29 +02:00
|
|
|
@machine = machine
|
2016-12-31 17:46:17 +01:00
|
|
|
@objects = objects
|
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
|
2018-03-25 18:36:00 +02:00
|
|
|
@machine.cpu_init.set_position( 0)
|
|
|
|
at = @machine.cpu_init.byte_length
|
2016-12-31 17:46:17 +01:00
|
|
|
at = assemble_objects( at )
|
2015-11-15 15:24:43 +01:00
|
|
|
# and then everything code
|
2016-12-31 17:46:17 +01:00
|
|
|
asseble_code_from( at )
|
2016-12-11 13:48:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def asseble_code_from( at )
|
2016-12-31 17:46:17 +01:00
|
|
|
@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}"
|
2018-03-25 17:23:00 +02:00
|
|
|
# create binary for assembly
|
|
|
|
objekt.create_binary if objekt.is_a? Parfait::TypedMethod
|
2016-12-16 14:41:26 +01:00
|
|
|
binary = objekt.binary
|
2017-01-01 20:52:55 +01:00
|
|
|
Positioned.set_position(binary,at)
|
2018-03-25 18:36:00 +02:00
|
|
|
objekt.risc_instructions.set_position( at + 12) # BinaryCode header
|
2018-03-25 17:23:00 +02:00
|
|
|
len = 4 * 14
|
2016-12-16 14:41:26 +01:00
|
|
|
at += binary.padded_length
|
2018-03-25 17:23:00 +02:00
|
|
|
nekst = binary.next
|
|
|
|
while(nekst)
|
|
|
|
Positioned.set_position(nekst , at)
|
|
|
|
at += binary.padded_length
|
|
|
|
nekst = nekst.next
|
|
|
|
len += 4 * 16
|
|
|
|
#puts "LENGTH #{len}"
|
|
|
|
end
|
|
|
|
log.debug "CODE2 #{objekt.name} at #{Positioned.position(binary)} len: #{len}"
|
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
|
|
|
|
2018-03-25 17:23:00 +02:00
|
|
|
def assemble_objects( at )
|
2016-12-11 13:48:12 +01:00
|
|
|
at += 8 # thats the padding
|
|
|
|
# want to have the objects first in the executable
|
2016-12-31 17:46:17 +01:00
|
|
|
@objects.each do | id , objekt|
|
2018-03-25 18:36:00 +02:00
|
|
|
next if objekt.is_a? Risc::Label # will get assembled as method.risc_instructions
|
2016-12-11 13:48:12 +01:00
|
|
|
next if objekt.is_a? Parfait::BinaryCode
|
2017-01-01 20:52:55 +01:00
|
|
|
Positioned.set_position(objekt,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
|
2017-01-01 20:52:55 +01:00
|
|
|
all = @objects.values.sort{|a,b| Positioned.position(a) <=> Positioned.position(b)}
|
2015-06-30 17:35:37 +02:00
|
|
|
all.each do |objekt|
|
2017-01-19 08:02:29 +01:00
|
|
|
next if objekt.is_a?(Risc::Label)
|
2017-01-01 20:52:55 +01:00
|
|
|
log.debug "Linked #{objekt.class}(#{objekt.object_id}) at #{Positioned.position(objekt)} / #{objekt.padded_length}"
|
|
|
|
Positioned.position(objekt)
|
2015-06-30 17:35:37 +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_create_binary
|
2015-06-30 17:35:37 +02:00
|
|
|
# first we need to create the binary code for the methods
|
2016-12-31 17:46:17 +01:00
|
|
|
@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
|
2016-12-31 17:46:17 +01:00
|
|
|
@stream.write_unsigned_int_8(0)
|
2015-06-30 17:35:37 +02:00
|
|
|
end
|
2016-12-11 13:48:12 +01:00
|
|
|
end
|
2017-01-01 20:52:55 +01:00
|
|
|
|
2016-12-11 13:48:12 +01:00
|
|
|
def try_write_objects
|
2015-11-15 15:24:43 +01:00
|
|
|
# then the objects , not code yet
|
2016-12-31 17:46:17 +01:00
|
|
|
@objects.each do | id, objekt|
|
2015-06-30 17:35:37 +02:00
|
|
|
next if objekt.is_a? Parfait::BinaryCode
|
2017-01-19 08:02:29 +01:00
|
|
|
next if objekt.is_a? Risc::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
|
2016-12-31 17:46:17 +01:00
|
|
|
@objects.each do |id, objekt|
|
2015-11-15 15:24:43 +01:00
|
|
|
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
|
2018-03-25 18:36:00 +02:00
|
|
|
#puts "Method #{method.source.risc_instructions.to_ac}"
|
2015-10-25 09:54:19 +01:00
|
|
|
begin
|
2018-03-25 18:36:00 +02:00
|
|
|
#puts "assemble #{method.source.risc_instructions}"
|
|
|
|
method.risc_instructions.assemble_all( stream )
|
2015-10-25 09:54:19 +01:00
|
|
|
rescue => e
|
2018-03-21 11:32:46 +01:00
|
|
|
log.debug "Assembly error #{method.name}\n#{method.to_rxf.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
|
2018-03-25 18:36:00 +02:00
|
|
|
total_byte_length = method.risc_instructions.total_byte_length
|
2016-12-16 14:41:26 +01:00
|
|
|
log.debug "Assembled code #{method.name} with length #{length}"
|
2018-03-25 17:23:00 +02:00
|
|
|
raise "length error #{binary.char_length} != #{total_byte_length}" if binary.char_length <= total_byte_length
|
2016-12-16 14:41:26 +01:00
|
|
|
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")
|
2017-01-01 20:52:55 +01:00
|
|
|
if @stream.length != Positioned.position(obj)
|
|
|
|
raise "Write #{obj.class} #{obj.object_id} at #{stream_position} not #{Positioned.position(obj)}"
|
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")
|
2017-01-01 20:52:55 +01:00
|
|
|
Positioned.position(obj)
|
2016-12-11 15:12:39 +01:00
|
|
|
end
|
|
|
|
|
2016-12-11 15:48:01 +01:00
|
|
|
def write_any_log( obj , at)
|
2017-01-01 20:52:55 +01:00
|
|
|
log.debug "#{at} #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{Positioned.position(obj)} , len:#{obj.padded_length}"
|
2016-12-11 15:48:01 +01:00
|
|
|
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 )
|
2017-01-01 20:52:55 +01:00
|
|
|
Positioned.position(object)
|
2016-12-11 15:48:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_object_check(object)
|
2015-11-14 14:04:04 +01:00
|
|
|
log.debug "Write object #{object.class} #{object.inspect}"
|
2016-12-31 17:46:17 +01:00
|
|
|
unless @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
|
2016-12-31 14:08:32 +01:00
|
|
|
if( object.is_a? Parfait::List)
|
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)
|
2016-12-31 17:46:17 +01:00
|
|
|
@stream.write_signed_int_32( MARKER )
|
2016-12-11 13:48:12 +01:00
|
|
|
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
|
2017-01-01 20:52:55 +01:00
|
|
|
log.debug "#{string.class} is #{string} at #{Positioned.position(string)} length #{string.length}"
|
2016-12-11 15:12:39 +01:00
|
|
|
write_checked_string(string , str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_checked_string(string, str)
|
2016-12-31 17:46:17 +01:00
|
|
|
@stream.write_signed_int_32( MARKER )
|
2016-02-25 20:50:10 +01:00
|
|
|
write_ref_for( string.get_type ) #ref
|
2016-12-31 17:46:17 +01:00
|
|
|
@stream.write_signed_int_32( 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
|
2016-12-31 17:46:17 +01:00
|
|
|
@stream.write_signed_int_32(0)
|
2015-10-16 16:13:08 +02:00
|
|
|
when Fixnum
|
2016-12-31 17:46:17 +01:00
|
|
|
@stream.write_signed_int_32(object)
|
2015-06-08 12:19:53 +02:00
|
|
|
else
|
2017-01-01 20:52:55 +01:00
|
|
|
@stream.write_signed_int_32(Positioned.position(object) + @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
|
2016-12-31 19:08:33 +01:00
|
|
|
pad = Padding.padding_for(length) - 4 # four is for the MARKER we write
|
2014-09-05 19:56:05 +02:00
|
|
|
pad.times do
|
2016-12-31 17:46:17 +01:00
|
|
|
@stream.write_unsigned_int_8(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
|
|
|
|
2017-10-05 15:41:45 +02:00
|
|
|
RxFile::Volotile.add(Assembler , [:objects])
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|