2015-10-18 18:27:02 +02:00
|
|
|
require_relative "collector"
|
2018-03-27 19:47:41 +02:00
|
|
|
require_relative "binary_writer"
|
2016-12-08 19:13:08 +01:00
|
|
|
|
2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2015-10-24 10:42:36 +02:00
|
|
|
|
2018-03-11 11:41:15 +01:00
|
|
|
# From code, the next step down is Vool, then Mom (in two steps)
|
2014-06-25 01:33:44 +02:00
|
|
|
#
|
2015-06-20 22:49:30 +02:00
|
|
|
# The next step transforms to the register machine layer, which is quite close to what actually
|
|
|
|
# executes. The step after transforms to Arm, which creates executables.
|
2014-06-25 01:33:44 +02:00
|
|
|
#
|
2015-06-20 22:49:30 +02:00
|
|
|
|
2018-07-01 12:45:14 +02:00
|
|
|
class Linker
|
2018-05-20 13:45:48 +02:00
|
|
|
include Util::Logging
|
2018-05-23 17:06:55 +02:00
|
|
|
log_level :info
|
2015-07-18 10:53:04 +02:00
|
|
|
|
2018-07-03 09:12:22 +02:00
|
|
|
def initialize(platform , assemblers , constants)
|
2018-07-01 20:27:27 +02: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 20:51:48 +02:00
|
|
|
@assemblers = assemblers
|
2018-07-03 09:12:22 +02:00
|
|
|
@constants = constants
|
2018-07-02 14:51:50 +02:00
|
|
|
@cpu_init = cpu_init_init
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2018-06-29 10:36:14 +02:00
|
|
|
|
2018-07-02 15:19:01 +02:00
|
|
|
attr_reader :constants , :cpu_init
|
2018-07-01 20:51:48 +02:00
|
|
|
attr_reader :platform , :assemblers
|
2015-10-24 10:42:36 +02:00
|
|
|
|
2018-06-15 08:18:39 +02:00
|
|
|
# machine keeps a list of all objects and their positions.
|
|
|
|
# this is lazily created with a collector
|
|
|
|
def object_positions
|
2018-07-01 13:11:29 +02:00
|
|
|
Collector.collect_space(self) if Position.positions.length < 2 #one is the label
|
2018-06-15 20:54:21 +02:00
|
|
|
Position.positions
|
2018-03-27 17:47:39 +02:00
|
|
|
end
|
|
|
|
|
2018-03-29 17:03:21 +02: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 09:29:38 +02: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 17:47:39 +02:00
|
|
|
def position_all
|
|
|
|
#need the initial jump at 0 and then functions
|
2018-07-02 14:51:50 +02:00
|
|
|
Position.new(@cpu_init).set(0)
|
2018-05-31 19:41:04 +02:00
|
|
|
code_start = position_objects( @platform.padding )
|
2018-03-27 17:47:39 +02:00
|
|
|
# and then everything code
|
2018-05-31 19:41:04 +02:00
|
|
|
position_code(code_start)
|
2018-03-27 17:47:39 +02:00
|
|
|
end
|
|
|
|
|
2018-03-29 17:03:21 +02:00
|
|
|
# go through everything that is not code (BinaryCode) and set position
|
|
|
|
# padded_length is what determines an objects (byte) length
|
2018-05-01 18:48:11 +02:00
|
|
|
# return final position that is stored in code_start
|
|
|
|
def position_objects(at)
|
2018-03-27 17:47:39 +02:00
|
|
|
# want to have the objects first in the executable
|
2018-06-15 20:54:21 +02:00
|
|
|
sorted = object_positions.keys.sort do |left,right|
|
2018-06-15 08:18:39 +02:00
|
|
|
left.class.name <=> right.class.name
|
|
|
|
end
|
2018-05-31 19:41:04 +02:00
|
|
|
previous = nil
|
2018-06-15 20:54:21 +02:00
|
|
|
sorted.each do |objekt|
|
2018-06-15 21:00:49 +02:00
|
|
|
next unless Position.is_object(objekt)
|
2018-05-05 18:32:01 +02:00
|
|
|
before = at
|
2018-06-16 09:58:54 +02:00
|
|
|
raise objekt.class unless( Position.set?(objekt)) #debug check
|
2018-06-15 20:54:21 +02:00
|
|
|
position = Position.get(objekt).set(at)
|
2018-06-02 22:02:59 +02:00
|
|
|
previous.position_listener(objekt) if previous
|
2018-05-31 19:41:04 +02:00
|
|
|
previous = position
|
2018-03-29 17:03:21 +02:00
|
|
|
at += objekt.padded_length
|
2018-07-02 22:03:00 +02:00
|
|
|
log.debug "Object #{objekt.class}:0x#{before.to_s(16)} len: #{(at - before).to_s(16)}"
|
2018-03-27 17:47:39 +02:00
|
|
|
end
|
|
|
|
at
|
|
|
|
end
|
|
|
|
|
2018-03-29 17:03:21 +02:00
|
|
|
# Position all BinaryCode.
|
|
|
|
#
|
2018-05-01 18:48:11 +02:00
|
|
|
# So that all code from one method is layed out linearly (for debugging)
|
2018-03-29 17:03:21 +02:00
|
|
|
# we go through methods, and then through all codes from the method
|
2018-05-01 18:48:11 +02:00
|
|
|
#
|
2018-05-31 19:41:04 +02:00
|
|
|
# start at code_start.
|
|
|
|
def position_code(code_start)
|
2018-07-02 08:37:58 +02:00
|
|
|
assemblers.each do |asm|
|
2018-07-30 09:23:42 +02: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 08:37:58 +02:00
|
|
|
instructions = asm.instructions
|
2018-07-30 09:23:42 +02:00
|
|
|
InstructionListener.init( instructions, asm.callable.binary)
|
2018-07-02 08:37:58 +02:00
|
|
|
code_pos.position_listener( LabelListener.new(instructions))
|
|
|
|
code_pos.set(code_start)
|
2018-07-30 09:23:42 +02:00
|
|
|
code_start = Position.get(asm.callable.binary.last_code).next_slot
|
2018-06-02 16:29:38 +02:00
|
|
|
end
|
2018-03-27 17:47:39 +02:00
|
|
|
end
|
|
|
|
|
2018-03-29 17:03:21 +02:00
|
|
|
# Create Binary code for all methods and the initial jump
|
|
|
|
# BinaryWriter handles the writing from instructions into BinaryCode objects
|
2018-05-01 18:48:11 +02:00
|
|
|
#
|
2018-03-27 19:47:41 +02:00
|
|
|
def create_binary
|
2018-07-02 08:37:58 +02:00
|
|
|
prerun
|
|
|
|
assemble
|
2018-07-02 14:51:50 +02:00
|
|
|
log.debug "BinaryInit #{@cpu_init.object_id.to_s(16)}"
|
2018-06-17 12:53:17 +02:00
|
|
|
end
|
|
|
|
|
2018-07-02 08:37:58 +02:00
|
|
|
def prerun
|
|
|
|
assemblers.each do |asm|
|
|
|
|
asm.instructions.each {|i| i.precheck }
|
2018-06-17 12:53:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-02 08:37:58 +02:00
|
|
|
def assemble
|
|
|
|
assemblers.each do |asm|
|
2018-07-30 09:23:42 +02:00
|
|
|
writer = BinaryWriter.new(asm.callable.binary)
|
2018-07-02 08:37:58 +02:00
|
|
|
writer.assemble(asm.instructions)
|
2018-03-27 19:47:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-02 14:51:50 +02: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 09:23:42 +02:00
|
|
|
init = assemblers.find {|asm| asm.callable.name == :__init__}
|
|
|
|
risc_init = Branch.new( "__initial_branch__" , init.callable.binary )
|
2018-07-02 14:51:50 +02:00
|
|
|
@platform.translator.translate(risc_init)
|
|
|
|
end
|
|
|
|
|
2014-06-25 01:33:44 +02:00
|
|
|
end
|
2015-06-01 07:40:17 +02:00
|
|
|
|
2018-07-01 12:45:14 +02:00
|
|
|
# module method to reset, and init
|
2018-07-01 13:11:29 +02:00
|
|
|
def self.boot!
|
2018-07-01 12:45:14 +02:00
|
|
|
Position.clear_positions
|
|
|
|
Builtin.boot_functions
|
2015-06-01 07:40:17 +02:00
|
|
|
end
|
2015-06-12 17:52:06 +02:00
|
|
|
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|