diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index 3bf0d517..73a63e17 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -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 diff --git a/lib/ast/if_expression.rb b/lib/ast/if_expression.rb index d67c2c87..2a48d589 100644 --- a/lib/ast/if_expression.rb +++ b/lib/ast/if_expression.rb @@ -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 diff --git a/lib/ast/operator_expressions.rb b/lib/ast/operator_expressions.rb index 449f1474..cd120cff 100644 --- a/lib/ast/operator_expressions.rb +++ b/lib/ast/operator_expressions.rb @@ -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 diff --git a/lib/vm/call_site.rb b/lib/vm/call_site.rb index 115f1c56..121819a1 100644 --- a/lib/vm/call_site.rb +++ b/lib/vm/call_site.rb @@ -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 diff --git a/test/fragments/test_functions.rb b/test/fragments/test_functions.rb index 2a62644a..8998d7ba 100644 --- a/test/fragments/test_functions.rb +++ b/test/fragments/test_functions.rb @@ -21,11 +21,10 @@ class TestFunctions < MiniTest::Test end end - putint(times(4,1)) + putint(times(7,6)) 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,0xbd,0xe8,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x52,0x61,0x69,0x73,0x61,0x2c,0x20,0x49,0x20,0x61,0x6d,0x20,0x63,0x72,0x79,0x73,0x74,0x6b,0x73,0x64,0x66,0x6b,0x6c,0x6a,0x73,0x6e,0x63,0x6a,0x6e,0x63,0x6e,0x0,0x0,0x0] + @should = [0x0,0xb0,0xa0,0xe3,0x7,0x10,0xa0,0xe3,0x6,0x20,0xa0,0xe3,0xc,0x0,0x0,0xeb,0x7,0x10,0xa0,0xe1,0x32,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x2,0x30,0x81,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x0,0x52,0xe3,0x10,0x0,0x0,0xa,0x6,0x0,0x2d,0xe9,0x2,0x10,0xa0,0xe1,0x1,0x20,0xa0,0xe3,0xf0,0xff,0xff,0xeb,0x6,0x0,0xbd,0xe8,0x7,0x30,0xa0,0xe1,0xe,0x0,0x2d,0xe9,0x3,0x20,0xa0,0xe1,0xf3,0xff,0xff,0xeb,0xe,0x0,0xbd,0xe8,0x7,0x40,0xa0,0xe1,0x1e,0x0,0x2d,0xe9,0x4,0x20,0xa0,0xe1,0xea,0xff,0xff,0xeb,0x1e,0x0,0xbd,0xe8,0x7,0x10,0xa0,0xe1,0x0,0x0,0x0,0xea,0x0,0x10,0xa0,0xe3,0x1,0x70,0xa0,0xe1,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 write "functions" end end - diff --git a/test/fragments/test_if.rb b/test/fragments/test_if.rb index 2b465440..23cb5a10 100644 --- a/test/fragments/test_if.rb +++ b/test/fragments/test_if.rb @@ -14,7 +14,7 @@ class TestIf < MiniTest::Test end itest(20) 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 write "if" end diff --git a/test/fragments/test_while.rb b/test/fragments/test_while.rb index 8d9d9d1e..16f4e5ba 100644 --- a/test/fragments/test_while.rb +++ b/test/fragments/test_while.rb @@ -19,7 +19,7 @@ class TestWhile < MiniTest::Test fibonaccit( 10 ) 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,0xe1,0x3,0x20,0xa0,0xe1,0x3,0x50,0x84,0xe0,0x5,0x30,0xa0,0xe1,0x1,0x60,0x41,0xe2,0x6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x7e,0x0,0x2d,0xe9,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,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20,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 write "while" end