rubyx/lib/risc/machine.rb

154 lines
5.1 KiB
Ruby
Raw Normal View History

require_relative "collector"
require_relative "binary_writer"
2016-12-08 19:13:08 +01:00
module Risc
# The Risc Machine is an abstraction of the register level. This is seperate from the
2016-12-06 10:38:09 +01:00
# actual assembler level to allow for several cpu architectures.
# The Instructions (see class Instruction) define what the machine can do (ie load/store/maths)
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
class Machine
include Util::Logging
2018-05-23 17:06:55 +02:00
log_level :info
def initialize
@booted = false
@risc_init = nil
2015-10-28 12:00:23 +01:00
@constants = []
end
attr_reader :constants , :cpu_init
attr_reader :booted , :translated
attr_reader :platform
# Translate code to whatever cpu is specified.
# Currently only :arm and :interpret
#
# Translating means translating the initial jump
2018-03-29 17:03:21 +02:00
# and then translating all methods
def translate( platform )
platform = platform.to_s.capitalize
@platform = Platform.for(platform)
@translated = true
translate_methods( @platform.translator )
@cpu_init = risc_init.to_cpu(@platform.translator)
end
2018-03-29 17:03:21 +02:00
# go through all methods and translate them to cpu, given the translator
def translate_methods(translator)
Parfait.object_space.get_all_methods.each do |method|
log.debug "Translate method #{method.name}"
method.translate_cpu(translator)
2015-06-12 17:52:06 +02:00
end
end
# machine keeps a list of all objects. this is lazily created with a collector
def objects
@objects ||= Collector.collect_space
end
# lazy init risc_init
def risc_init
@risc_init ||= Branch.new( "__initial_branch__" , Parfait.object_space.get_init.risc_instructions )
end
# add a constant (which get created during compilation and need to be linked)
def add_constant(const)
raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object)
@constants << const
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
# - 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
def position_all
raise "Not translated " unless @translated
#need the initial jump at 0 and then functions
Position.set(cpu_init , 0 , nil)
@code_start = position_objects( @platform.padding )
# and then everything code
position_code
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
# return final position that is stored in code_start
def position_objects(at)
# want to have the objects first in the executable
sorted = objects.values.sort{|left,right| left.class.name <=> right.class.name}
sorted.each do | objekt|
2018-03-29 17:03:21 +02:00
next if objekt.is_a?( Parfait::BinaryCode) or objekt.is_a?( Risc::Label )
2018-05-05 18:32:01 +02:00
before = at
Position.set(objekt,at)
2018-03-29 17:03:21 +02:00
at += objekt.padded_length
2018-05-05 18:32:01 +02:00
log.debug "Object #{objekt.class}:#{before.to_s(16)} len: #{(at - before).to_s(16)}"
end
at
end
2018-03-29 17:03:21 +02:00
# Position all BinaryCode.
#
# 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
#
# start at @code_start. The method is called until
# assembly stops throwing errors
def position_code
at = @code_start
first_method = Parfait.object_space.types.values.first.methods
before = at
Position.set( first_method.binary , at , first_method)
Position.set( first_method.cpu_instructions, at + Parfait::BinaryCode.byte_offset , first_method.binary)
log.debug "Method #{first_method.name}:#{before.to_s(16)} len: #{(at - before).to_s(16)}"
log.debug "Instructions #{first_method.cpu_instructions.object_id.to_s(16)}:#{(before+Parfait::BinaryCode.byte_offset).to_s(16)}"
at
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
#
# current (poor) design throws an exception when the assembly can't fit
# constant loads into one instruction.
#
def create_binary
objects.each do |id , method|
next unless method.is_a? Parfait::TypedMethod
writer = BinaryWriter.new(method.binary)
writer.assemble(method.cpu_instructions)
end
log.debug "BinaryInit #{cpu_init.object_id.to_s(16)}"
end
2015-06-12 17:52:06 +02:00
def boot
initialize
Position.clear_positions
@objects = nil
@translated = false
2015-06-12 17:52:06 +02:00
boot_parfait!
@booted = true
self
2015-06-12 17:52:06 +02:00
end
2014-06-25 01:33:44 +02:00
end
2015-06-12 17:52:06 +02:00
# Module function to retrieve singleton
def self.machine
unless defined?(@machine)
@machine = Machine.new
end
@machine
end
2015-06-12 17:52:06 +02:00
end
require_relative "boot"