diff --git a/lib/register/passes/return_implementation.rb b/lib/register/passes/return_implementation.rb deleted file mode 100644 index cabd23cc..00000000 --- a/lib/register/passes/return_implementation.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Register - class ReturnImplementation - def run block - block.codes.dup.each do |code| - next unless code.is_a? Virtual::MethodReturn - new_codes = [] - # move the current message to new_message - new_codes << RegisterTransfer.new(code, Register.message_reg , Register.new_message_reg ) - # and restore the message from saved value in new_message - new_codes << Register.get_slot(code,:new_message , :caller , :message ) - #load the return address into pc, affecting return. (other cpus have commands for this, but not arm) - new_codes << FunctionReturn.new( code , Register.new_message_reg , Register.resolve_index(:message , :return_address) ) - block.replace(code , new_codes ) - end - end - end - Virtual.machine.add_pass "Register::ReturnImplementation" -end diff --git a/lib/register/register.rb b/lib/register/register.rb index 4a377a97..12ca21eb 100644 --- a/lib/register/register.rb +++ b/lib/register/register.rb @@ -1,7 +1,6 @@ require_relative "instruction" require_relative "register_value" require_relative "assembler" -require_relative "passes/return_implementation" require_relative "passes/call_implementation" # So the memory model of the machine allows for indexed access into an "object" . diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index 33704713..a5e714d2 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -16,4 +16,3 @@ end require_relative "instructions/halt" require_relative "instructions/method_call" require_relative "instructions/method_enter" -require_relative "instructions/method_return" diff --git a/lib/virtual/instructions/method_return.rb b/lib/virtual/instructions/method_return.rb deleted file mode 100644 index 5ce6f58f..00000000 --- a/lib/virtual/instructions/method_return.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Virtual - - - # also the return shuffles our objects beck before actually transferring control - class MethodReturn < Instruction - def initialize method - @method = method - end - - attr_reader :method - - end - -end diff --git a/lib/virtual/method_source.rb b/lib/virtual/method_source.rb index f5fe5718..b784c216 100644 --- a/lib/virtual/method_source.rb +++ b/lib/virtual/method_source.rb @@ -60,7 +60,13 @@ module Virtual set_return_type( return_type ) @blocks = [enter] @current = enter - new_block("return").add_code(MethodReturn.new(method)) + ret = new_block("return") + # move the current message to new_message + ret.add_code Register::RegisterTransfer.new(self, Register.message_reg , Register.new_message_reg ) + # and restore the message from saved value in new_message + ret.add_code Register.get_slot(self,:new_message , :caller , :message ) + #load the return address into pc, affecting return. (other cpus have commands for this, but not arm) + ret.add_code Register::FunctionReturn.new( self , Register.new_message_reg , Register.resolve_index(:message , :return_address) ) @constants = [] end attr_reader :blocks , :constants , :return_type diff --git a/test/compiler/fragments/test_class.rb b/test/compiler/fragments/test_class.rb index 43dbe7b6..4501f362 100644 --- a/test/compiler/fragments/test_class.rb +++ b/test/compiler/fragments/test_class.rb @@ -11,7 +11,7 @@ class Bar end end HERE - @expect = [ [Virtual::MethodEnter] ,[Virtual::MethodReturn]] + @expect = [ [Virtual::MethodEnter] ,[RegisterTransfer,GetSlot,FunctionReturn]] check end diff --git a/test/compiler/fragments/test_foo.rb b/test/compiler/fragments/test_foo.rb index 63bc66ad..65153b4c 100644 --- a/test/compiler/fragments/test_foo.rb +++ b/test/compiler/fragments/test_foo.rb @@ -17,7 +17,7 @@ class Object end HERE @expect = [ [Virtual::MethodEnter,Register::GetSlot,Virtual::Set,Virtual::Set, - Virtual::Set,Virtual::Set,Virtual::MethodCall] ,[Virtual::MethodReturn] ] + Virtual::Set,Virtual::Set,Virtual::MethodCall] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end diff --git a/test/compiler/fragments/test_functions.rb b/test/compiler/fragments/test_functions.rb index 49fc11b5..30f7b30e 100644 --- a/test/compiler/fragments/test_functions.rb +++ b/test/compiler/fragments/test_functions.rb @@ -36,7 +36,7 @@ class Object end HERE @expect = [[Virtual::MethodEnter,Register::GetSlot,Virtual::Set,Virtual::Set,Virtual::MethodCall] , - [Virtual::MethodReturn]] + [RegisterTransfer,GetSlot,FunctionReturn]] check end diff --git a/test/compiler/fragments/test_hello.rb b/test/compiler/fragments/test_hello.rb index 750ef8d5..4cd38c86 100644 --- a/test/compiler/fragments/test_hello.rb +++ b/test/compiler/fragments/test_hello.rb @@ -12,7 +12,7 @@ class Object end HERE @expect = [[Virtual::MethodEnter,Virtual::Set,Register::GetSlot,Virtual::Set, - Virtual::Set,Virtual::MethodCall] ,[Virtual::MethodReturn]] + Virtual::Set,Virtual::MethodCall] ,[RegisterTransfer,GetSlot,FunctionReturn]] check end end diff --git a/test/compiler/fragments/test_if.rb b/test/compiler/fragments/test_if.rb index 6a69d755..b64a4b83 100644 --- a/test/compiler/fragments/test_if.rb +++ b/test/compiler/fragments/test_if.rb @@ -18,7 +18,7 @@ end HERE @expect = [[Virtual::MethodEnter,Virtual::Set,Virtual::Set,Register::GetSlot, Register::GetSlot,Register::OperatorInstruction,Register::IsZeroBranch] , - [Virtual::Set,Register::AlwaysBranch] ,[Virtual::Set] ,[] ,[Virtual::MethodReturn] ] + [Virtual::Set,Register::AlwaysBranch] ,[Virtual::Set] ,[] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end @@ -30,7 +30,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,Virtual::Set] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,Virtual::Set] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -52,7 +52,7 @@ class Object end HERE @expect = [ [Virtual::MethodEnter,Register::GetSlot,Virtual::Set,Virtual::Set, - Virtual::Set,Virtual::Set,Virtual::MethodCall] ,[Virtual::MethodReturn] ] + Virtual::Set,Virtual::Set,Virtual::MethodCall] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end end diff --git a/test/compiler/fragments/test_putint.rb b/test/compiler/fragments/test_putint.rb index 3e4d9014..53421317 100644 --- a/test/compiler/fragments/test_putint.rb +++ b/test/compiler/fragments/test_putint.rb @@ -17,7 +17,7 @@ class Object end HERE @expect = [ [Virtual::MethodEnter,Virtual::Set,Register::GetSlot,Virtual::Set, - Virtual::Set,Virtual::MethodCall] ,[Virtual::MethodReturn] ] + Virtual::Set,Virtual::MethodCall] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end end diff --git a/test/compiler/fragments/test_recursive_fibo.rb b/test/compiler/fragments/test_recursive_fibo.rb index 7dadbd6c..d24680a2 100644 --- a/test/compiler/fragments/test_recursive_fibo.rb +++ b/test/compiler/fragments/test_recursive_fibo.rb @@ -28,7 +28,7 @@ class Object end HERE @expect = [[Virtual::MethodEnter,Register::GetSlot,Virtual::Set,Virtual::Set, - Virtual::Set,Virtual::Set,Virtual::MethodCall] , [Virtual::MethodReturn]] + Virtual::Set,Virtual::Set,Virtual::MethodCall] , [RegisterTransfer,GetSlot,FunctionReturn]] check end end diff --git a/test/compiler/fragments/test_while_fibo.rb b/test/compiler/fragments/test_while_fibo.rb index dd5ba2fc..92240f3f 100644 --- a/test/compiler/fragments/test_while_fibo.rb +++ b/test/compiler/fragments/test_while_fibo.rb @@ -25,7 +25,7 @@ class Object end HERE @expect = [ [Virtual::MethodEnter,Register::GetSlot,Virtual::Set,Virtual::Set, - Virtual::Set,Virtual::Set,Virtual::MethodCall] ,[Virtual::MethodReturn] ] + Virtual::Set,Virtual::Set,Virtual::MethodCall] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end diff --git a/test/compiler/statements/test_assign.rb b/test/compiler/statements/test_assign.rb index a1172f8d..a9dd8327 100644 --- a/test/compiler/statements/test_assign.rb +++ b/test/compiler/statements/test_assign.rb @@ -17,7 +17,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,LoadConstant,SetSlot] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,LoadConstant,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -29,7 +29,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -42,7 +42,7 @@ end end HERE @expect = [[Virtual::MethodEnter,LoadConstant,LoadConstant, - OperatorInstruction,GetSlot,SetSlot],[Virtual::MethodReturn]] + OperatorInstruction,GetSlot,SetSlot],[RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -55,7 +55,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -67,7 +67,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,LoadConstant, GetSlot,SetSlot] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,LoadConstant, GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -80,7 +80,7 @@ class Object end HERE @expect = [[Virtual::MethodEnter,GetSlot,GetSlot,SetSlot, LoadConstant,SetSlot, - Virtual::MethodCall,GetSlot,GetSlot,SetSlot] , [Virtual::MethodReturn]] + Virtual::MethodCall,GetSlot,GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end end diff --git a/test/compiler/statements/test_call.rb b/test/compiler/statements/test_call.rb index dc03d658..7f4832bf 100644 --- a/test/compiler/statements/test_call.rb +++ b/test/compiler/statements/test_call.rb @@ -19,7 +19,7 @@ end HERE @expect = [[Virtual::MethodEnter,GetSlot,LoadConstant, SetSlot,LoadConstant,SetSlot,Virtual::MethodCall,GetSlot] , - [Virtual::MethodReturn]] + [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -39,7 +39,7 @@ end HERE @expect = [[Virtual::MethodEnter,GetSlot,LoadConstant, SetSlot,LoadConstant,SetSlot,Virtual::MethodCall,GetSlot] , - [Virtual::MethodReturn]] + [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -59,7 +59,7 @@ end HERE @expect = [ [Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot,GetSlot, GetSlot,GetSlot,SetSlot,LoadConstant,SetSlot,Virtual::MethodCall, - GetSlot] ,[Virtual::MethodReturn] ] + GetSlot] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end @@ -79,7 +79,7 @@ end HERE @expect = [ [Virtual::MethodEnter,GetSlot,GetSlot,GetSlot,SetSlot, LoadConstant,SetSlot,Virtual::MethodCall, - GetSlot] ,[Virtual::MethodReturn] ] + GetSlot] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end @@ -96,7 +96,7 @@ end HERE @expect = [ [Virtual::MethodEnter , GetSlot,GetSlot,SetSlot,LoadConstant,SetSlot,LoadConstant, SetSlot,Virtual::MethodCall,GetSlot], - [Virtual::MethodReturn]] + [RegisterTransfer,GetSlot,FunctionReturn]] check end diff --git a/test/compiler/statements/test_if.rb b/test/compiler/statements/test_if.rb index ee11d7ee..2d4d30e5 100644 --- a/test/compiler/statements/test_if.rb +++ b/test/compiler/statements/test_if.rb @@ -19,7 +19,7 @@ HERE @expect = [[Virtual::MethodEnter,LoadConstant,LoadConstant, OperatorInstruction,IsZeroBranch] , [LoadConstant,AlwaysBranch] ,[LoadConstant] ,[] , - [Virtual::MethodReturn]] + [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -37,7 +37,7 @@ HERE @expect = [[Virtual::MethodEnter,LoadConstant,LoadConstant, OperatorInstruction,IsZeroBranch] , [AlwaysBranch] ,[LoadConstant] ,[] , - [Virtual::MethodReturn]] + [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -56,7 +56,7 @@ end HERE @expect = [ [Virtual::MethodEnter,GetSlot,SetSlot,LoadConstant, SetSlot,LoadConstant,SetSlot,Virtual::MethodCall, - GetSlot] ,[Virtual::MethodReturn] ] + GetSlot] ,[RegisterTransfer,GetSlot,FunctionReturn] ] check end end diff --git a/test/compiler/statements/test_return.rb b/test/compiler/statements/test_return.rb index c456da16..26fa5d29 100644 --- a/test/compiler/statements/test_return.rb +++ b/test/compiler/statements/test_return.rb @@ -13,7 +13,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,LoadConstant] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,LoadConstant] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -26,7 +26,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,GetSlot,GetSlot] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,GetSlot,GetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -39,7 +39,8 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot,GetSlot,GetSlot] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot,GetSlot,GetSlot] , + [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -52,7 +53,7 @@ class Object end end HERE - @expect = [[Virtual::MethodEnter,GetSlot,GetSlot] , [Virtual::MethodReturn]] + @expect = [[Virtual::MethodEnter,GetSlot,GetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -65,7 +66,7 @@ class Object end HERE @expect = [[Virtual::MethodEnter,GetSlot,GetSlot,SetSlot, LoadConstant, - SetSlot,Virtual::MethodCall,GetSlot] , [Virtual::MethodReturn]] + SetSlot,Virtual::MethodCall,GetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]] check end end diff --git a/test/compiler/statements/test_while.rb b/test/compiler/statements/test_while.rb index 97d1615a..03bcf351 100644 --- a/test/compiler/statements/test_while.rb +++ b/test/compiler/statements/test_while.rb @@ -16,7 +16,7 @@ class Object end HERE @expect = [[Virtual::MethodEnter],[LoadConstant,IsZeroBranch,LoadConstant,AlwaysBranch], - [],[Virtual::MethodReturn]] + [],[RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -33,7 +33,7 @@ end HERE @expect = [[Virtual::MethodEnter,LoadConstant,GetSlot,SetSlot],[GetSlot,GetSlot,LoadConstant,OperatorInstruction, IsZeroBranch,GetSlot,GetSlot,LoadConstant,OperatorInstruction,GetSlot,SetSlot,AlwaysBranch], - [],[Virtual::MethodReturn]] + [],[RegisterTransfer,GetSlot,FunctionReturn]] check end @@ -54,7 +54,7 @@ HERE [GetSlot,GetSlot,LoadConstant,OperatorInstruction,IsZeroBranch,GetSlot, GetSlot,LoadConstant,OperatorInstruction,GetSlot,SetSlot,GetSlot, GetSlot,AlwaysBranch] , - [],[Virtual::MethodReturn]] + [],[RegisterTransfer,GetSlot,FunctionReturn]] check end end