moved registers to machine, changed return to 0 (from 7) and erased all integer references to registers

This commit is contained in:
Torsten Ruger 2014-06-10 13:29:01 +03:00
parent d7a60f2803
commit 7ca3207b3e
7 changed files with 45 additions and 29 deletions

View File

@ -9,6 +9,20 @@ require_relative "constants"
module Arm module Arm
class ArmMachine < Vm::RegisterMachine class ArmMachine < Vm::RegisterMachine
# The constants are here for readablility, the code uses access functions below
RETURN_REG = :r0
TYPE_REG = :r1
RECEIVER_REG = :r2
def return_register
RETURN_REG
end
def type_register
TYPE_REG
end
def receiver_register
RECEIVER_REG
end
def integer_equals block , left , right def integer_equals block , left , right
block << cmp( left , right ) block << cmp( left , right )
@ -106,11 +120,11 @@ module Arm
end end
def syscall block , num def syscall block , num
sys_and_ret = Vm::Integer.new( Vm::Function::RETURN_REG ) #small todo, is this actually correct for all (that they return int)
sys_and_ret = Vm::Integer.new( Vm::RegisterMachine.instance.return_register )
block << mov( sys_and_ret , num ) block << mov( sys_and_ret , num )
block << swi( 0 ) block << swi( 0 )
#small todo, is this actually correct for all (that they return int) block << mov( sys_and_ret , return_register ) # syscall returns in r0, more to our return
block << mov( sys_and_ret , :r0 ) # syscall returns in r0, more to our return
#todo should write type into r0 according to syscall #todo should write type into r0 according to syscall
sys_and_ret sys_and_ret
end end

View File

@ -12,10 +12,10 @@ module Arm
@immediate = 0 @immediate = 0
@rn = :r0 # register zero = zero bit pattern @rn = :r0 # register zero = zero bit pattern
raise inspect if to.is_a?(Vm::Value) and # NO-OP -> pass raise inspect if to.is_a?(Vm::Value) and
from.is_a?(Vm::Value) and # from.is_a?(Vm::Value) and
!@attributes[:shift_lsr] and # !@attributes[:shift_lsr] and
to.register_symbol == from.register_symbol # to.register_symbol == from.register_symbol
raise "uups " if @to.register_symbol == :rr1 raise "uups " if @to.register_symbol == :rr1
end end

View File

@ -9,7 +9,7 @@ module Ast
if receiver.is_a?(NameExpression) and (receiver.name == :self) if receiver.is_a?(NameExpression) and (receiver.name == :self)
function = context.current_class.get_or_create_function(name) function = context.current_class.get_or_create_function(name)
value_receiver = Vm::Integer.new(Vm::Function::RECEIVER_REG) value_receiver = Vm::Integer.new(Vm::RegisterMachine.instance.receiver_register)
else else
value_receiver = receiver.compile(context , into) value_receiver = receiver.compile(context , into)
function = context.current_class.get_or_create_function(name) function = context.current_class.get_or_create_function(name)

View File

@ -7,12 +7,13 @@ module Ast
locals = {} locals = {}
params.each_with_index do |param , index| params.each_with_index do |param , index|
arg = param.name arg = param.name
arg_value = Vm::Integer.new(index+2) register = Vm::RegisterUse.new(Vm::RegisterMachine.instance.receiver_register).next_reg_use(index + 1)
arg_value = Vm::Integer.new(register)
locals[arg] = arg_value locals[arg] = arg_value
args << arg_value args << arg_value
end end
# class depends on receiver # class depends on receiver
me = Vm::Integer.new( Vm::Function::RECEIVER_REG ) me = Vm::Integer.new( Vm::RegisterMachine.instance.receiver_register )
if receiver.nil? if receiver.nil?
clazz = context.current_class clazz = context.current_class
else else
@ -36,7 +37,7 @@ module Ast
raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Vm::Word raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Vm::Word
end end
return_reg = Vm::Integer.new(7) return_reg = Vm::Integer.new(Vm::RegisterMachine.instance.return_register)
if last_compiled.is_a?(Vm::IntegerConstant) or last_compiled.is_a?(Vm::ObjectConstant) if last_compiled.is_a?(Vm::IntegerConstant) or last_compiled.is_a?(Vm::ObjectConstant)
return_reg.load into , last_compiled if last_compiled.register_symbol != return_reg.register_symbol return_reg.load into , last_compiled if last_compiled.register_symbol != return_reg.register_symbol
else else

View File

