rubyx/lib/risc/assembler.rb

280 lines
8.8 KiB
Ruby
Raw Normal View History

module Risc
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 Logging
2018-03-26 17:14:39 +02:00
log_level :debug
MARKER = 0xA51AF00D
def initialize( machine , objects)
@machine = machine
@objects = objects
@load_at = 0x8054 # this is linux/arm
end
def assemble
2015-06-10 10:43:50 +02:00
#need the initial jump at 0 and then functions
2018-03-26 17:14:39 +02:00
@machine.cpu_init.set_position( 0 )
at = @machine.cpu_init.byte_length
2018-03-26 17:14:39 +02:00
at = position_objects( at )
# and then everything code
2018-03-26 17:14:39 +02:00
position_code_from( at )
2016-12-11 13:48:12 +01:00
end
2018-03-26 17:18:25 +02:00
def position_objects( at )
at += 8 # thats the padding
# want to have the objects first in the executable
@objects.each do | id , objekt|
2018-03-26 18:17:30 +02:00
case objekt
when Risc::Label # will get assembled as method.cpu_instructions
2018-03-26 17:18:25 +02:00
Positioned.set_position(objekt,at)
2018-03-26 18:17:30 +02:00
when Parfait::BinaryCode
else
Positioned.set_position(objekt,at)
at += objekt.padded_length
2018-03-26 17:18:25 +02:00
end
end
at
end
2018-03-26 17:14:39 +02:00
def position_code_from( at )
2018-03-26 18:17:30 +02:00
@objects.each do |id , method|
next unless method.is_a? Parfait::TypedMethod
log.debug "CODE1 #{method.name}"
# create binary for assembly
2018-03-26 18:17:30 +02:00
binary = method.binary
Positioned.set_position(binary,at)
2018-03-26 18:17:30 +02:00
method.cpu_instructions.set_position( at + 12) # BinaryCode header
len = 4 * 14
2016-12-16 14:41:26 +01:00
at += binary.padded_length
nekst = binary.next
while(nekst)
Positioned.set_position(nekst , at)
at += binary.padded_length
nekst = nekst.next
len += 4 * 16
#puts "LENGTH #{len}"
end
2018-03-26 18:17:30 +02:00
log.debug "CODE2 #{method.name} at #{Positioned.position(binary)} len: #{len}"
end
2016-12-11 13:48:12 +01:00
at
end
2016-12-11 15:12:39 +01:00
2018-03-26 17:14:39 +02:00
# objects must be written in same order as positioned / assembled
def write_as_string
assemble
2018-03-26 17:14:39 +02:00
write_debug
write_create_binary
write_objects
2018-03-26 18:17:30 +02:00
write_code
2016-12-11 15:12:39 +01:00
log.debug "Assembled #{stream_position} bytes"
return @stream.string
end
# debugging loop accesses all positions to force an error if it's not set
2018-03-26 17:14:39 +02:00
def write_debug
all = @objects.values.sort{|a,b| Positioned.position(a) <=> Positioned.position(b)}
all.each do |objekt|
next if objekt.is_a?(Risc::Label)
log.debug "Linked #{objekt.class}(#{objekt.object_id}) at #{Positioned.position(objekt)} / #{objekt.padded_length}"
Positioned.position(objekt)
end
2016-12-11 13:48:12 +01:00
end
2018-03-26 17:14:39 +02:00
def write_create_binary
# first we need to create the binary code for the methods
@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_unsigned_int_8(0)
end
2016-12-11 13:48:12 +01:00
end
2018-03-26 17:14:39 +02:00
def write_objects
# then the objects , not code yet
@objects.each do | id, objekt|
next if objekt.is_a? Parfait::BinaryCode
next if objekt.is_a? Risc::Label # ignore
write_any( objekt )
end
2016-12-11 13:48:12 +01:00
end
2018-03-26 18:17:30 +02:00
def write_code
# then write the methods to file
@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
2018-03-26 18:17:30 +02:00
def assemble_binary_method( method )
stream = StringIO.new
2018-03-26 12:43:26 +02:00
#puts "Method #{method.source.cpu_instructions.to_ac}"
begin
2018-03-26 12:43:26 +02:00
#puts "assemble #{method.source.cpu_instructions}"
method.cpu_instructions.assemble_all( stream )
rescue => e
log.debug "Assembly error #{method.name}\n#{method.to_rxf.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
2018-03-26 12:43:26 +02:00
total_byte_length = method.cpu_instructions.total_byte_length
2016-12-16 14:41:26 +01:00
log.debug "Assembled code #{method.name} with length #{length}"
raise "length error #{binary.total_byte_length} != #{total_byte_length}" if binary.total_byte_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
def write_any obj
2016-12-11 15:48:01 +01:00
write_any_log( obj , "Write")
if @stream.length != Positioned.position(obj)
raise "Write #{obj.class} #{obj.object_id} at #{stream_position} not #{Positioned.position(obj)}"
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")
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)
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)
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 )
Positioned.position(object)
2016-12-11 15:48:01 +01:00
end
def write_object_check(object)
log.debug "Write object #{object.class} #{object.inspect}"
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)
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_signed_int_32( MARKER )
2018-03-26 17:14:39 +02:00
written = 0 # compensate for the "secret" marker
2016-12-11 13:48:12 +01:00
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 #{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)
@stream.write_signed_int_32( MARKER )
write_ref_for( string.get_type ) #ref
@stream.write_signed_int_32( 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_signed_int_32(0)
2015-10-16 16:13:08 +02:00
when Fixnum
@stream.write_signed_int_32(object)
2015-06-08 12:19:53 +02:00
else
@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
# 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.padding_for(length) - 4 # four is for the MARKER we write
2014-09-05 19:56:05 +02:00
pad.times do
@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
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
2017-10-05 15:41:45 +02:00
RxFile::Volotile.add(Assembler , [:objects])
end