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
|
|
|
|
# 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)
|
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
|
|
|
|
2014-06-25 15:00:24 +02:00
|
|
|
class Machine
|
2016-12-29 20:24:11 +01:00
|
|
|
include Logging
|
2016-12-31 14:08:32 +01:00
|
|
|
log_level :info
|
2015-07-18 10:53:04 +02:00
|
|
|
|
2014-06-25 15:00:24 +02:00
|
|
|
def initialize
|
2015-06-01 07:40:17 +02:00
|
|
|
@booted = false
|
2015-10-28 12:00:23 +01:00
|
|
|
@constants = []
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2018-03-29 16:39:31 +02:00
|
|
|
attr_reader :constants , :risc_init , :cpu_init , :binary_init
|
2018-03-27 17:47:39 +02:00
|
|
|
attr_reader :booted , :translated
|
2015-10-24 10:42:36 +02:00
|
|
|
|
2018-03-26 18:42:40 +02:00
|
|
|
# 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
|
2015-10-24 10:42:36 +02:00
|
|
|
def translate_arm
|
2018-03-27 17:47:39 +02:00
|
|
|
@translated = true
|
2018-03-26 18:42:40 +02:00
|
|
|
translate(Arm::Translator.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
# translate the methods to whatever cpu the translator translates to
|
|
|
|
def translate( translator )
|
2016-12-30 13:10:49 +01:00
|
|
|
methods = Parfait.object_space.collect_methods
|
2018-03-26 18:42:40 +02:00
|
|
|
translate_methods( methods , translator )
|
|
|
|
@cpu_init = translator.translate( @risc_init )
|
2018-03-29 16:39:31 +02:00
|
|
|
@binary_init = Parfait::BinaryCode.new(1)
|
2016-12-15 18:20:54 +01:00
|
|
|
end
|
|
|
|
|
2018-03-26 18:42:40 +02:00
|
|
|
def translate_methods(methods , translator)
|
2015-10-24 10:42:36 +02:00
|
|
|
methods.each do |method|
|
2016-12-31 13:54:15 +01:00
|
|
|
log.debug "Translate method #{method.name}"
|
2018-03-25 18:36:00 +02:00
|
|
|
method.translate_cpu(translator)
|
2015-06-12 17:52:06 +02:00
|
|
|
end
|
2015-05-12 14:36:44 +02:00
|
|
|
end
|
|
|
|
|
2018-03-27 17:47:39 +02:00
|
|
|
# machine keeps a list of all objects. this is lazily created with a collector
|
|
|
|
def objects
|
|
|
|
@objects ||= Collector.collect_space
|
|
|
|
end
|
|
|
|
|
|
|
|
def position_all
|
|
|
|
translate_arm unless @translated
|
|
|
|
#need the initial jump at 0 and then functions
|
|
|
|
cpu_init.set_position( 0 )
|
2018-03-29 16:39:31 +02:00
|
|
|
Positioned.set_position(cpu_init.first , 0)
|
|
|
|
Positioned.set_position(binary_init,0)
|
|
|
|
at = position_objects( binary_init.padded_length )
|
2018-03-27 17:47:39 +02:00
|
|
|
# and then everything code
|
|
|
|
position_code_from( at )
|
|
|
|
end
|
|
|
|
|
|
|
|
def position_objects( at )
|
|
|
|
at += 8 # thats the padding
|
|
|
|
# want to have the objects first in the executable
|
|
|
|
objects.each do | id , objekt|
|
|
|
|
case objekt
|
|
|
|
when Parfait::BinaryCode
|
|
|
|
when Risc::Label
|
|
|
|
else
|
|
|
|
Positioned.set_position(objekt,at)
|
|
|
|
at += objekt.padded_length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
at
|
|
|
|
end
|
|
|
|
|
|
|
|
def position_code_from( at )
|
|
|
|
objects.each do |id , method|
|
|
|
|
next unless method.is_a? Parfait::TypedMethod
|
|
|
|
log.debug "CODE1 #{method.name}:#{at}"
|
|
|
|
method.cpu_instructions.set_position( at + 12) # BinaryCode header
|
|
|
|
before = at
|
|
|
|
nekst = method.binary
|
|
|
|
while(nekst)
|
|
|
|
Positioned.set_position(nekst , at)
|
|
|
|
at += nekst.padded_length
|
|
|
|
nekst = nekst.next
|
|
|
|
#puts "LENGTH #{len}"
|
|
|
|
end
|
|
|
|
log.debug "CODE2 #{method.name}:#{at} len: #{at - before}"
|
|
|
|
end
|
|
|
|
at
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:47:41 +02:00
|
|
|
def create_binary
|
|
|
|
objects.each do |id , method|
|
|
|
|
next unless method.is_a? Parfait::TypedMethod
|
2018-03-28 12:04:25 +02:00
|
|
|
#puts "Binary for #{method.name}:#{}"
|
2018-03-27 19:47:41 +02:00
|
|
|
writer = BinaryWriter.new(method.binary)
|
|
|
|
writer.assemble(method.cpu_instructions)
|
|
|
|
end
|
2018-03-29 16:39:31 +02:00
|
|
|
BinaryWriter.new(binary_init).assemble(cpu_init)
|
2018-03-27 19:47:41 +02:00
|
|
|
end
|
|
|
|
|
2015-06-12 17:52:06 +02:00
|
|
|
def boot
|
2015-11-09 09:04:37 +01:00
|
|
|
initialize
|
2018-03-27 17:47:39 +02:00
|
|
|
@objects = nil
|
2018-03-27 18:06:16 +02:00
|
|
|
@translated = false
|
2015-06-12 17:52:06 +02:00
|
|
|
boot_parfait!
|
2018-03-25 18:36:00 +02:00
|
|
|
@risc_init = Branch.new( "__initial_branch__" , Parfait.object_space.get_init.risc_instructions )
|
2015-06-12 17:52:06 +02:00
|
|
|
@booted = true
|
2015-07-18 10:53:04 +02:00
|
|
|
self
|
2015-06-12 17:52:06 +02:00
|
|
|
end
|
|
|
|
|
2014-06-25 01:33:44 +02:00
|
|
|
end
|
2015-06-01 07:40:17 +02:00
|
|
|
|
2015-06-12 17:52:06 +02:00
|
|
|
# Module function to retrieve singleton
|
2015-06-01 07:40:17 +02:00
|
|
|
def self.machine
|
|
|
|
unless defined?(@machine)
|
|
|
|
@machine = Machine.new
|
|
|
|
end
|
|
|
|
@machine
|
|
|
|
end
|
2015-06-12 17:52:06 +02:00
|
|
|
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-19 19:29:33 +02:00
|
|
|
|
|
|
|
require_relative "boot"
|