@ -2,11 +2,11 @@ module Ast
class ReturnExpression < Expression class ReturnExpression < Expression
# attr_reader :expression # attr_reader :expression
def compile context , into def compile context , into
puts "compiling return expression #{expression}, now return in 7" puts "compiling return expression #{expression}, now return in return_regsiter"
expression_value = expression.compile(context , into) expression_value = expression.compile(context , into)
# copied from function expression: TODO make function # copied from function expression: TODO make function
return_reg = Vm::Integer.new(7) return_reg = Vm::Integer.new(Vm::RegisterMachine.instance.return_register)
if expression_value.is_a?(Vm::IntegerConstant) or expression_value.is_a?(Vm::ObjectConstant) if expression_value.is_a?(Vm::IntegerConstant) or expression_value.is_a?(Vm::ObjectConstant)
return_reg.load into , expression_value return_reg.load into , expression_value
else else

View File

@ -27,27 +27,24 @@ module Vm
class Function < Code class Function < Code
TYPE_REG = :r0
RECEIVER_REG = :r1
RETURN_REG = :r7
def initialize(name , receiver = Vm::Integer , args = [] , return_type = Vm::Integer) def initialize(name , receiver = Vm::Integer , args = [] , return_type = Vm::Integer)
super() super()
@name = name.to_sym @name = name.to_sym
if receiver.is_a?(Value) if receiver.is_a?(Value)
@receiver = receiver @receiver = receiver
raise "arg in non std register #{arg.inspect}" unless RECEIVER_REG == receiver.register_symbol raise "arg in non std register #{receiver.inspect}" unless RegisterMachine.instance.receiver_register == receiver.register_symbol
else else
@receiver = receiver.new(RECEIVER_REG) @receiver = receiver.new(RegisterMachine.instance.receiver_register)
end end
@args = Array.new(args.length) @args = Array.new(args.length)
args.each_with_index do |arg , i| args.each_with_index do |arg , i|
shouldda = RegisterUse.new(RegisterMachine.instance.receiver_register).next_reg_use(i + 1)
if arg.is_a?(Value) if arg.is_a?(Value)
@args[i] = arg @args[i] = arg
raise "arg #{i}in non std register #{arg.inspect}" unless RECEIVER_REG == arg.used_register.next_reg(-1-i) raise "arg #{i} in non std register #{arg.used_register}, expecting #{shouldda}" unless shouldda == arg.used_register
else else
@args[i] = arg.new(RegisterUse.new(RECEIVER_REG).next_reg(i + 1)) @args[i] = arg.new(shouldda)
end end
end end
set_return return_type set_return return_type
@ -64,9 +61,9 @@ module Vm
def set_return type_or_value def set_return type_or_value
@return_type = type_or_value || Vm::Integer @return_type = type_or_value || Vm::Integer
if @return_type.is_a?(Value) if @return_type.is_a?(Value)
raise "return in non std register #{@return_type.inspect}" unless RETURN_REG == @return_type.register_symbol raise "return in non std register #{@return_type.inspect}" unless RegisterMachine.instance.return_register == @return_type.register_symbol
else else
@return_type = @return_type.new(RETURN_REG) @return_type = @return_type.new(RegisterMachine.instance.return_register)
end end
end end
def arity def arity

View File

@ -63,13 +63,17 @@ module Vm
@symbol = r @symbol = r
end end
#helper methods to calculate with register symbols def == other
def next_reg by = 1 return false if other.nil?
int = @symbol[1,3].to_i return false if other.class != RegisterUse
"r#{int + by}".to_sym symbol == other.symbol
end end
#helper method to calculate with register symbols
def next_reg_use by = 1 def next_reg_use by = 1
RegisterUse.new( next_reg(by) ) int = @symbol[1,3].to_i
sym = "r#{int + by}".to_sym
RegisterUse.new( sym )
end end
end end
@ -152,7 +156,7 @@ module Vm
block.mov( self , right ) #move the value block.mov( self , right ) #move the value
elsif right.is_a? StringConstant elsif right.is_a? StringConstant
block.add( self , right , nil) #move the address, by "adding" to pc, ie pc relative block.add( self , right , nil) #move the address, by "adding" to pc, ie pc relative
block.mov( Integer.new(self.used_register.next_reg) , right.length ) #and the length HACK TODO block.mov( Integer.new(self.used_register.next_reg_use) , right.length ) #and the length HACK TODO
elsif right.is_a?(BootClass) or right.is_a?(MetaClass) elsif right.is_a?(BootClass) or right.is_a?(MetaClass)
block.add( self , right , nil) #move the address, by "adding" to pc, ie pc relative block.add( self , right , nil) #move the address, by "adding" to pc, ie pc relative
else else