remove passes and achieve the same by translating
This commit is contained in:
parent
57f37ec023
commit
a871f96630
88
lib/arm/translator.rb
Normal file
88
lib/arm/translator.rb
Normal file
@ -0,0 +1,88 @@
|
||||
module Arm
|
||||
class Translator
|
||||
|
||||
# don't replace labels
|
||||
def translate_Label code
|
||||
nil
|
||||
end
|
||||
|
||||
# Arm stores the return address in a register (not on the stack)
|
||||
# The register is called link , or lr for short .
|
||||
# Maybe because it provides the "link" back to the caller
|
||||
# the vm defines a register for the location, so we store it there.
|
||||
def translate_SaveReturn code
|
||||
ArmMachine.str( :lr , code.register , 4 * code.index )
|
||||
end
|
||||
|
||||
def translate_GetSlot code
|
||||
# times 4 because arm works in bytes, but vm in words
|
||||
ArmMachine.ldr( code.register , code.array , 4 * code.index )
|
||||
end
|
||||
|
||||
def translate_RegisterTransfer code
|
||||
# Register machine convention is from => to
|
||||
# But arm has the receiver/result as the first
|
||||
ArmMachine.mov( code.to , code.from)
|
||||
end
|
||||
|
||||
def translate_SetSlot code
|
||||
# times 4 because arm works in bytes, but vm in words
|
||||
ArmMachine.str( code.register , code.array , 4 * code.index )
|
||||
end
|
||||
|
||||
def translate_FunctionCall code
|
||||
ArmMachine.call( code.method )
|
||||
end
|
||||
|
||||
def translate_FunctionReturn code
|
||||
ArmMachine.ldr( :pc , code.register , 4 * code.index )
|
||||
end
|
||||
|
||||
def translate_LoadConstant code
|
||||
constant = code.constant
|
||||
if constant.is_a?(Parfait::Object) or constant.is_a? Symbol
|
||||
return ArmMachine.add( code.register , constant )
|
||||
else
|
||||
return ArmMachine.mov( code.register , code.constant )
|
||||
end
|
||||
end
|
||||
|
||||
# This implements branch logic, which is simply assembler branch
|
||||
#
|
||||
# The only target for a call is a Block, so we just need to get the address for the code
|
||||
# and branch to it.
|
||||
def translate_Branch code
|
||||
ArmMachine.b( code.block )
|
||||
end
|
||||
def translate_Syscall code
|
||||
call_codes = { :putstring => 4 , :exit => 1 }
|
||||
int_code = call_codes[code.name]
|
||||
raise "Not implemented syscall, #{code.name}" unless int_code
|
||||
send( code.name , int_code )
|
||||
end
|
||||
|
||||
def putstring int_code
|
||||
codes = ArmMachine.ldr( :r1 , Register.message_reg, 4 * Register.resolve_index(:message , :receiver))
|
||||
codes.append ArmMachine.add( :r1 , :r1 , 8 )
|
||||
codes.append ArmMachine.mov( :r0 , 1 )
|
||||
codes.append ArmMachine.mov( :r2 , 12 ) # String length, obvious TODO
|
||||
syscall(int_code , codes )
|
||||
end
|
||||
|
||||
def exit int_code
|
||||
codes = Register::Label.new(nil , "exit")
|
||||
syscall int_code , codes
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# syscall is always triggered by swi(0)
|
||||
# The actual code (ie the index of the kernel function) is in r7
|
||||
def syscall int_code , codes
|
||||
codes.append ArmMachine.mov( :r7 , int_code )
|
||||
codes.append ArmMachine.swi( 0 )
|
||||
codes
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -30,6 +30,13 @@ module Register
|
||||
end
|
||||
alias :<< :set_next
|
||||
|
||||
# during translation we replace one by one
|
||||
def replace_next nekst
|
||||
old = @next
|
||||
@next = nekst
|
||||
@next.append old.next
|
||||
end
|
||||
|
||||
# get the next instruction (without arg given )
|
||||
# when given an interger, advance along the line that many time and return.
|
||||
def next( amount = 1)
|
||||
@ -43,6 +50,19 @@ module Register
|
||||
@next = instruction
|
||||
end
|
||||
|
||||
# return last set instruction. ie follow the linked list until it stops
|
||||
def last
|
||||
code = self
|
||||
code = code.next while( code.next )
|
||||
return code
|
||||
end
|
||||
|
||||
# set next for the last (see last)
|
||||
# so append the given code to the linked list at the end
|
||||
def append code
|
||||
last.set_next code
|
||||
end
|
||||
|
||||
def length labels = []
|
||||
ret = 1
|
||||
ret += self.next.length( labels ) if self.next
|
||||
|
@ -1,91 +1,56 @@
|
||||
require 'parslet/convenience'
|
||||
require_relative "collector"
|
||||
module Register
|
||||
# The Register Machine is a object based virtual machine in which ruby is implemented.
|
||||
# The Register Machine is a object based virtual machine on which ruby will be implemented.
|
||||
#
|
||||
# It is minimal and realistic and low level
|
||||
# - minimal means that if one thing can be implemented by another, it is left out. This is quite
|
||||
# the opposite from ruby, which has several loops, many redundant if forms and the like.
|
||||
# - realistic means it is easy to implement on a 32 bit machine (arm) and possibly 64 bit.
|
||||
# Memory access,some registers of same size are the underlying hardware. (not ie byte machine)
|
||||
# - low level means it's basic instructions are realively easily implemented in a register machine.
|
||||
# Low level means low level in oo terms though, so basic instruction to implement oo
|
||||
# #
|
||||
|
||||
# The ast is transformed to virtual-machine objects, some of which represent code, some data.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# More concretely, a virtual machine is a sort of oo turing machine, it has a current instruction,
|
||||
# executes the instructions, fetches the next one and so on.
|
||||
# Off course the instructions are not soo simple, but in oo terms quite so.
|
||||
#
|
||||
# The machine is virtual in the sense that it is completely modeled in software,
|
||||
# it's complete state explicitly available (not implicitly by walking stacks or something)
|
||||
|
||||
# The machine has a no register, but objects that represent it's state. There are four
|
||||
# - message : the currently executing message (See Parfait::Message)
|
||||
# - receiver : or self. This is actually an instance of Message, but "hoisted" out
|
||||
# - frame : A pssible frame for temporary data. Also part of the message and "hoisted" out
|
||||
# - next_message: A message object that the current activation wants to send.
|
||||
#
|
||||
# Messages form a linked list (not a stack) and the Space is responsible for storing
|
||||
# and handing out empty messages
|
||||
#
|
||||
# The "machine" is not part of the run-time (Parfait)
|
||||
|
||||
class Machine
|
||||
include Collector
|
||||
|
||||
def initialize
|
||||
@parser = Parser::Salama.new
|
||||
@passes = [ ]
|
||||
@objects = {}
|
||||
@booted = false
|
||||
end
|
||||
attr_reader :passes , :space , :class_mappings , :init , :objects , :booted
|
||||
attr_reader :space , :class_mappings , :init , :objects , :booted
|
||||
|
||||
# run all passes before the pass given
|
||||
# also collect the block to run the passes on and
|
||||
# runs housekeeping Minimizer and Collector
|
||||
# Has to be called before run_after
|
||||
def run_before stop_at
|
||||
@blocks = [@init]
|
||||
|
||||
# idea being that later method missing could catch translate_xxx and translate to target xxx
|
||||
# now we just instantiate ArmTranslater and pass instructions
|
||||
def translate_arm
|
||||
translator = Arm::Translator.new
|
||||
methods = []
|
||||
@space.classes.values.each do |c|
|
||||
c.instance_methods.each do |f|
|
||||
nb = f.source.blocks
|
||||
@blocks += nb
|
||||
methods << f.source
|
||||
end
|
||||
end
|
||||
@passes.each do |pass_class|
|
||||
#puts "running #{pass_class}"
|
||||
run_blocks_for pass_class
|
||||
return if stop_at == pass_class
|
||||
methods.each do |method|
|
||||
instruction = method.instructions
|
||||
begin
|
||||
nekst = instruction.next
|
||||
t = translate(translator , nekst) # returning nil means no replace
|
||||
instruction.replace_next(t) if t
|
||||
instruction = nekst
|
||||
end while instruction.next
|
||||
end
|
||||
end
|
||||
|
||||
# run all passes after the pass given
|
||||
# run_before MUST be called first.
|
||||
# the two are meant as a poor mans breakoint
|
||||
def run_after start_at
|
||||
run = false
|
||||
@passes.each do |pass_class|
|
||||
if run
|
||||
#puts "running #{pass_class}"
|
||||
run_blocks_for pass_class
|
||||
else
|
||||
run = true if start_at == pass_class
|
||||
end
|
||||
end
|
||||
# translator should translate from register instructio set to it's own (arm eg)
|
||||
# for each instruction we call the translator with translate_XXX
|
||||
# with XXX being the class name.
|
||||
# the result is replaced in the stream
|
||||
def translate translator , instruction
|
||||
class_name = instruction.class.name.split("::").last
|
||||
translator.send( "translate_#{class_name}".to_sym , instruction)
|
||||
end
|
||||
|
||||
# as before, run all passes that are registered
|
||||
# (but now finer control with before/after versions)
|
||||
def run_passes
|
||||
return if @passes.empty?
|
||||
run_before @passes.first
|
||||
run_after @passes.first
|
||||
end
|
||||
|
||||
# Objects are data and get assembled after functions
|
||||
def add_object o
|
||||
@ -96,25 +61,6 @@ module Register
|
||||
true
|
||||
end
|
||||
|
||||
# Passes may be added to by anyone who wants
|
||||
# This is intentionally quite flexible, though one sometimes has to watch the order of them
|
||||
# most ordering is achieved by ordering the requires and using add_pass
|
||||
# but more precise control is possible with the _after and _before versions
|
||||
|
||||
def add_pass pass
|
||||
@passes << pass
|
||||
end
|
||||
def add_pass_after( pass , after)
|
||||
index = @passes.index(after)
|
||||
raise "No such pass (#{pass}) to add after: #{after}" unless index
|
||||
@passes.insert(index+1 , pass)
|
||||
end
|
||||
def add_pass_before( pass , after)
|
||||
index = @passes.index(after)
|
||||
raise "No such pass to add after: #{after}" unless index
|
||||
@passes.insert(index , pass)
|
||||
end
|
||||
|
||||
def boot
|
||||
boot_parfait!
|
||||
@init = Branch.new( "__init__" , self.space.get_init.source.instructions )
|
||||
|
@ -13,3 +13,4 @@ require "salama-object-file"
|
||||
require "register"
|
||||
require "register/builtin/object"
|
||||
require "arm/arm_machine"
|
||||
require "arm/translator"
|
||||
|
@ -4,19 +4,14 @@ class HelloTest < MiniTest::Test
|
||||
|
||||
def check
|
||||
machine = Register.machine.boot
|
||||
#TODO remove this hack: write proper aliases
|
||||
statements = machine.parse_and_compile @string_input
|
||||
output_at = "Register::CallImplementation"
|
||||
#{}"Register::CallImplementation"
|
||||
machine.parse_and_compile @string_input
|
||||
machine.collect
|
||||
machine.run_before output_at
|
||||
#puts Sof.write(machine.space)
|
||||
machine.run_after output_at
|
||||
machine.translate_arm
|
||||
writer = Elf::ObjectWriter.new(machine)
|
||||
writer.save "hello.o"
|
||||
end
|
||||
|
||||
def pest_string_put
|
||||
def test_string_put
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
|
Loading…
Reference in New Issue
Block a user