2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2018-03-29 17:17:19 +02:00
|
|
|
# To create a binary, we need a so called Text element. Bad name for what is the code
|
|
|
|
#
|
|
|
|
# Binary code is already created by the Machine (by translating risc to arm to binary)
|
|
|
|
#
|
|
|
|
# This class serves to write all the objects of the machine (wich also contain the code)
|
|
|
|
# into one stream or binary text object. This is then written to an ELF text section.
|
|
|
|
#
|
|
|
|
|
|
|
|
class TextWriter
|
2015-11-04 10:48:51 +01:00
|
|
|
include Logging
|
2018-04-30 12:28:55 +02:00
|
|
|
log_level :debug
|
2015-05-12 14:36:44 +02:00
|
|
|
|
2018-04-30 12:28:55 +02:00
|
|
|
MARKER = 0xBAD4C0DE
|
2015-11-15 19:42:07 +01:00
|
|
|
|
2018-03-29 11:16:27 +02:00
|
|
|
def initialize( machine)
|
2015-05-31 12:02:29 +02:00
|
|
|
@machine = machine
|
2018-03-29 11:16:27 +02:00
|
|
|
@objects = machine.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
|
|
|
|
|
2018-03-29 17:17:19 +02:00
|
|
|
# objects must be written in same order as positioned by the machine, namely
|
|
|
|
# - intial jump
|
|
|
|
# - all objects
|
|
|
|
# - all BinaryCode
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_as_string
|
2018-03-29 11:16:27 +02:00
|
|
|
@stream = StringIO.new
|
2018-04-30 12:28:55 +02:00
|
|
|
write_init(@machine.binary_init)
|
2018-03-26 17:14:39 +02:00
|
|
|
write_debug
|
|
|
|
write_objects
|
2018-03-26 18:17:30 +02:00
|
|
|
write_code
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "Assembled 0x#{stream_position.to_s(16)} bytes"
|
2016-12-11 15:12:39 +01:00
|
|
|
return @stream.string
|
|
|
|
end
|
|
|
|
|
2018-03-29 17:17:19 +02:00
|
|
|
# debugging loop to write out positions (in debug)
|
2018-03-26 17:14:39 +02:00
|
|
|
def write_debug
|
2018-03-29 17:17:19 +02:00
|
|
|
@objects.each do |id , objekt|
|
2017-01-19 08:02:29 +01:00
|
|
|
next if objekt.is_a?(Risc::Label)
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "Linked #{objekt.class}:0x#{objekt.object_id.to_s(16)} at 0x#{Positioned.position(objekt).to_s(16)} / 0x#{objekt.padded_length.to_s(16)}"
|
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
|
|
|
|
2018-03-29 17:17:19 +02:00
|
|
|
# Write all the objects
|
2018-03-26 17:14:39 +02:00
|
|
|
def 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
|
|
|
|
2018-03-29 11:16:27 +02:00
|
|
|
# Write the BinaryCode objects of all methods to stream.
|
|
|
|
# Really like any other object, it's just about the ordering
|
2018-03-26 18:17:30 +02:00
|
|
|
def write_code
|
2018-03-29 11:16:27 +02:00
|
|
|
@objects.each do |id, method|
|
|
|
|
next unless method.is_a? Parfait::TypedMethod
|
|
|
|
binary = method.binary
|
|
|
|
while(binary) do
|
|
|
|
write_any( binary )
|
|
|
|
binary = binary.next
|
|
|
|
end
|
2015-05-26 19:17:43 +02:00
|
|
|
end
|
|
|
|
end
|
2015-05-31 13:45:28 +02:00
|
|
|
|
2018-03-29 17:17:19 +02:00
|
|
|
# Write any object just logs a bit and passes to write_any_out
|
2018-03-29 11:16:27 +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)
|
2018-03-29 16:39:31 +02:00
|
|
|
raise "Write #{obj.class}:0x#{obj.object_id.to_s(16)} at 0x#{stream_position.to_s(16)} not 0x#{Positioned.position(obj).to_s(16)}"
|
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)
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "#{at} #{obj.class}:0x#{obj.object_id.to_s(16)} at stream 0x#{stream_position.to_s(16)} pos:0x#{Positioned.position(obj).to_s(16)} , len:0x#{obj.padded_length.to_s(16)}"
|
2016-12-11 15:48:01 +01:00
|
|
|
end
|
|
|
|
|
2018-03-29 17:17:19 +02:00
|
|
|
# Most objects are the same and get passed to write_object
|
|
|
|
# But Strings and BinaryCode write out binary, so they have different methods (for now)
|
2016-12-11 15:12:39 +01:00
|
|
|
def write_any_out(obj)
|
2018-03-29 16:39:31 +02:00
|
|
|
case obj
|
|
|
|
when Parfait::Word, Symbol
|
2015-06-09 11:37:32 +02:00
|
|
|
write_String obj
|
2018-03-29 16:39:31 +02:00
|
|
|
when Parfait::BinaryCode
|
|
|
|
write_BinaryCode obj
|
2018-03-31 18:12:06 +02:00
|
|
|
when Parfait::Data4
|
2018-03-31 12:58:08 +02:00
|
|
|
write_data2 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
|
2018-03-29 17:17:19 +02:00
|
|
|
|
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)
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "instances=#{object.get_instance_variables.inspect} mem_len=0x#{object.padded_length.to_s(16)}"
|
2016-12-11 15:48:01 +01:00
|
|
|
indexed_written = write_object_indexed(object)
|
|
|
|
log.debug "type #{obj_written} , total #{obj_written + indexed_written} (array #{indexed_written})"
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "Len = 0x#{object.get_length.to_s(16)} , inst =0x#{object.get_type.instance_length.to_s(16)}" if object.is_a? Parfait::Type
|
2016-12-11 15:48:01 +01:00
|
|
|
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)
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "Write object #{object.class} #{object.inspect[0..100]}"
|
2018-03-29 16:39:31 +02:00
|
|
|
#Only initially created codes are collected. Binary_init and method "tails" not
|
|
|
|
if !@objects.has_key?(object.object_id) and !object.is_a?(Parfait::BinaryCode)
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "Object at 0x#{Positioned.position(object).to_s(16)}:#{object.get_type()}"
|
|
|
|
raise "Object(0x#{object.object_id.to_s(16)}) not linked #{object.inspect}"
|
2015-05-30 11:20:39 +02:00
|
|
|
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 )
|
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
|
2018-03-29 11:16:27 +02:00
|
|
|
inst = nil if [:cpu_instructions , :risc_instructions].include?(var)
|
2016-12-11 13:48:12 +01:00
|
|
|
write_ref_for(inst)
|
|
|
|
written += 4
|
|
|
|
end
|
|
|
|
written
|
2014-08-29 14:49:59 +02:00
|
|
|
end
|
|
|
|
|
2018-03-31 12:58:08 +02:00
|
|
|
def write_data2( code )
|
|
|
|
@stream.write_signed_int_32( MARKER )
|
|
|
|
write_ref_for( code.get_type )
|
2018-03-31 18:12:06 +02:00
|
|
|
log.debug "Data4 witten stream 0x#{@stream.length.to_s(16)}"
|
2018-03-31 12:58:08 +02:00
|
|
|
end
|
|
|
|
|
2018-04-30 12:28:55 +02:00
|
|
|
# first jump, treated as a binary code, but this one needs
|
|
|
|
# the actual jump as the first
|
|
|
|
def write_init( code )
|
|
|
|
code.each_word do |word|
|
|
|
|
@stream.write_unsigned_int_32( word || 0 )
|
|
|
|
end
|
|
|
|
write_ref_for( code.next )
|
|
|
|
write_ref_for( code.get_type )
|
|
|
|
@stream.write_signed_int_32( MARKER )
|
|
|
|
log.debug "Code16 witten stream 0x#{@stream.length.to_s(16)}"
|
|
|
|
end
|
|
|
|
|
2018-03-29 16:39:31 +02:00
|
|
|
def write_BinaryCode( code )
|
|
|
|
@stream.write_signed_int_32( MARKER )
|
|
|
|
write_ref_for( code.get_type )
|
|
|
|
write_ref_for( code.next )
|
|
|
|
code.each_word do |word|
|
|
|
|
@stream.write_unsigned_int_32( word || 0 )
|
|
|
|
end
|
|
|
|
log.debug "Code16 witten stream 0x#{@stream.length.to_s(16)}"
|
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
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "#{string.class} is #{string} at 0x#{Positioned.position(string).to_s(16)} length 0x#{string.length.to_s(16)}"
|
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
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "String (0x#{string.length.to_s(16)}) stream 0x#{@stream.length.to_s(16)}"
|
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
|
2018-03-29 17:17:19 +02:00
|
|
|
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
|
2018-03-29 11:16:27 +02:00
|
|
|
log.debug "padded 0x#{length.to_s(16)} with 0x#{pad.to_s(16)} stream #{before.to_s(16)}/#{after.to_s(16)}"
|
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
|
|
|
|
2018-03-29 17:17:19 +02:00
|
|
|
RxFile::Volotile.add(TextWriter , [:objects])
|
2014-08-25 16:03:39 +02:00
|
|
|
end
|