trying to get function calls to work (but will have to rework regeister handling)
This commit is contained in:
parent
7d69132d90
commit
79a28ac5fa
@ -31,6 +31,7 @@ module Arm
|
||||
end
|
||||
|
||||
def word_load value , reg
|
||||
raise "nnn " unless reg.class == Symbol
|
||||
mov( :left => reg , :right => value )
|
||||
end
|
||||
def string_load str_lit , reg
|
||||
|
@ -21,6 +21,9 @@ module Arm
|
||||
if( arg.is_a? Fixnum ) #HACK to not have to change the code just now
|
||||
arg = Arm::NumLiteral.new( arg )
|
||||
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.value.fits_u8?)
|
||||
# no shifting needed
|
||||
@ -57,7 +60,6 @@ module Arm
|
||||
elsif (arg.type == 'rrx')
|
||||
shift_imm = 0
|
||||
end
|
||||
|
||||
@operand = rm_ref | (shift_op << 4) | (shift_imm << 4+3)
|
||||
else
|
||||
raise "invalid operand argument #{arg.inspect} , #{inspect}"
|
||||
@ -68,6 +70,7 @@ module Arm
|
||||
build
|
||||
instuction_class = 0b00 # OPC_DATA_PROCESSING
|
||||
val = @operand.is_a?(Symbol) ? reg_code(@operand) : @operand
|
||||
raise inspect unless reg_code(@rd)
|
||||
val |= (reg_code(@rd) << 12)
|
||||
val |= (reg_code(@rn) << 12+4)
|
||||
val |= (@update_status_flag << 12+4+4)#20
|
||||
|
@ -2,7 +2,7 @@ module Ast
|
||||
class FunctionExpression < Expression
|
||||
attr_reader :name, :params, :block
|
||||
def initialize name, params, block
|
||||
@name, @params, @block = name, params, block
|
||||
@name, @params, @block = name.to_sym, params, block
|
||||
end
|
||||
def attributes
|
||||
[:name, :params, :block]
|
||||
@ -16,6 +16,7 @@ module Ast
|
||||
def compile context
|
||||
args = params.collect{|p| Vm::Value.type p.name }
|
||||
function = Vm::Function.new(name ,args )
|
||||
context.program.add_function function
|
||||
parent_locals = context.locals
|
||||
context.locals = {}
|
||||
block.each do |b|
|
||||
|
@ -56,6 +56,7 @@ module Vm
|
||||
|
||||
def assemble io
|
||||
@entry.assemble(io)
|
||||
raise @body.inspect
|
||||
@body.assemble(io)
|
||||
@exit.assemble(io)
|
||||
end
|
||||
|
@ -14,14 +14,14 @@ module Vm
|
||||
def assign_function context
|
||||
@function = context.program.get_function @name
|
||||
if @function
|
||||
raise "error #{self}" unless @function.arity != args.length
|
||||
raise "error #{self} , #{@function.args.length} != #{args.length}" if @function.arity != args.length
|
||||
else
|
||||
@function = context.program.get_or_create_function @name
|
||||
end
|
||||
end
|
||||
def load_args
|
||||
args.each_with_index do |arg , index|
|
||||
add_code arg.load(index)
|
||||
add_code arg.load("r#{index}".to_sym)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -43,14 +43,21 @@ module Vm
|
||||
@objects << o # TODO check type , no basic values allowed (must be wrapped)
|
||||
end
|
||||
|
||||
def add_function function
|
||||
raise "not a function #{function}" unless function.is_a? Function
|
||||
@functions << function
|
||||
end
|
||||
|
||||
def get_function name
|
||||
@functions.detect{ |f| (f.name == name) && (f.class == Function) }
|
||||
name = name.to_sym
|
||||
@functions.detect{ |f| (f.name == name) }
|
||||
end
|
||||
|
||||
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
|
||||
def get_or_create_function name
|
||||
fun = get_function name
|
||||
unless fun
|
||||
puts @functions.inspect
|
||||
fun = Function.new(name)
|
||||
block = Core::Kernel.send(name)
|
||||
fun.set_body block
|
||||
|
@ -24,6 +24,8 @@ module Vm
|
||||
def initialize value
|
||||
@value = value
|
||||
end
|
||||
attr_reader :value
|
||||
|
||||
#naming convention to infer types in kernel functions. Kernel types are basic types, ie see below
|
||||
#
|
||||
def self.type name
|
||||
@ -41,6 +43,9 @@ module Vm
|
||||
def load reg
|
||||
Machine.instance.word_load self , reg
|
||||
end
|
||||
def length
|
||||
4
|
||||
end
|
||||
end
|
||||
|
||||
class Unsigned < Word
|
||||
|
@ -26,7 +26,7 @@ class TestRunner < MiniTest::Test
|
||||
# file is a list of expressions, al but the last must be a function
|
||||
# and the last is wrapped as a main
|
||||
parts.each_with_index do |part,index|
|
||||
puts "parsing #{index}=#{part}"
|
||||
puts "parsing #{index}=#{part.inspect}"
|
||||
expr = part.compile( program.context )
|
||||
if index = parts.length
|
||||
program.main = expr
|
||||
|
Loading…
Reference in New Issue
Block a user