move the if logic around (execute false first) and fix the parameters passing
This commit is contained in:
@ -16,6 +16,7 @@ module Ast
|
||||
call.load_args into
|
||||
call.do_call into
|
||||
current_function.restore_locals(context , into) if current_function
|
||||
puts "compile call #{function.return_type}"
|
||||
function.return_type
|
||||
end
|
||||
|
||||
|
@ -12,26 +12,28 @@ module Ast
|
||||
[:cond, :if_true, :if_false]
|
||||
end
|
||||
def compile context , into
|
||||
false_block = into.new_block "#{into.name}_else"
|
||||
merge_block = false_block.new_block "#{into.name}_if_merge"
|
||||
# to execute the logic as the if states it, the blocks are the other way around
|
||||
# so we can the jump over the else if true ,and the else joins unconditionally after the true_block
|
||||
false_block = into.new_block "#{into.name}_if_false"
|
||||
true_block = false_block.new_block "#{into.name}_if_true"
|
||||
merge_block = true_block.new_block "#{into.name}_if_merge"
|
||||
|
||||
cond_val = cond.compile(context , into)
|
||||
puts "compiled if condition #{cond_val.inspect}"
|
||||
into.b false_block , condition_code: cond_val.not_operator
|
||||
into.b true_block , condition_code: cond_val.operator
|
||||
|
||||
last = nil
|
||||
if_true.each do |part|
|
||||
last = part.compile(context , into )
|
||||
puts "compiled in if true #{last.inspect}"
|
||||
end
|
||||
|
||||
into.b merge_block
|
||||
|
||||
if_false.each do |part|
|
||||
last = part.compile(context , false_block )
|
||||
puts "compiled in if false #{last.inspect}"
|
||||
end
|
||||
|
||||
false_block.b merge_block
|
||||
|
||||
last = nil
|
||||
if_true.each do |part|
|
||||
last = part.compile(context , true_block )
|
||||
puts "compiled in if true #{last.inspect}"
|
||||
end
|
||||
|
||||
puts "compile if end"
|
||||
into.insert_at merge_block
|
||||
|
||||
|
@ -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) if r_val.is_a?(Vm::Value) and l_val.register != r_val.register
|
||||
l_val = l_val.move( into , r_val)
|
||||
else
|
||||
l_val = context.function.new_local.move( into , r_val )
|
||||
end
|
||||
|
@ -16,7 +16,7 @@ module Vm
|
||||
if arg.is_a?(IntegerConstant) or arg.is_a?(StringConstant)
|
||||
function.args[index].load into , arg
|
||||
else
|
||||
function.args[index].move( into, arg ) if arg.register != args[index].register
|
||||
function.args[index].move( into, arg ) if arg.register != function.args[index].register
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user