move the if logic around (execute false first) and fix the parameters passing
This commit is contained in:
parent
5a5b016a7e
commit
5afa6f4239
@ -16,6 +16,7 @@ module Ast
|
|||||||
call.load_args into
|
call.load_args into
|
||||||
call.do_call into
|
call.do_call into
|
||||||
current_function.restore_locals(context , into) if current_function
|
current_function.restore_locals(context , into) if current_function
|
||||||
|
puts "compile call #{function.return_type}"
|
||||||
function.return_type
|
function.return_type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,26 +12,28 @@ module Ast
|
|||||||
[:cond, :if_true, :if_false]
|
[:cond, :if_true, :if_false]
|
||||||
end
|
end
|
||||||
def compile context , into
|
def compile context , into
|
||||||
false_block = into.new_block "#{into.name}_else"
|
# to execute the logic as the if states it, the blocks are the other way around
|
||||||
merge_block = false_block.new_block "#{into.name}_if_merge"
|
# 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)
|
cond_val = cond.compile(context , into)
|
||||||
puts "compiled if condition #{cond_val.inspect}"
|
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|
|
if_false.each do |part|
|
||||||
last = part.compile(context , false_block )
|
last = part.compile(context , false_block )
|
||||||
puts "compiled in if false #{last.inspect}"
|
puts "compiled in if false #{last.inspect}"
|
||||||
end
|
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"
|
puts "compile if end"
|
||||||
into.insert_at merge_block
|
into.insert_at merge_block
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ module Ast
|
|||||||
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
||||||
l_val = context.locals[left.name]
|
l_val = context.locals[left.name]
|
||||||
if( l_val ) #variable existed, move data there
|
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
|
else
|
||||||
l_val = context.function.new_local.move( into , r_val )
|
l_val = context.function.new_local.move( into , r_val )
|
||||||
end
|
end
|
||||||
|
@ -16,7 +16,7 @@ module Vm
|
|||||||
if arg.is_a?(IntegerConstant) or arg.is_a?(StringConstant)
|
if arg.is_a?(IntegerConstant) or arg.is_a?(StringConstant)
|
||||||
function.args[index].load into , arg
|
function.args[index].load into , arg
|
||||||
else
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -21,11 +21,10 @@ class TestFunctions < MiniTest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
putint(times(4,1))
|
putint(times(7,6))
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0xb0,0xa0,0xe3,0x28,0x0,0x8f,0xe2,0x28,0x10,0xa0,0xe3,0x1,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x0,0x10,0xa0,0xe1,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x80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0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0]
|
||||||
parse
|
parse
|
||||||
write "functions"
|
write "functions"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ class TestIf < MiniTest::Test
|
|||||||
end
|
end
|
||||||
itest(20)
|
itest(20)
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0xb0,0xa0,0xe3,0x14,0x10,0xa0,0xe3,0x2,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0xc,0x0,0x51,0xe3,0x5,0x0,0x0,0xaa,0x2,0x0,0x2d,0xe9,0x3c,0x10,0x8f,0xe2,0x8,0x20,0xa0,0xe3,0x7,0x0,0x0,0xeb,0x2,0x0,0xbd,0xe8,0x4,0x0,0x0,0xea,0x2,0x0,0x2d,0xe9,0x2c,0x10,0x8f,0xe2,0x8,0x20,0xa0,0xe3,0x1,0x0,0x0,0xeb,0x2,0x0,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x74,0x68,0x65,0x6e,0x0,0x0,0x0,0x0,0x65,0x6c,0x73,0x65,0x0,0x0,0x0,0x0]
|
@should = [0x0,0xb0,0xa0,0xe3,0x14,0x10,0xa0,0xe3,0x2,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0xc,0x0,0x51,0xe3,0x5,0x0,0x0,0xba,0x2,0x0,0x2d,0xe9,0x3c,0x10,0x8f,0xe2,0x8,0x20,0xa0,0xe3,0x7,0x0,0x0,0xeb,0x2,0x0,0xbd,0xe8,0x4,0x0,0x0,0xea,0x2,0x0,0x2d,0xe9,0x2c,0x10,0x8f,0xe2,0x8,0x20,0xa0,0xe3,0x1,0x0,0x0,0xeb,0x2,0x0,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x65,0x6c,0x73,0x65,0x0,0x0,0x0,0x0,0x74,0x68,0x65,0x6e,0x0,0x0,0x0,0x0]
|
||||||
parse
|
parse
|
||||||
write "if"
|
write "if"
|
||||||
end
|
end
|
||||||
|
@ -19,7 +19,7 @@ class TestWhile < MiniTest::Test
|
|||||||
|
|
||||||
fibonaccit( 10 )
|
fibonaccit( 10 )
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0xb0,0xa0,0xe3,0xa,0x10,0xa0,0xe3,0x2,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x0,0x20,0xa0,0xe3,0x1,0x30,0xa0,0xe3,0x1,0x0,0x51,0xe3,0x6,0x0,0x0,0xda,0x2,0x40,0xa0,0x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6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x7e,0x0,0x2d,0xe9,0x3,0x10,0xa0,0xe1,0x12,0x0,0x0,0xeb,0x7e,0x0,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0]
|
||||||
parse
|
parse
|
||||||
write "while"
|
write "while"
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user