checkpointing on the mad road to register allocation

This commit is contained in:
Torsten Ruger
2014-05-13 18:21:24 +03:00
parent b0302948dd
commit f6711ea49c
12 changed files with 86 additions and 76 deletions

View File

@ -14,7 +14,7 @@ module Ast
value.to_s
end
def compile context , into
Vm::Integer.new value
Vm::IntegerConstant.new value
end
def attributes
[:value]

View File

@ -18,16 +18,20 @@ module Ast
end
def compile context , into
raise "function does not compile into anything #{self}" if into
parent_locals = context.locals
context.locals = {}
args = []
locals = {}
params.each_with_index do |param , index|
arg = param.name
arg_value = Vm::Integer.new(index)
context.locals[arg] = arg_value
locals[arg] = arg_value
args << arg_value
end
function = Vm::Function.new(name , args )
parent_locals = context.locals
parent_function = context.function
context.locals = locals
context.function = function
context.program.add_function function
into = function.entry
block.each do |b|
@ -41,6 +45,7 @@ module Ast
puts compiled.inspect
end
context.locals = parent_locals
context.function = parent_function
function
end

View File

@ -43,10 +43,15 @@ module Ast
"#{left} #{operator} #{right}"
end
def compile context , into
puts "compile #{to_s}"
r_val = right.compile(context , into)
if operator == "=" # assignemnt
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
if r_val.is_a? Vm::IntegerConstant
puts context.attributes.keys.join(" ")
next_register = context.function.next_register
r_val = Vm::Integer.new(next_register).load( into , r_val )
end
context.locals[left.name] = r_val
return r_val
end
@ -54,9 +59,9 @@ module Ast
case operator
when ">"
code = l_val.less_or_equal r_val
code = l_val.less_or_equal into , r_val
when "+"
code = l_val.plus r_val
code = l_val.plus into , r_val
else
raise "unimplemented operator #{operator} #{self}"
end