remove passes and achieve the same by translating

This commit is contained in:
Torsten Ruger 2015-10-24 11:42:36 +03:00
parent 57f37ec023
commit a871f96630
5 changed files with 138 additions and 88 deletions

88
lib/arm/translator.rb Normal file
View 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

View File

@ -30,6 +30,13 @@ module Register
end end
alias :<< :set_next 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 ) # get the next instruction (without arg given )
# when given an interger, advance along the line that many time and return. # when given an interger, advance along the line that many time and return.
def next( amount = 1) def next( amount = 1)
@ -43,6 +50,19 @@ module Register
@next = instruction @next = instruction
end 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 = [] def length labels = []
ret = 1 ret = 1
ret += self.next.length( labels ) if self.next ret += self.next.length( labels ) if self.next

View File

@ -1,91 +1,56 @@
require 'parslet/convenience' require 'parslet/convenience'
require_relative "collector" require_relative "collector"
module Register 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 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 # 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. # 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 class Machine
include Collector include Collector
def initialize def initialize
@parser = Parser::Salama.new @parser = Parser::Salama.new
@passes = [ ]
@objects = {} @objects = {}
@booted = false @booted = false
end 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 # idea being that later method missing could catch translate_xxx and translate to target xxx
# runs housekeeping Minimizer and Collector # now we just instantiate ArmTranslater and pass instructions
# Has to be called before run_after def translate_arm
def run_before stop_at translator = Arm::Translator.new
@blocks = [@init] methods = []
@space.classes.values.each do |c| @space.classes.values.each do |c|
c.instance_methods.each do |f| c.instance_methods.each do |f|
nb = f.source.blocks methods << f.source
@blocks += nb
end end
end end
@passes.each do |pass_class| methods.each do |method|
#puts "running #{pass_class}" instruction = method.instructions
run_blocks_for pass_class begin
return if stop_at == pass_class 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
end end
# run all passes after the pass given # translator should translate from register instructio set to it's own (arm eg)
# run_before MUST be called first. # for each instruction we call the translator with translate_XXX
# the two are meant as a poor mans breakoint # with XXX being the class name.
def run_after start_at # the result is replaced in the stream
run = false def translate translator , instruction
@passes.each do |pass_class| class_name = instruction.class.name.split("::").last
if run translator.send( "translate_#{class_name}".to_sym , instruction)
#puts "running #{pass_class}"
run_blocks_for pass_class
else
run = true if start_at == pass_class
end
end
end 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 # Objects are data and get assembled after functions
def add_object o def add_object o
@ -96,25 +61,6 @@ module Register
true true
end 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 def boot
boot_parfait! boot_parfait!
@init = Branch.new( "__init__" , self.space.get_init.source.instructions ) @init = Branch.new( "__init__" , self.space.get_init.source.instructions )

View File

@ -13,3 +13,4 @@ require "salama-object-file"
require "register" require "register"
require "register/builtin/object" require "register/builtin/object"
require "arm/arm_machine" require "arm/arm_machine"
require "arm/translator"

View File

@ -4,19 +4,14 @@ class HelloTest < MiniTest::Test
def check def check
machine = Register.machine.boot machine = Register.machine.boot
#TODO remove this hack: write proper aliases machine.parse_and_compile @string_input
statements = machine.parse_and_compile @string_input
output_at = "Register::CallImplementation"
#{}"Register::CallImplementation"
machine.collect machine.collect
machine.run_before output_at machine.translate_arm
#puts Sof.write(machine.space)
machine.run_after output_at
writer = Elf::ObjectWriter.new(machine) writer = Elf::ObjectWriter.new(machine)
writer.save "hello.o" writer.save "hello.o"
end end
def pest_string_put def test_string_put
@string_input = <<HERE @string_input = <<HERE
class Object class Object
int main() int main()