2017-01-19 09:02:29 +02:00
|
|
|
module Risc
|
2018-03-29 18:17:19 +03: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)
|
|
|
|
#
|
2018-07-02 17:05:02 +03:00
|
|
|
# This class serves to write all the objects of the linker (wich also contain the code)
|
2018-03-29 18:17:19 +03:00
|
|
|
# into one stream or binary text object. This is then written to an ELF text section.
|
|
|
|
#
|
2018-05-05 19:32:01 +03:00
|
|
|
# A word about positions: The c world has a thing called position independent code, and
|
2019-09-05 13:25:40 +03:00
|
|
|
# basically we follw that idea. Code (ie jumps and constant loads) are all relative.
|
2018-05-05 19:32:01 +03:00
|
|
|
# But we have pointers. In C the linker takes care of bending those, we have to
|
|
|
|
# do that ourselves, in write_ref. That's why we need the load adddess and basically
|
|
|
|
# we just add it to pointers.
|
2018-03-29 18:17:19 +03:00
|
|
|
|
|
|
|
class TextWriter
|
2018-05-20 14:45:48 +03:00
|
|
|
include Util::Logging
|
2018-05-01 19:48:11 +03:00
|
|
|
log_level :info
|
2015-05-12 15:36:44 +03:00
|
|
|
|
2018-07-02 17:05:02 +03:00
|
|
|
def initialize(linker)
|
|
|
|
@linker = linker
|
|
|
|
raise "may not me nil" if linker.nil?
|
2014-08-25 17:03:39 +03:00
|
|
|
end
|
|
|
|
|
2018-07-02 17:05:02 +03:00
|
|
|
# objects must be written in same order as positioned by the linker, namely
|
2018-03-29 18:17:19 +03:00
|
|
|
# - intial jump
|
|
|
|
# - all objects
|
|
|
|
# - all BinaryCode
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_as_string
|
2018-03-29 12:16:27 +03:00
|
|
|
@stream = StringIO.new
|
2018-07-02 17:05:02 +03:00
|
|
|
write_init(@linker.cpu_init)
|
2018-03-26 18:14:39 +03:00
|
|
|
write_debug
|
|
|
|
write_objects
|
2018-03-26 19:17:30 +03:00
|
|
|
write_code
|
2018-03-29 12:16:27 +03:00
|
|
|
log.debug "Assembled 0x#{stream_position.to_s(16)} bytes"
|
2016-12-11 16:12:39 +02:00
|
|
|
return @stream.string
|
|
|
|
end
|
|
|
|
|
2018-05-13 15:28:10 +03:00
|
|
|
def sorted_objects
|
2018-07-02 17:05:02 +03:00
|
|
|
@linker.object_positions.keys.sort do |left , right|
|
2018-05-13 15:28:10 +03:00
|
|
|
Position.get(left).at <=> Position.get(right).at
|
|
|
|
end
|
|
|
|
end
|
2018-03-29 18:17:19 +03:00
|
|
|
# debugging loop to write out positions (in debug)
|
2018-03-26 18:14:39 +03:00
|
|
|
def write_debug
|
2018-05-13 15:28:10 +03:00
|
|
|
sorted_objects.each do |objekt|
|
2017-01-19 09:02:29 +02:00
|
|
|
next if objekt.is_a?(Risc::Label)
|
2018-05-06 20:04:02 +03:00
|
|
|
log.debug "Linked #{objekt.class}:0x#{objekt.object_id.to_s(16)} at #{Position.get(objekt)} / 0x#{objekt.padded_length.to_s(16)}"
|
2015-06-30 18:35:37 +03:00
|
|
|
end
|
2016-12-11 14:48:12 +02:00
|
|
|
end
|
2015-11-15 16:24:43 +02:00
|
|
|
|
2018-05-13 15:28:10 +03:00
|
|
|
# Write all the objects in the order that they have been positioed
|
2018-03-26 18:14:39 +03:00
|
|
|
def write_objects
|
2018-05-13 15:28:10 +03:00
|
|
|
sorted_objects.each do |objekt|
|
2018-06-15 22:00:49 +03:00
|
|
|
next unless Position.is_object(objekt)
|
2015-06-30 18:35:37 +03:00
|
|
|
write_any( objekt )
|
2014-08-30 13:48:52 +03:00
|
|
|
end
|
2016-12-11 14:48:12 +02:00
|
|
|
end
|
2015-11-15 16:24:43 +02:00
|
|
|
|
2018-03-29 12:16:27 +03:00
|
|
|
# Write the BinaryCode objects of all methods to stream.
|
|
|
|
# Really like any other object, it's just about the ordering
|
2018-03-26 19:17:30 +03:00
|
|
|
def write_code
|
2018-07-02 17:05:02 +03:00
|
|
|
@linker.assemblers.each do |asm|
|
2018-07-30 10:23:42 +03:00
|
|
|
asm.callable.each_binary do |code|
|
2018-07-02 17:05:02 +03:00
|
|
|
write_any(code)
|
2018-03-29 12:16:27 +03:00
|
|
|
end
|
2015-05-26 20:17:43 +03:00
|
|
|
end
|
|
|
|
end
|
2015-05-31 14:45:28 +03:00
|
|
|
|
2018-03-29 18:17:19 +03:00
|
|
|
# Write any object just logs a bit and passes to write_any_out
|
2018-03-29 12:16:27 +03:00
|
|
|
def write_any( obj )
|
2016-12-11 16:48:01 +02:00
|
|
|
write_any_log( obj , "Write")
|
2018-05-13 18:01:45 +03:00
|
|
|
if stream_position != Position.get(obj).at
|
|
|
|
raise "Write #{obj.class}:0x#{obj.object_id.to_s(16)} at 0x#{stream_position.to_s(16)} not #{Position.get(obj)}"
|
2015-05-26 20:17:43 +03:00
|
|
|
end
|
2016-12-11 16:12:39 +02:00
|
|
|
write_any_out(obj)
|
2016-12-11 16:48:01 +02:00
|
|
|
write_any_log( obj , "Wrote")
|
2018-05-06 20:04:02 +03:00
|
|
|
Position.get(obj)
|
2016-12-11 16:12:39 +02:00
|
|
|
end
|
|
|
|
|
2016-12-11 16:48:01 +02:00
|
|
|
def write_any_log( obj , at)
|
2018-05-13 15:28:10 +03:00
|
|
|
log.debug "#{at} #{obj.class}:0x#{obj.object_id.to_s(16)} at stream 0x#{stream_position.to_s(16)} pos:#{Position.get(obj)} , len:0x#{obj.padded_length.to_s(16)}"
|
2016-12-11 16:48:01 +02:00
|
|
|
end
|
|
|
|
|
2018-03-29 18:17:19 +03: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 16:12:39 +02:00
|
|
|
def write_any_out(obj)
|
2018-03-29 17:39:31 +03:00
|
|
|
case obj
|
|
|
|
when Parfait::Word, Symbol
|
2015-06-09 11:37:32 +02:00
|
|
|
write_String obj
|
2018-03-29 17:39:31 +03:00
|
|
|
when Parfait::BinaryCode
|
2018-05-31 00:07:58 +03:00
|
|
|
write_BinaryCode( obj )
|
|
|
|
when Parfait::ReturnAddress
|
|
|
|
write_return_address( obj )
|
2018-05-30 14:55:17 +03:00
|
|
|
when Parfait::Integer
|
2018-05-31 00:07:58 +03:00
|
|
|
write_integer( obj )
|
2018-03-31 19:12:06 +03:00
|
|
|
when Parfait::Data4
|
2018-05-31 00:07:58 +03:00
|
|
|
write_data4( obj )
|
2015-06-08 12:24:28 +02:00
|
|
|
else
|
2018-05-31 00:07:58 +03:00
|
|
|
write_object( obj )
|
2015-06-08 12:24:28 +02:00
|
|
|
end
|
2014-09-09 13:28:07 +03:00
|
|
|
end
|
2018-03-29 18:17:19 +03:00
|
|
|
|
2016-02-25 12:03:11 -08:00
|
|
|
# write type of the instance, and the variables that are passed
|
2014-08-29 21:00:25 +03: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 16:48:01 +02:00
|
|
|
obj_written = write_object_variables(object)
|
2018-03-29 12:16:27 +03:00
|
|
|
log.debug "instances=#{object.get_instance_variables.inspect} mem_len=0x#{object.padded_length.to_s(16)}"
|
2016-12-11 16:48:01 +02:00
|
|
|
indexed_written = write_object_indexed(object)
|
|
|
|
log.debug "type #{obj_written} , total #{obj_written + indexed_written} (array #{indexed_written})"
|
2018-03-29 12:16:27 +03: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 16:48:01 +02:00
|
|
|
pad_after( obj_written + indexed_written )
|
2018-05-06 20:04:02 +03:00
|
|
|
Position.get(object)
|
2016-12-11 16:48:01 +02:00
|
|
|
end
|
|
|
|
|
2016-12-11 14:48:12 +02:00
|
|
|
def write_object_indexed(object)
|
|
|
|
written = 0
|
2016-12-31 15:08:32 +02: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 10:34:58 +02:00
|
|
|
written += 4
|
2015-06-08 12:19:53 +02:00
|
|
|
end
|
|
|
|
end
|
2016-12-11 14:48:12 +02:00
|
|
|
written
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_object_variables(object)
|
2018-03-26 18:14:39 +03:00
|
|
|
written = 0 # compensate for the "secret" marker
|
2016-12-11 14:48:12 +02: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 12:16:27 +03:00
|
|
|
inst = nil if [:cpu_instructions , :risc_instructions].include?(var)
|
2016-12-11 14:48:12 +02:00
|
|
|
write_ref_for(inst)
|
|
|
|
written += 4
|
|
|
|
end
|
|
|
|
written
|
2014-08-29 15:49:59 +03:00
|
|
|
end
|
|
|
|
|
2018-05-31 00:07:58 +03:00
|
|
|
def write_return_address( addr )
|
|
|
|
write_ref_for( addr.get_type )
|
|
|
|
write_ref_for( addr.next_integer )
|
2018-08-30 16:38:00 +03:00
|
|
|
val = addr.value ? addr.value + @linker.platform.loaded_at : Parfait.object_space.nil_object
|
|
|
|
write_ref_for( val )
|
2018-05-31 00:07:58 +03:00
|
|
|
write_ref_for( 0 )
|
|
|
|
log.debug "Integer witten stream 0x#{@stream.length.to_s(16)}"
|
|
|
|
end
|
|
|
|
|
2018-05-30 14:55:17 +03:00
|
|
|
def write_integer( int )
|
|
|
|
write_ref_for( int.get_type )
|
|
|
|
write_ref_for( int.next_integer )
|
2018-05-30 23:49:01 +03:00
|
|
|
write_ref_for( int.value )
|
|
|
|
write_ref_for( 0 )
|
2018-05-30 14:55:17 +03:00
|
|
|
log.debug "Integer witten stream 0x#{@stream.length.to_s(16)}"
|
|
|
|
end
|
|
|
|
|
2018-05-13 15:28:10 +03:00
|
|
|
def write_data4( code )
|
2018-05-30 14:55:17 +03:00
|
|
|
write_ref_for( code.get_type )
|
|
|
|
write_ref_for( code.get_type )
|
2018-05-14 15:17:04 +03:00
|
|
|
write_ref_for( code.get_type )
|
2018-03-31 13:58:08 +03:00
|
|
|
write_ref_for( code.get_type )
|
2018-03-31 19:12:06 +03:00
|
|
|
log.debug "Data4 witten stream 0x#{@stream.length.to_s(16)}"
|
2018-03-31 13:58:08 +03:00
|
|
|
end
|
|
|
|
|
2018-05-12 18:36:59 +03:00
|
|
|
# first jump,
|
|
|
|
def write_init( cpu_init )
|
|
|
|
cpu_init.assemble(@stream)
|
2018-07-02 17:05:02 +03:00
|
|
|
@stream.write_unsigned_int_8(0) until @linker.platform.padding == stream_position
|
2018-05-01 19:20:16 +03:00
|
|
|
log.debug "Init witten stream 0x#{@stream.length.to_s(16)}"
|
2018-04-30 13:28:55 +03:00
|
|
|
end
|
|
|
|
|
2018-03-29 17:39:31 +03:00
|
|
|
def write_BinaryCode( code )
|
|
|
|
write_ref_for( code.get_type )
|
2018-08-12 14:48:20 +03:00
|
|
|
write_ref_for( code.next_code )
|
2018-03-29 17:39:31 +03:00
|
|
|
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 11:55:46 +03:00
|
|
|
end
|
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_String( string )
|
2015-11-14 15:04:04 +02: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 08:33:23 +03:00
|
|
|
str = string.to_s if string.is_a? Symbol
|
2018-05-06 20:04:02 +03:00
|
|
|
log.debug "#{string.class} is #{string} at 0x#{Position.get(string)} length 0x#{string.length.to_s(16)}"
|
2016-12-11 16:12:39 +02:00
|
|
|
write_checked_string(string , str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_checked_string(string, str)
|
2016-02-25 11:50:10 -08:00
|
|
|
write_ref_for( string.get_type ) #ref
|
2016-12-31 18:46:17 +02:00
|
|
|
@stream.write_signed_int_32( str.length ) #int
|
2014-08-26 22:35:56 +03:00
|
|
|
@stream.write str
|
2018-05-30 14:55:17 +03:00
|
|
|
pad_after(str.length + 8 ) # type , length
|
2018-03-29 12:16:27 +03:00
|
|
|
log.debug "String (0x#{string.length.to_s(16)}) stream 0x#{@stream.length.to_s(16)}"
|
2014-08-26 22:35:56 +03:00
|
|
|
end
|
|
|
|
|
2015-06-09 11:37:32 +02:00
|
|
|
def write_Symbol(sym)
|
|
|
|
return write_String(sym)
|
2014-08-26 22:35:56 +03:00
|
|
|
end
|
|
|
|
|
2015-05-12 15:36:44 +03:00
|
|
|
private
|
2014-08-29 21:00:25 +03:00
|
|
|
|
2015-05-30 12:20:39 +03:00
|
|
|
# write means we write the resulting address straight into the assembler stream
|
2014-09-16 16:06:56 +03:00
|
|
|
# object means the object of which we write the address
|
2014-09-17 12:04:54 +03:00
|
|
|
def write_ref_for object
|
2015-10-16 17:13:08 +03:00
|
|
|
case object
|
|
|
|
when nil
|
2016-12-31 18:46:17 +02:00
|
|
|
@stream.write_signed_int_32(0)
|
2019-02-07 18:24:35 +02:00
|
|
|
when ::Integer
|
2016-12-31 18:46:17 +02:00
|
|
|
@stream.write_signed_int_32(object)
|
2015-06-08 12:19:53 +02:00
|
|
|
else
|
2018-07-02 17:05:02 +03:00
|
|
|
@stream.write_signed_int_32(Position.get(object) + @linker.platform.loaded_at)
|
2015-06-08 12:19:53 +02:00
|
|
|
end
|
2014-08-29 21:00:25 +03:00
|
|
|
end
|
|
|
|
|
2014-09-07 17:31:40 +03:00
|
|
|
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
|
2018-03-29 18:17:19 +03:00
|
|
|
def pad_after( length )
|
2015-07-02 13:49:33 +03:00
|
|
|
before = stream_position
|
2019-08-17 21:07:07 +03:00
|
|
|
pad = Parfait::Object.padded(length) - length # for header, type
|
2014-09-05 20:56:05 +03:00
|
|
|
pad.times do
|
2016-12-31 18:46:17 +02:00
|
|
|
@stream.write_unsigned_int_8(0)
|
2014-09-05 20:56:05 +03:00
|
|
|
end
|
2015-07-02 13:49:33 +03:00
|
|
|
after = stream_position
|
2018-03-29 12:16:27 +03: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 20:56:05 +03:00
|
|
|
end
|
2014-09-07 17:31:40 +03:00
|
|
|
|
2015-07-02 13:49:33 +03:00
|
|
|
# return the stream length as hex
|
2015-06-27 15:17:15 +03:00
|
|
|
def stream_position
|
2015-11-04 10:34:58 +02:00
|
|
|
@stream.length
|
2015-06-27 15:17:15 +03:00
|
|
|
end
|
2014-08-25 17:03:39 +03:00
|
|
|
end
|
2014-08-26 16:36:12 +03:00
|
|
|
|
2014-08-25 17:03:39 +03:00
|
|
|
end
|