From d0b734c57c7864194a9c8615d99f2300ea593d32 Mon Sep 17 00:00:00 2001 From: Torsten Date: Wed, 18 Mar 2020 17:49:23 +0200 Subject: [PATCH] adding register_names to instruction protocol --- lib/risc/instructions/branch.rb | 5 +++++ lib/risc/instructions/dynamic_jump.rb | 5 +++++ lib/risc/instructions/function_call.rb | 5 +++++ lib/risc/instructions/function_return.rb | 5 +++++ lib/risc/instructions/getter.rb | 7 +++++++ lib/risc/instructions/instruction.rb | 5 +++++ lib/risc/instructions/label.rb | 5 +++++ lib/risc/instructions/load_constant.rb | 5 +++++ lib/risc/instructions/load_data.rb | 5 +++++ lib/risc/instructions/operator_instruction.rb | 5 +++++ lib/risc/instructions/setter.rb | 7 +++++++ lib/risc/instructions/syscall.rb | 5 +++++ lib/risc/instructions/transfer.rb | 5 +++++ 13 files changed, 69 insertions(+) diff --git a/lib/risc/instructions/branch.rb b/lib/risc/instructions/branch.rb index 69a20097..e3dd6aef 100644 --- a/lib/risc/instructions/branch.rb +++ b/lib/risc/instructions/branch.rb @@ -11,6 +11,11 @@ module Risc end attr_reader :label + # return an array of names of registers that is used by the instruction + def register_names + [] + end + def to_s case label when Label diff --git a/lib/risc/instructions/dynamic_jump.rb b/lib/risc/instructions/dynamic_jump.rb index 4505f5f1..4f7aac07 100644 --- a/lib/risc/instructions/dynamic_jump.rb +++ b/lib/risc/instructions/dynamic_jump.rb @@ -12,6 +12,11 @@ module Risc end attr_reader :register + # return an array of names of registers that is used by the instruction + def register_names + [register.symbol ] + end + def to_s class_source( register.to_s) end diff --git a/lib/risc/instructions/function_call.rb b/lib/risc/instructions/function_call.rb index cfc11a74..3df7271e 100644 --- a/lib/risc/instructions/function_call.rb +++ b/lib/risc/instructions/function_call.rb @@ -10,6 +10,11 @@ module Risc end attr_reader :method + # return an array of names of registers that is used by the instruction + def register_names + [] + end + def to_s class_source method.name end diff --git a/lib/risc/instructions/function_return.rb b/lib/risc/instructions/function_return.rb index 691b5195..b90a8611 100644 --- a/lib/risc/instructions/function_return.rb +++ b/lib/risc/instructions/function_return.rb @@ -11,6 +11,11 @@ module Risc end attr_reader :register + # return an array of names of registers that is used by the instruction + def register_names + [register.symbol] + end + def to_s class_source "#{register} " end diff --git a/lib/risc/instructions/getter.rb b/lib/risc/instructions/getter.rb index 564883eb..8cc22374 100644 --- a/lib/risc/instructions/getter.rb +++ b/lib/risc/instructions/getter.rb @@ -29,6 +29,13 @@ module Risc end attr_accessor :array , :index , :register + # return an array of names of registers that is used by the instruction + def register_names + names = [array.symbol , register.symbol] + names << index.symbol if index.is_a?(RegisterValue) + names + end + def to_s class_source "#{array}[#{index}] -> #{register}" end diff --git a/lib/risc/instructions/instruction.rb b/lib/risc/instructions/instruction.rb index 6eb3e128..dcecc6dc 100644 --- a/lib/risc/instructions/instruction.rb +++ b/lib/risc/instructions/instruction.rb @@ -41,6 +41,11 @@ module Risc def precheck end + # return an array of names of registers that is used by the instruction + def register_names + raise "Not implemented in #{self.class}" + end + def to_cpu( translator ) translator.translate( self ) end diff --git a/lib/risc/instructions/label.rb b/lib/risc/instructions/label.rb index fb17f63e..8bfec0e8 100644 --- a/lib/risc/instructions/label.rb +++ b/lib/risc/instructions/label.rb @@ -23,6 +23,11 @@ module Risc end attr_reader :name , :address + # return an array of names of registers that is used by the instruction + def register_names + [] + end + def to_cpu(translator) @cpu_label ||= super end diff --git a/lib/risc/instructions/load_constant.rb b/lib/risc/instructions/load_constant.rb index f526956d..cf2a4c18 100644 --- a/lib/risc/instructions/load_constant.rb +++ b/lib/risc/instructions/load_constant.rb @@ -14,6 +14,11 @@ module Risc end attr_accessor :register , :constant + # return an array of names of registers that is used by the instruction + def register_names + [register.symbol] + end + def to_s class_source "#{register} <- #{constant_str}" end diff --git a/lib/risc/instructions/load_data.rb b/lib/risc/instructions/load_data.rb index 43a9286e..57389df8 100644 --- a/lib/risc/instructions/load_data.rb +++ b/lib/risc/instructions/load_data.rb @@ -17,6 +17,11 @@ module Risc end attr_accessor :register , :constant + # return an array of names of registers that is used by the instruction + def register_names + [register.symbol] + end + def to_s class_source "#{register} <- #{constant}" end diff --git a/lib/risc/instructions/operator_instruction.rb b/lib/risc/instructions/operator_instruction.rb index a0d877bb..3f61e421 100644 --- a/lib/risc/instructions/operator_instruction.rb +++ b/lib/risc/instructions/operator_instruction.rb @@ -32,6 +32,11 @@ module Risc end attr_reader :operator, :left , :right , :result + # return an array of names of registers that is used by the instruction + def register_names + [left.symbol , right.symbol, result.symbol] + end + def to_s class_source "#{left} #{operator} #{right}" end diff --git a/lib/risc/instructions/setter.rb b/lib/risc/instructions/setter.rb index af529ab6..06c962fb 100644 --- a/lib/risc/instructions/setter.rb +++ b/lib/risc/instructions/setter.rb @@ -27,6 +27,13 @@ module Risc end attr_accessor :register , :array , :index + # return an array of names of registers that is used by the instruction + def register_names + names = [array.symbol , register.symbol] + names << index.symbol if index.is_a?(RegisterValue) + names + end + def to_s class_source "#{register} -> #{array}[#{index}]" end diff --git a/lib/risc/instructions/syscall.rb b/lib/risc/instructions/syscall.rb index c2701c3e..a5de82ca 100644 --- a/lib/risc/instructions/syscall.rb +++ b/lib/risc/instructions/syscall.rb @@ -16,6 +16,11 @@ module Risc end attr_reader :name + # return an array of names of registers that is used by the instruction + def register_names + [] + end + def to_s class_source name end diff --git a/lib/risc/instructions/transfer.rb b/lib/risc/instructions/transfer.rb index cb870015..46eb15fe 100644 --- a/lib/risc/instructions/transfer.rb +++ b/lib/risc/instructions/transfer.rb @@ -25,6 +25,11 @@ module Risc end attr_reader :from, :to + # return an array of names of registers that is used by the instruction + def register_names + [from.symbol , to.symbol] + end + def to_s class_source "#{from} -> #{to}" end