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
|
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
|
|
|
|
2014-06-25 15:00:24 +02:00
|
|
|
def initialize
|
2015-06-01 07:40:17 +02:00
|
|
|
@booted = false
|
2018-05-01 21:36:46 +02:00
|
|
|
@risc_init = nil
|
2015-10-28 12:00:23 +01:00
|
|
|
@constants = []
|
2018-05-30 22:49:01 +02:00
|
|
|
@next_address = nil
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2018-05-12 17:36:59 +02:00
|
|
|
attr_reader :constants , :cpu_init
|
2018-03-27 17:47:39 +02:00
|
|
|
attr_reader :booted , :translated
|
2018-05-12 17:36:59 +02:00
|
|
|
attr_reader :platform
|
2015-10-24 10:42:36 +02:00
|
|
|
|
2018-05-16 20:00:14 +02:00
|
|
|
# Translate code to whatever cpu is specified.
|
|
|
|
# Currently only :arm and :interpret
|
2018-03-26 18:42:40 +02:00
|
|
|
#
|
2018-05-16 20:00:14 +02:00
|
|
|
# Translating means translating the initial jump
|
2018-03-29 17:03:21 +02:00
|
|
|
# and then translating all methods
|
2018-05-16 20:00:14 +02:00
|
|
|
def translate( platform )
|
|
|
|
platform = platform.to_s.capitalize
|
|
|
|
@platform = Platform.for(platform)
|
|
|
|
@translated = true
|
2018-05-24 18:20:06 +02:00
|
|
|
translate_methods( @platform.translator )
|
2018-05-16 20:00:14 +02:00
|
|
|
@cpu_init = risc_init.to_cpu(@platform.translator)
|
2016-12-15 18:20:54 +01:00
|
|
|
end
|
|
|
|
|
2018-03-29 17:03:21 +02:00
|
|
|
# go through all methods and translate them to cpu, given the translator
|
2018-05-24 18:20:06 +02:00
|
|
|
def translate_methods(translator)
|
|
|
|
Parfait.object_space.get_all_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
|
|
|
|
|
2018-05-01 18:19:04 +02:00
|
|
|
# lazy init risc_init
|
|
|
|
def risc_init
|
|
|
|
@risc_init ||= Branch.new( "__initial_branch__" , Parfait.object_space.get_init.risc_instructions )
|
|
|
|
end
|
2018-05-30 09:29:38 +02:00
|
|
|
# add a constant (which get created during compilation and need to be linked)
|
2018-03-31 12:25:59 +02:00
|
|
|
def add_constant(const)
|
2018-04-30 12:28:55 +02:00
|
|
|
raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object)
|
2018-03-31 12:25:59 +02:00
|
|
|
@constants << const
|
|
|
|
end
|
2018-05-16 20:00:14 +02:00
|
|
|
|
2018-05-30 22:49:01 +02:00
|
|
|
# hand out a return address for use as constant the address is added
|
|
|
|
def get_address
|
|
|
|
10.times do # 10 for whole pages
|
|
|
|
@next_address = Parfait::ReturnAddress.new(0,@next_address)
|
|
|
|
add_constant( @next_address )
|
|
|
|
end unless @next_address
|
|
|
|
addr = @next_address
|
|
|
|
@next_address = @next_address.next_integer
|
|
|
|
addr
|
|
|
|
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
|
2018-05-16 20:00:14 +02:00
|
|
|
raise "Not translated " unless @translated
|
2018-03-27 17:47:39 +02:00
|
|
|
#need the initial jump at 0 and then functions
|
2018-06-02 22:02:59 +02:00
|
|
|
Position.new(cpu_init , 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-05-29 19:52:58 +02:00
|
|
|
sorted = objects.values.sort{|left,right| left.class.name <=> right.class.name}
|
2018-05-31 19:41:04 +02:00
|
|
|
previous = nil
|
2018-05-29 19:52:58 +02:00
|
|
|
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
|
2018-06-02 22:02:59 +02:00
|
|
|
position = Position.new(objekt , at)
|
|
|
|
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-05-05 18:32:01 +02:00
|
|
|
log.debug "Object #{objekt.class}:#{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-06-02 16:29:38 +02:00
|
|
|
Parfait.object_space.types.values.each do |type|
|
|
|
|
next unless type.methods
|
|
|
|
type.methods.each_method do |method|
|
2018-06-09 07:10:41 +02:00
|
|
|
Position.log.debug "Position starts for method #{method.name}"
|
2018-06-07 18:26:02 +02:00
|
|
|
last_code = CodeListener.init(method.binary)
|
|
|
|
last_code.set(code_start)
|
2018-06-02 22:48:12 +02:00
|
|
|
first_position = InstructionListener.init(method.cpu_instructions, method.binary)
|
2018-06-05 17:11:25 +02:00
|
|
|
first_position.set( code_start + Parfait::BinaryCode.byte_offset)
|
2018-06-02 20:20:15 +02:00
|
|
|
code_start = last_code.next_slot
|
2018-06-02 16:29:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
#Position.set( first_method.cpu_instructions, code_start + Parfait::BinaryCode.byte_offset , first_method.binary)
|
|
|
|
#log.debug "Method #{first_method.name}:#{before.to_s(16)} len: #{(code_start - before).to_s(16)}"
|
|
|
|
#log.debug "Instructions #{first_method.cpu_instructions.object_id.to_s(16)}:#{(before+Parfait::BinaryCode.byte_offset).to_s(16)}"
|
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
|
|
|
#
|
|
|
|
# current (poor) design throws an exception when the assembly can't fit
|
|
|
|
# constant loads into one instruction.
|
|
|
|
#
|
2018-03-27 19:47:41 +02:00
|
|
|
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
|
2018-05-17 08:31:36 +02:00
|
|
|
log.debug "BinaryInit #{cpu_init.object_id.to_s(16)}"
|
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-05-24 13:27:53 +02:00
|
|
|
Position.clear_positions
|
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!
|
|
|
|
@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"
|