2015-10-18 19:27:02 +03:00
|
|
|
require_relative "collector"
|
2018-03-27 20:47:41 +03:00
|
|
|
require_relative "binary_writer"
|
2016-12-08 20:13:08 +02:00
|
|
|
|
2017-01-19 09:02:29 +02:00
|
|
|
module Risc
|
2015-10-24 11:42:36 +03:00
|
|
|
|
2019-09-05 13:25:40 +03:00
|
|
|
# A RiscCollection is the last virtual stop on the way to binary.
|
|
|
|
# It creates a Linker to go to binary. The name linker came in analogy to
|
|
|
|
# "normal" (ie c world) lingo, because there too the linker is all about
|
|
|
|
# positioning code and data.
|
2014-06-25 02:33:44 +03:00
|
|
|
#
|
2019-09-05 13:25:40 +03:00
|
|
|
# Here we also do the assembling, which (at least in arm) creates this mess
|
|
|
|
# of dependencies. As a simple example imagine a branch, a load and the label
|
|
|
|
# to which we jump. The brnahc needs the address, but the load may be 4 or 8
|
|
|
|
# instructions, and thus the label address is only known after the load.
|
|
|
|
# Exrapolate times 10, that's why this works with listeners and a sort
|
|
|
|
# of self organizing aproach (see class Position).
|
2015-06-20 23:49:30 +03:00
|
|
|
|
2018-07-01 13:45:14 +03:00
|
|
|
class Linker
|
2018-05-20 14:45:48 +03:00
|
|
|
include Util::Logging
|
2018-05-23 18:06:55 +03:00
|
|
|
log_level :info
|
2015-07-18 11:53:04 +03:00
|
|
|
|
2018-07-03 10:12:22 +03:00
|
|
|
def initialize(platform , assemblers , constants)
|
2018-07-01 21:27:27 +03:00
|
|
|
if(platform.is_a?(Symbol))
|
|
|
|
platform = platform.to_s.capitalize
|
|
|
|
platform = Risc::Platform.for(platform)
|
|
|
|
end
|
|
|
|
raise "Platform must be platform, not #{platform.class}" unless platform.is_a?(Platform)
|
|
|
|
@platform = platform
|
2018-07-01 21:51:48 +03:00
|
|
|
@assemblers = assemblers
|
2018-07-03 10:12:22 +03:00
|
|
|
@constants = constants
|
2018-07-02 15:51:50 +03:00
|
|
|
@cpu_init = cpu_init_init
|
2014-06-25 16:00:24 +03:00
|
|
|
end
|
2018-06-29 11:36:14 +03:00
|
|
|
|
2018-07-02 16:19:01 +03:00
|
|
|
attr_reader :constants , :cpu_init
|
2018-07-01 21:51:48 +03:00
|
|
|
attr_reader :platform , :assemblers
|
2015-10-24 11:42:36 +03:00
|
|
|
|
2018-06-15 09:18:39 +03:00
|
|
|
# machine keeps a list of all objects and their positions.
|
|
|
|
# this is lazily created with a collector
|
|
|
|
def object_positions
|
2018-07-01 14:11:29 +03:00
|
|
|
Collector.collect_space(self) if Position.positions.length < 2 #one is the label
|
2018-06-15 21:54:21 +03:00
|
|
|
Position.positions
|
2018-03-27 18:47:39 +03:00
|
|
|
end
|
|
|
|
|
2018-03-29 18:03:21 +03:00
|
|
|
# To create binaries, objects (and labels) need to have a position
|
|
|
|
# (so objects can be loaded and branches know where to jump)
|
|
|
|
#
|
|
|
|
# Position in the order
|
|
|
|
# - initial jump
|
2018-05-30 10:29:38 +03:00
|
|
|
# - all objects
|
|
|
|
# - all code (BinaryCode objects)
|
|
|
|
# As code length may change during assembly, this way at least the objects stay
|
|
|
|
# in place and we don't have to deal with changing loading code
|
2018-03-27 18:47:39 +03:00
|
|
|
def position_all
|
|
|
|
#need the initial jump at 0 and then functions
|
2018-07-02 15:51:50 +03:00
|
|
|
Position.new(@cpu_init).set(0)
|
2018-05-31 20:41:04 +03:00
|
|
|
code_start = position_objects( @platform.padding )
|
2018-03-27 18:47:39 +03:00
|
|
|
# and then everything code
|
2018-05-31 20:41:04 +03:00
|
|
|
position_code(code_start)
|
2018-03-27 18:47:39 +03:00
|
|
|
end
|
|
|
|
|
2018-03-29 18:03:21 +03:00
|
|
|
# go through everything that is not code (BinaryCode) and set position
|
|
|
|
# padded_length is what determines an objects (byte) length
|
2018-05-01 19:48:11 +03:00
|
|
|
# return final position that is stored in code_start
|
|
|
|
def position_objects(at)
|
2018-03-27 18:47:39 +03:00
|
|
|
# want to have the objects first in the executable
|
2018-06-15 21:54:21 +03:00
|
|
|
sorted = object_positions.keys.sort do |left,right|
|
2018-06-15 09:18:39 +03:00
|
|
|
left.class.name <=> right.class.name
|
|
|
|
end
|
2018-05-31 20:41:04 +03:00
|
|
|
previous = nil
|
2018-06-15 21:54:21 +03:00
|
|
|
sorted.each do |objekt|
|
2018-06-15 22:00:49 +03:00
|
|
|
next unless Position.is_object(objekt)
|
2018-05-05 19:32:01 +03:00
|
|
|
before = at
|
2018-06-16 10:58:54 +03:00
|
|
|
raise objekt.class unless( Position.set?(objekt)) #debug check
|
2018-06-15 21:54:21 +03:00
|
|
|
position = Position.get(objekt).set(at)
|
2018-06-02 23:02:59 +03:00
|
|
|
previous.position_listener(objekt) if previous
|
2018-05-31 20:41:04 +03:00
|
|
|
previous = position
|
2018-03-29 18:03:21 +03:00
|
|
|
at += objekt.padded_length
|
2018-07-02 23:03:00 +03:00
|
|
|
log.debug "Object #{objekt.class}:0x#{before.to_s(16)} len: #{(at - before).to_s(16)}"
|
2018-03-27 18:47:39 +03:00
|
|
|
end
|
|
|
|
at
|
|
|
|
end
|
|
|
|
|
2018-03-29 18:03:21 +03:00
|
|
|
# Position all BinaryCode.
|
|
|
|
#
|
2018-05-01 19:48:11 +03:00
|
|
|
# So that all code from one method is layed out linearly (for debugging)
|
2018-03-29 18:03:21 +03:00
|
|
|
# we go through methods, and then through all codes from the method
|
2018-05-01 19:48:11 +03:00
|
|
|
#
|
2018-05-31 20:41:04 +03:00
|
|
|
# start at code_start.
|
|
|
|
def position_code(code_start)
|
2018-07-02 09:37:58 +03:00
|
|
|
assemblers.each do |asm|
|
2018-07-30 10:23:42 +03:00
|
|
|
Position.log.debug "Method start #{code_start.to_s(16)} #{asm.callable.name}"
|
|
|
|
code_pos = CodeListener.init(asm.callable.binary, platform)
|
2018-07-02 09:37:58 +03:00
|
|
|
instructions = asm.instructions
|
2018-07-30 10:23:42 +03:00
|
|
|
InstructionListener.init( instructions, asm.callable.binary)
|
2018-07-02 09:37:58 +03:00
|
|
|
code_pos.position_listener( LabelListener.new(instructions))
|
|
|
|
code_pos.set(code_start)
|
2018-07-30 10:23:42 +03:00
|
|
|
code_start = Position.get(asm.callable.binary.last_code).next_slot
|
2018-06-02 17:29:38 +03:00
|
|
|
end
|
2018-03-27 18:47:39 +03:00
|
|
|
end
|
|
|
|
|
2018-03-29 18:03:21 +03:00
|
|
|
# Create Binary code for all methods and the initial jump
|
|
|
|
# BinaryWriter handles the writing from instructions into BinaryCode objects
|
2018-05-01 19:48:11 +03:00
|
|
|
#
|
2018-03-27 20:47:41 +03:00
|
|
|
def create_binary
|
2018-07-02 09:37:58 +03:00
|
|
|
prerun
|
|
|
|
assemble
|
2018-07-02 15:51:50 +03:00
|
|
|
log.debug "BinaryInit #{@cpu_init.object_id.to_s(16)}"
|
2018-06-17 13:53:17 +03:00
|
|
|
end
|
|
|
|
|
2018-07-02 09:37:58 +03:00
|
|
|
def prerun
|
|
|
|
assemblers.each do |asm|
|
2018-09-01 11:20:59 +03:00
|
|
|
asm.instructions.each do |ins|
|
|
|
|
ins.precheck
|
|
|
|
end
|
2018-06-17 13:53:17 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-02 09:37:58 +03:00
|
|
|
def assemble
|
|
|
|
assemblers.each do |asm|
|
2018-07-30 10:23:42 +03:00
|
|
|
writer = BinaryWriter.new(asm.callable.binary)
|
2018-07-02 09:37:58 +03:00
|
|
|
writer.assemble(asm.instructions)
|
2018-03-27 20:47:41 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-02 15:51:50 +03:00
|
|
|
private
|
|
|
|
# cpu_init come from translating the risc_init
|
|
|
|
# risc_init is a branch to the __init__ method
|
|
|
|
#
|
|
|
|
def cpu_init_init
|
2018-07-30 10:23:42 +03:00
|
|
|
init = assemblers.find {|asm| asm.callable.name == :__init__}
|
|
|
|
risc_init = Branch.new( "__initial_branch__" , init.callable.binary )
|
2018-07-02 15:51:50 +03:00
|
|
|
@platform.translator.translate(risc_init)
|
|
|
|
end
|
2015-06-01 08:40:17 +03:00
|
|
|
end
|
2015-05-08 15:10:30 +03:00
|
|
|
end
|