diff --git a/lib/risc/instructions/branch.rb b/lib/risc/instructions/branch.rb index e3dd6aef..5a14e97f 100644 --- a/lib/risc/instructions/branch.rb +++ b/lib/risc/instructions/branch.rb @@ -12,7 +12,7 @@ module Risc attr_reader :label # return an array of names of registers that is used by the instruction - def register_names + def register_attributes [] end diff --git a/lib/risc/instructions/dynamic_jump.rb b/lib/risc/instructions/dynamic_jump.rb index 4f7aac07..b07d53a9 100644 --- a/lib/risc/instructions/dynamic_jump.rb +++ b/lib/risc/instructions/dynamic_jump.rb @@ -13,8 +13,8 @@ module Risc attr_reader :register # return an array of names of registers that is used by the instruction - def register_names - [register.symbol ] + def register_attributes + [:register ] end def to_s diff --git a/lib/risc/instructions/function_call.rb b/lib/risc/instructions/function_call.rb index 3df7271e..84e9838e 100644 --- a/lib/risc/instructions/function_call.rb +++ b/lib/risc/instructions/function_call.rb @@ -11,7 +11,7 @@ module Risc attr_reader :method # return an array of names of registers that is used by the instruction - def register_names + def register_attributes [] end diff --git a/lib/risc/instructions/function_return.rb b/lib/risc/instructions/function_return.rb index b90a8611..3e57c177 100644 --- a/lib/risc/instructions/function_return.rb +++ b/lib/risc/instructions/function_return.rb @@ -12,8 +12,8 @@ module Risc attr_reader :register # return an array of names of registers that is used by the instruction - def register_names - [register.symbol] + def register_attributes + [:register] end def to_s diff --git a/lib/risc/instructions/getter.rb b/lib/risc/instructions/getter.rb index 8cc22374..ead30890 100644 --- a/lib/risc/instructions/getter.rb +++ b/lib/risc/instructions/getter.rb @@ -30,9 +30,9 @@ module Risc 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) + def register_attributes + names = [:array , :register] + names << :index if index.is_a?(RegisterValue) names end diff --git a/lib/risc/instructions/instruction.rb b/lib/risc/instructions/instruction.rb index dcecc6dc..f0986260 100644 --- a/lib/risc/instructions/instruction.rb +++ b/lib/risc/instructions/instruction.rb @@ -42,10 +42,33 @@ module Risc end # return an array of names of registers that is used by the instruction - def register_names + def register_attributes raise "Not implemented in #{self.class}" end + # return an array of names of registers that is used by the instruction + def register_names + register_attributes.collect {|attr| get_register(attr)} + end + + # get the register value that has the name (ie attribute by that name) + # return the registers name ie RegisterValue's symbol + def get_register(name) + instance_variable_get("@#{name}".to_sym).symbol + end + + # set all registers that has the name "name" + # going through the register_names and setting all where the + # get_register would return name + # Set to the value given as second arg. + def set_registers(name , value) + register_attributes.each do |attr| + reg = instance_variable_get("@#{attr}".to_sym) + next unless reg.symbol == name + reg.set_name(value) + end + end + def to_cpu( translator ) translator.translate( self ) end diff --git a/lib/risc/instructions/label.rb b/lib/risc/instructions/label.rb index 8bfec0e8..8fd5170e 100644 --- a/lib/risc/instructions/label.rb +++ b/lib/risc/instructions/label.rb @@ -24,7 +24,7 @@ module Risc attr_reader :name , :address # return an array of names of registers that is used by the instruction - def register_names + def register_attributes [] end diff --git a/lib/risc/instructions/load_constant.rb b/lib/risc/instructions/load_constant.rb index cf2a4c18..92dbea46 100644 --- a/lib/risc/instructions/load_constant.rb +++ b/lib/risc/instructions/load_constant.rb @@ -15,8 +15,8 @@ module Risc attr_accessor :register , :constant # return an array of names of registers that is used by the instruction - def register_names - [register.symbol] + def register_attributes + [:register] end def to_s diff --git a/lib/risc/instructions/load_data.rb b/lib/risc/instructions/load_data.rb index 57389df8..26d3498d 100644 --- a/lib/risc/instructions/load_data.rb +++ b/lib/risc/instructions/load_data.rb @@ -18,8 +18,8 @@ module Risc attr_accessor :register , :constant # return an array of names of registers that is used by the instruction - def register_names - [register.symbol] + def register_attributes + [:register] end def to_s diff --git a/lib/risc/instructions/operator_instruction.rb b/lib/risc/instructions/operator_instruction.rb index 3f61e421..2d94a511 100644 --- a/lib/risc/instructions/operator_instruction.rb +++ b/lib/risc/instructions/operator_instruction.rb @@ -33,8 +33,8 @@ module Risc 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] + def register_attributes + [:left , :right, :result] end def to_s diff --git a/lib/risc/instructions/setter.rb b/lib/risc/instructions/setter.rb index 06c962fb..59f68b27 100644 --- a/lib/risc/instructions/setter.rb +++ b/lib/risc/instructions/setter.rb @@ -28,9 +28,9 @@ module Risc 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) + def register_attributes + names = [:array , :register] + names << :index if index.is_a?(RegisterValue) names end diff --git a/lib/risc/instructions/syscall.rb b/lib/risc/instructions/syscall.rb index a5de82ca..01934097 100644 --- a/lib/risc/instructions/syscall.rb +++ b/lib/risc/instructions/syscall.rb @@ -17,7 +17,7 @@ module Risc attr_reader :name # return an array of names of registers that is used by the instruction - def register_names + def register_attributes [] end diff --git a/lib/risc/instructions/transfer.rb b/lib/risc/instructions/transfer.rb index 46eb15fe..9eade0d4 100644 --- a/lib/risc/instructions/transfer.rb +++ b/lib/risc/instructions/transfer.rb @@ -26,8 +26,8 @@ module Risc attr_reader :from, :to # return an array of names of registers that is used by the instruction - def register_names - [from.symbol , to.symbol] + def register_attributes + [:from , :to] end def to_s diff --git a/lib/risc/register_value.rb b/lib/risc/register_value.rb index e51a4813..ea45dc43 100644 --- a/lib/risc/register_value.rb +++ b/lib/risc/register_value.rb @@ -5,11 +5,15 @@ module Risc # The type is always known, and sometimes the value too # Or something about the value, like some instances types # + # During initial creation ssa-like names are given to the registers. + # Later the name is changed, with set_name. When that happens + # the original is retained as ssa attttribute for debugging. + # # When participating in the compiler dsl, a compiler may be set to get the # results of dsl operations (like <<) back to the compiler class RegisterValue - attr_reader :symbol , :type , :extra + attr_reader :symbol , :type , :extra , :ssa attr_reader :compiler @@ -33,6 +37,17 @@ module Risc @type.class_name end + # During initial creation ssa-like names are given to the registers. + # Later the name is changed, with set_name. When that happens + # the original is retained as ssa attttribute for debugging. + def set_name( symbol ) + raise "not Symbol #{symbol}:#{symbol.class}" unless symbol.is_a?(Symbol) + old = @symbol + @ssa = @symbol + @symbol = symbol + old + end + # allows to set the compiler, which is mainly done by the compiler # but sometimes, eg in exit, one nneds to create the reg by hand and set # return the RegisterValue for chaining in assignment diff --git a/test/risc/test_register_value1.rb b/test/risc/test_register_value1.rb index 5fd0e15d..b7282625 100644 --- a/test/risc/test_register_value1.rb +++ b/test/risc/test_register_value1.rb @@ -50,5 +50,15 @@ module Risc assert_equal Parfait::Type , reg.type.class assert_equal "Integer_Type" , reg.type.name end + def test_has_ssa + assert_nil @r0.ssa + end + def test_set_name + assert_equal :message , @r0.set_name(:r0) + end + def test_set_ssa + @r0.set_name(:r0) + assert_equal :message , @r0.ssa + end end end