trying to get function calls to work (but will have to rework regeister handling)

This commit is contained in:
Torsten Ruger 2014-05-10 15:24:56 +03:00
parent 7d69132d90
commit 79a28ac5fa
9 changed files with 25 additions and 7 deletions

View File

@ -31,6 +31,7 @@ module Arm
end end
def word_load value , reg def word_load value , reg
raise "nnn " unless reg.class == Symbol
mov( :left => reg , :right => value ) mov( :left => reg , :right => value )
end end
def string_load str_lit , reg def string_load str_lit , reg

View File

@ -21,6 +21,9 @@ module Arm
if( arg.is_a? Fixnum ) #HACK to not have to change the code just now if( arg.is_a? Fixnum ) #HACK to not have to change the code just now
arg = Arm::NumLiteral.new( arg ) arg = Arm::NumLiteral.new( arg )
end end
if( arg.is_a? Vm::Signed ) #HACK to not have to change the code just now
arg = Arm::NumLiteral.new( arg.value )
end
if (arg.is_a?(Arm::NumLiteral)) if (arg.is_a?(Arm::NumLiteral))
if (arg.value.fits_u8?) if (arg.value.fits_u8?)
# no shifting needed # no shifting needed
@ -57,7 +60,6 @@ module Arm
elsif (arg.type == 'rrx') elsif (arg.type == 'rrx')
shift_imm = 0 shift_imm = 0
end end
@operand = rm_ref | (shift_op << 4) | (shift_imm << 4+3) @operand = rm_ref | (shift_op << 4) | (shift_imm << 4+3)
else else
raise "invalid operand argument #{arg.inspect} , #{inspect}" raise "invalid operand argument #{arg.inspect} , #{inspect}"
@ -68,6 +70,7 @@ module Arm
build build
instuction_class = 0b00 # OPC_DATA_PROCESSING instuction_class = 0b00 # OPC_DATA_PROCESSING
val = @operand.is_a?(Symbol) ? reg_code(@operand) : @operand val = @operand.is_a?(Symbol) ? reg_code(@operand) : @operand
raise inspect unless reg_code(@rd)
val |= (reg_code(@rd) << 12) val |= (reg_code(@rd) << 12)
val |= (reg_code(@rn) << 12+4) val |= (reg_code(@rn) << 12+4)
val |= (@update_status_flag << 12+4+4)#20 val |= (@update_status_flag << 12+4+4)#20

View File

@ -2,7 +2,7 @@ module Ast
class FunctionExpression < Expression class FunctionExpression < Expression
attr_reader :name, :params, :block attr_reader :name, :params, :block
def initialize name, params, block def initialize name, params, block
@name, @params, @block = name, params, block @name, @params, @block = name.to_sym, params, block
end end
def attributes def attributes
[:name, :params, :block] [:name, :params, :block]
@ -16,6 +16,7 @@ module Ast
def compile context def compile context
args = params.collect{|p| Vm::Value.type p.name } args = params.collect{|p| Vm::Value.type p.name }
function = Vm::Function.new(name ,args ) function = Vm::Function.new(name ,args )
context.program.add_function function
parent_locals = context.locals parent_locals = context.locals
context.locals = {} context.locals = {}
block.each do |b| block.each do |b|

View File

@ -56,6 +56,7 @@ module Vm
def assemble io def assemble io
@entry.assemble(io) @entry.assemble(io)
raise @body.inspect
@body.assemble(io) @body.assemble(io)
@exit.assemble(io) @exit.assemble(io)
end end

View File

@ -14,14 +14,14 @@ module Vm
def assign_function context def assign_function context
@function = context.program.get_function @name @function = context.program.get_function @name
if @function if @function
raise "error #{self}" unless @function.arity != args.length raise "error #{self} , #{@function.args.length} != #{args.length}" if @function.arity != args.length
else else
@function = context.program.get_or_create_function @name @function = context.program.get_or_create_function @name
end end
end end
def load_args def load_args
args.each_with_index do |arg , index| args.each_with_index do |arg , index|
add_code arg.load(index) add_code arg.load("r#{index}".to_sym)
end end
end end

View File

@ -43,14 +43,21 @@ module Vm
@objects << o # TODO check type , no basic values allowed (must be wrapped) @objects << o # TODO check type , no basic values allowed (must be wrapped)
end end
def add_function function
raise "not a function #{function}" unless function.is_a? Function
@functions << function
end
def get_function name def get_function name
@functions.detect{ |f| (f.name == name) && (f.class == Function) } name = name.to_sym
@functions.detect{ |f| (f.name == name) }
end end
# preferred way of creating new functions (also forward declarations, will flag unresolved later) # preferred way of creating new functions (also forward declarations, will flag unresolved later)
def get_or_create_function name def get_or_create_function name
fun = get_function name fun = get_function name
unless fun unless fun
puts @functions.inspect
fun = Function.new(name) fun = Function.new(name)
block = Core::Kernel.send(name) block = Core::Kernel.send(name)
fun.set_body block fun.set_body block

View File

@ -24,6 +24,8 @@ module Vm
def initialize value def initialize value
@value = value @value = value
end end
attr_reader :value
#naming convention to infer types in kernel functions. Kernel types are basic types, ie see below #naming convention to infer types in kernel functions. Kernel types are basic types, ie see below
# #
def self.type name def self.type name
@ -41,6 +43,9 @@ module Vm
def load reg def load reg
Machine.instance.word_load self , reg Machine.instance.word_load self , reg
end end
def length
4
end
end end
class Unsigned < Word class Unsigned < Word

View File

@ -26,7 +26,7 @@ class TestRunner < MiniTest::Test
# file is a list of expressions, al but the last must be a function # file is a list of expressions, al but the last must be a function
# and the last is wrapped as a main # and the last is wrapped as a main
parts.each_with_index do |part,index| parts.each_with_index do |part,index|
puts "parsing #{index}=#{part}" puts "parsing #{index}=#{part.inspect}"
expr = part.compile( program.context ) expr = part.compile( program.context )
if index = parts.length if index = parts.length
program.main = expr program.main = expr