rubyx/lib/risc/machine.rb

157 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 Logging
2016-12-31 14:08:32 +01:00
log_level :info
def initialize
@booted = false
2015-10-28 12:00:23 +01:00
@constants = []
end
attr_reader :constants , :cpu_init , :binary_init
attr_reader :booted , :translated
# translate to arm, ie instantiate an arm translator and pass it to translate
#
# currently we have no machanism to translate to other cpu's (nor such translators)
# but the mechanism is ready
def translate_arm
@translated = true
translate(Arm::Translator.new)
end
2018-03-29 17:03:21 +02:00
# translate code to whatever cpu the translator translates to
# this means translating the initial jump (cpu_init into binary_init)
# and then translating all methods
def translate( translator )
methods = Parfait.object_space.get_all_methods
translate_methods( methods , translator )
@cpu_init = risc_init.to_cpu(translator)
2018-03-29 16:39:31 +02:00
@binary_init = Parfait::BinaryCode.new(1)
end
2018-03-29 17:03:21 +02:00
# go through all methods and translate them to cpu, given the translator
def translate_methods(methods , translator)
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 compilatio 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 object
# - all code
# As code length amy change during assembly, this way at least the objects stay
# in place and we don't have to deal with chaning loading code
def position_all
translate_arm unless @translated
#need the initial jump at 0 and then functions
cpu_init.set_position( 0 )
#Positioned.set_position(cpu_init.first , 0)
2018-03-29 16:39:31 +02:00
Positioned.set_position(binary_init,0)
at = position_objects( binary_init.padded_length )
# and then everything code
position_code_from( at )
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
def position_objects( at )
# want to have the objects first in the executable
objects.each do | id , objekt|
2018-03-29 17:03:21 +02:00
next if objekt.is_a?( Parfait::BinaryCode) or objekt.is_a?( Risc::Label )
Positioned.set_position(objekt,at)
at += objekt.padded_length
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 debuggin)
# we go through methods, and then through all codes from the method
def position_code_from( at )
objects.each do |id , method|
next unless method.is_a? Parfait::TypedMethod
log.debug "POS1 #{method.name}:#{at.to_s(16)}"
2018-03-29 17:03:21 +02:00
method.cpu_instructions.set_position( at )
before = at
nekst = method.binary
while(nekst)
Positioned.set_position(nekst , at)
at += nekst.padded_length
nekst = nekst.next
end
log.debug "POS2 #{method.name}:#{at.to_s(16)} len: #{(at - before).to_s(16)}"
end
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
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
puts "CPU init"
puts cpu_init.first.inspect[0...130]
puts Parfait.object_space.get_init.risc_instructions.inspect[0...130]
puts Parfait.object_space.get_init.cpu_instructions.inspect[0...130]
2018-03-29 16:39:31 +02:00
BinaryWriter.new(binary_init).assemble(cpu_init)
end
2015-06-12 17:52:06 +02:00
def boot
initialize
@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"