change in register_names protocol
move to returning the attribute names getting and setting can then be automated in the base class
This commit is contained in:
parent
0137056b89
commit
c890e8402b
@ -12,7 +12,7 @@ module Risc
|
|||||||
attr_reader :label
|
attr_reader :label
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ module Risc
|
|||||||
attr_reader :register
|
attr_reader :register
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[register.symbol ]
|
[:register ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
@ -11,7 +11,7 @@ module Risc
|
|||||||
attr_reader :method
|
attr_reader :method
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,8 +12,8 @@ module Risc
|
|||||||
attr_reader :register
|
attr_reader :register
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[register.symbol]
|
[:register]
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
@ -30,9 +30,9 @@ module Risc
|
|||||||
attr_accessor :array , :index , :register
|
attr_accessor :array , :index , :register
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
names = [array.symbol , register.symbol]
|
names = [:array , :register]
|
||||||
names << index.symbol if index.is_a?(RegisterValue)
|
names << :index if index.is_a?(RegisterValue)
|
||||||
names
|
names
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -42,10 +42,33 @@ module Risc
|
|||||||
end
|
end
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# 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}"
|
raise "Not implemented in #{self.class}"
|
||||||
end
|
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 )
|
def to_cpu( translator )
|
||||||
translator.translate( self )
|
translator.translate( self )
|
||||||
end
|
end
|
||||||
|
@ -24,7 +24,7 @@ module Risc
|
|||||||
attr_reader :name , :address
|
attr_reader :name , :address
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -15,8 +15,8 @@ module Risc
|
|||||||
attr_accessor :register , :constant
|
attr_accessor :register , :constant
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[register.symbol]
|
[:register]
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
@ -18,8 +18,8 @@ module Risc
|
|||||||
attr_accessor :register , :constant
|
attr_accessor :register , :constant
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[register.symbol]
|
[:register]
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
@ -33,8 +33,8 @@ module Risc
|
|||||||
attr_reader :operator, :left , :right , :result
|
attr_reader :operator, :left , :right , :result
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[left.symbol , right.symbol, result.symbol]
|
[:left , :right, :result]
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
@ -28,9 +28,9 @@ module Risc
|
|||||||
attr_accessor :register , :array , :index
|
attr_accessor :register , :array , :index
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
names = [array.symbol , register.symbol]
|
names = [:array , :register]
|
||||||
names << index.symbol if index.is_a?(RegisterValue)
|
names << :index if index.is_a?(RegisterValue)
|
||||||
names
|
names
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ module Risc
|
|||||||
attr_reader :name
|
attr_reader :name
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,8 +26,8 @@ module Risc
|
|||||||
attr_reader :from, :to
|
attr_reader :from, :to
|
||||||
|
|
||||||
# return an array of names of registers that is used by the instruction
|
# return an array of names of registers that is used by the instruction
|
||||||
def register_names
|
def register_attributes
|
||||||
[from.symbol , to.symbol]
|
[:from , :to]
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
@ -5,11 +5,15 @@ module Risc
|
|||||||
# The type is always known, and sometimes the value too
|
# The type is always known, and sometimes the value too
|
||||||
# Or something about the value, like some instances types
|
# 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
|
# When participating in the compiler dsl, a compiler may be set to get the
|
||||||
# results of dsl operations (like <<) back to the compiler
|
# results of dsl operations (like <<) back to the compiler
|
||||||
class RegisterValue
|
class RegisterValue
|
||||||
|
|
||||||
attr_reader :symbol , :type , :extra
|
attr_reader :symbol , :type , :extra , :ssa
|
||||||
|
|
||||||
attr_reader :compiler
|
attr_reader :compiler
|
||||||
|
|
||||||
@ -33,6 +37,17 @@ module Risc
|
|||||||
@type.class_name
|
@type.class_name
|
||||||
end
|
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
|
# 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
|
# but sometimes, eg in exit, one nneds to create the reg by hand and set
|
||||||
# return the RegisterValue for chaining in assignment
|
# return the RegisterValue for chaining in assignment
|
||||||
|
@ -50,5 +50,15 @@ module Risc
|
|||||||
assert_equal Parfait::Type , reg.type.class
|
assert_equal Parfait::Type , reg.type.class
|
||||||
assert_equal "Integer_Type" , reg.type.name
|
assert_equal "Integer_Type" , reg.type.name
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user