From 60c8f8ef7315879d940081921a0f14979c5c29dc Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 30 Jun 2015 09:43:50 +0300 Subject: [PATCH] move generator functions to the instructions they generate --- lib/register/instructions/get_slot.rb | 11 +++++++++ lib/register/instructions/save_return.rb | 14 ++++++++++-- lib/register/instructions/set_slot.rb | 11 +++++++++ lib/register/register_reference.rb | 29 ------------------------ 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/register/instructions/get_slot.rb b/lib/register/instructions/get_slot.rb index d2e3fda1..d8737e22 100644 --- a/lib/register/instructions/get_slot.rb +++ b/lib/register/instructions/get_slot.rb @@ -28,4 +28,15 @@ module Register end attr_accessor :register , :array , :index end + + # Produce a GetSlot instruction. + # From and to are registers or symbols that can be transformed to a register by resolve_to_register + # index resolves with resolve_index. + def self.get_slot from , index , to + index = resolve_index( from , index) + from = resolve_to_register from + to = resolve_to_register to + GetSlot.new( from , index , to) + end + end diff --git a/lib/register/instructions/save_return.rb b/lib/register/instructions/save_return.rb index 6531ba5d..77a19911 100644 --- a/lib/register/instructions/save_return.rb +++ b/lib/register/instructions/save_return.rb @@ -3,7 +3,7 @@ module Register # save the return address of a call # register and index specify where the return address is stored - # This instruction exists mainly, so we don't have to hard-code where the machine stores the + # This instruction exists mainly, so we don't have to hard-code where the machine stores the # address. In arm that is a register, but intel may (?) push it, and who knows, what other machines do. class SaveReturn < Instruction @@ -13,4 +13,14 @@ module Register end attr_reader :register , :index end -end \ No newline at end of file + + # Produce a SaveReturn instruction. + # From is a register or symbol that can be transformed to a register by resolve_to_register + # index resolves with resolve_index. + def self.save_return from , index + index = resolve_index( from , index) + from = resolve_to_register from + SaveReturn.new( from , index ) + end + +end diff --git a/lib/register/instructions/set_slot.rb b/lib/register/instructions/set_slot.rb index ef191210..f00eac50 100644 --- a/lib/register/instructions/set_slot.rb +++ b/lib/register/instructions/set_slot.rb @@ -27,4 +27,15 @@ module Register end attr_accessor :register , :array , :index end + + # Produce a SetSlot instruction. + # From and to are registers or symbols that can be transformed to a register by resolve_to_register + # index resolves with resolve_index. + def self.set_slot from , to , index + index = resolve_index( to , index) + from = resolve_to_register from + to = resolve_to_register to + SetSlot.new( from , to , index) + end + end diff --git a/lib/register/register_reference.rb b/lib/register/register_reference.rb index 61cb958a..eb1fcbeb 100644 --- a/lib/register/register_reference.rb +++ b/lib/register/register_reference.rb @@ -86,35 +86,6 @@ module Register RegisterReference.new :r4 end - # Produce a GetSlot instruction (see there). - # From and to are registers or symbols that can be transformed to a register by resolve_to_register - # index resolves with resolve_index. - def self.get_slot from , index , to - index = resolve_index( from , index) - from = resolve_to_register from - to = resolve_to_register to - GetSlot.new( from , index , to) - end - - # Produce a SetSlot instruction (see there). - # From and to are registers or symbols that can be transformed to a register by resolve_to_register - # index resolves with resolve_index. - def self.set_slot from , to , index - index = resolve_index( to , index) - from = resolve_to_register from - to = resolve_to_register to - SetSlot.new( from , to , index) - end - - # Produce a SaveReturn instruction (see there). - # From is a register or symbol that can be transformed to a register by resolve_to_register - # index resolves with resolve_index. - def self.save_return from , index - index = resolve_index( from , index) - from = resolve_to_register from - SaveReturn.new( from , index ) - end - # The first arg is a class name (possibly lowercase) and the second an instance variable name. # By looking up the class and the layout for that class, we can resolve the instance # variable name to an index.