working on registers, awip (a work in progress)

This commit is contained in:
Torsten Ruger
2014-05-25 08:43:07 +03:00
parent 1e326e00b9
commit 6ac92cac3a
9 changed files with 80 additions and 40 deletions

View File

@ -35,12 +35,21 @@ module Ast
context.function = function
into = function.body
last_compiled = nil
body.each do |b|
compiled = b.compile(context , into)
function.return_type = compiled
puts "compiled in function #{compiled.inspect}"
raise "alarm #{compiled} \n #{b}" unless compiled.is_a? Vm::Word
last_compiled = b.compile(context , into)
puts "compiled in function #{last_compiled.inspect}"
raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Vm::Word
end
return_reg = Vm::Integer.new(0)
if last_compiled.is_a?(Vm::IntegerConstant) or last_compiled.is_a?(Vm::StringConstant)
return_reg.load into , last_compiled if last_compiled.register != return_reg.register
else
return_reg.move( into, last_compiled ) if last_compiled.register != return_reg.register
end
function.set_return return_reg
context.locals = parent_locals
context.function = parent_function
function

View File

@ -23,7 +23,7 @@ module Ast
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
l_val = context.locals[left.name]
if( l_val ) #variable existed, move data there
l_val = l_val.move( into , r_val)
l_val = l_val.move( into , r_val) if r_val.is_a?(Vm::Value) and l_val.register != r_val.register
else
l_val = context.function.new_local.move( into , r_val )
end
@ -39,10 +39,11 @@ module Ast
code = l_val.greater_than into , r_val
when "<"
code = l_val.less_than into , r_val
when "=="
code = l_val.equals into , r_val
when "+"
res = context.function.new_local
into.res = l_val + r_val
# code = res.plus into , l_val , r_val
code = res
when "-"
res = context.function.new_local