From 3912e0cd3146efe4be423d18082de92923fa1015 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 14 May 2014 22:04:03 +0300 Subject: [PATCH] fix register issues, some typos --- lib/arm/arm_machine.rb | 13 +++++++------ lib/arm/logic_helper.rb | 1 + lib/ast/operator_expressions.rb | 15 +++++++-------- lib/ast/while_expression.rb | 2 ++ lib/vm/call_site.rb | 2 +- lib/vm/function.rb | 6 +++--- lib/vm/values.rb | 8 ++++---- test/test_runner.rb | 4 +--- 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/arm/arm_machine.rb b/lib/arm/arm_machine.rb index ccc07fbc..7728e261 100644 --- a/lib/arm/arm_machine.rb +++ b/lib/arm/arm_machine.rb @@ -15,14 +15,14 @@ module Arm Vm::Bool.new end - def integer_plus block , left , right - block.add_code add(:left => left , :right => left , :extra => :right ) - left + def integer_plus block , result , left , right + block.add_code add(:left => result , :right => left , :extra => right ) + result end - def integer_minus block , left , right - block.add_code sub(:left => left , :right => left , :extra => :right ) - left + def integer_minus block , result , left , right + block.add_code sub(:left => result , :right => left , :extra => right ) + result end def integer_load block , left , right @@ -54,6 +54,7 @@ module Arm end def main_exit exit syscall(exit , 1) + exit end def function_entry block, f_name # entry.add_code push( :regs => [:lr] ) diff --git a/lib/arm/logic_helper.rb b/lib/arm/logic_helper.rb index d25d7ec4..e575c44f 100644 --- a/lib/arm/logic_helper.rb +++ b/lib/arm/logic_helper.rb @@ -39,6 +39,7 @@ module Arm elsif (op_with_rot = calculate_u8_with_rr(arg)) @operand = op_with_rot @i = 1 + raise "hmm" else raise "cannot fit numeric literal argument in operand #{arg.inspect}" end diff --git a/lib/ast/operator_expressions.rb b/lib/ast/operator_expressions.rb index 973a0099..d07ed818 100644 --- a/lib/ast/operator_expressions.rb +++ b/lib/ast/operator_expressions.rb @@ -24,12 +24,8 @@ module Ast if( l_val ) #variable existed, move data there l_val = l_val.move( into , r_val) else - if r_val.is_a? Vm::IntegerConstant - next_register = context.function.next_register - l_val = Vm::Integer.new(next_register).load( into , r_val ) - else - l_val = r_val - end + next_register = context.function.next_register + l_val = Vm::Integer.new(next_register).load( into , r_val ) end context.locals[left.name] = l_val return l_val @@ -41,12 +37,15 @@ module Ast when ">" code = l_val.less_or_equal into , r_val when "+" - code = l_val.plus into , r_val + res = Vm::Integer.new(context.function.next_register) + code = res.plus into , l_val , r_val when "-" - code = l_val.minus into , r_val + res = Vm::Integer.new(context.function.next_register) + code = res.minus into , l_val , r_val else raise "unimplemented operator #{operator} #{self}" end + code end end end \ No newline at end of file diff --git a/lib/ast/while_expression.rb b/lib/ast/while_expression.rb index f2c0b069..99e2eebe 100644 --- a/lib/ast/while_expression.rb +++ b/lib/ast/while_expression.rb @@ -16,11 +16,13 @@ module Ast def compile context , into cond_val = condition.compile(context , into) #set up branches for bodies + # jump to end if done last = nil body.each do |part| last = part.compile(context , into ) puts "compiled in while #{last.inspect}" end + #jump back to test return last end end diff --git a/lib/vm/call_site.rb b/lib/vm/call_site.rb index 17499556..6a975393 100644 --- a/lib/vm/call_site.rb +++ b/lib/vm/call_site.rb @@ -16,7 +16,7 @@ module Vm if arg.is_a? IntegerConstant Vm::Integer.new(index).load into , arg else - arg.move( into , index ) if arg.register != index + Vm::Integer.new(index).move( into, arg ) if arg.register != index end end end diff --git a/lib/vm/function.rb b/lib/vm/function.rb index f98fe423..70b7f107 100644 --- a/lib/vm/function.rb +++ b/lib/vm/function.rb @@ -22,8 +22,8 @@ module Vm super() @name = name @args = args - @entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_exit") ,name ) - @exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_entry") , name ) + @entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry") ,name ) + @exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit") , name ) @body = Block.new("#{name}_body") @reg_count = 0 branch_body @@ -47,7 +47,7 @@ module Vm @entry.link_at address , context address += @entry.length @body.link_at(address , context) - address += @entry.length + address += @body.length @exit.link_at(address,context) end diff --git a/lib/vm/values.rb b/lib/vm/values.rb index 55390eb0..8813c76b 100644 --- a/lib/vm/values.rb +++ b/lib/vm/values.rb @@ -63,11 +63,11 @@ module Vm CMachine.instance.integer_less_or_equal block , self , right end - def plus block , right - CMachine.instance.integer_plus block , self , right + def plus block , left , right + CMachine.instance.integer_plus block , self , left , right end - def minus block , right - CMachine.instance.integer_minus block , self , right + def minus block , left , right + CMachine.instance.integer_minus block , self , left , right end def load block , right diff --git a/test/test_runner.rb b/test/test_runner.rb index ebb51e30..9e694d91 100644 --- a/test/test_runner.rb +++ b/test/test_runner.rb @@ -31,7 +31,6 @@ class TestRunner < MiniTest::Test else expr = part.compile( program.context , nil ) raise "should be function definition for now" unless expr.is_a? Vm::Function - program.add_function expr end end @@ -45,9 +44,8 @@ class TestRunner < MiniTest::Test blocks.flatten.each do |b| writer.add_symbol b.name.to_s , b.position end - assembly = program.assemble(StringIO.new) - writer.set_text assembly.string + writer.set_text binary.string writer.save(file.gsub(".rb" , ".o")) # puts program.to_yaml