2014-08-22 16:40:09 +02:00
|
|
|
module Register
|
2014-06-14 09:59:25 +02:00
|
|
|
|
2015-05-24 14:07:07 +02:00
|
|
|
# RegisterReference is not the name for a register, "only" for a certain use of it.
|
|
|
|
# In a way it is like a variable name, a storage location. The location is a register off course,
|
2015-05-30 11:20:39 +02:00
|
|
|
# but which register can be changed, and _all_ instructions sharing the RegisterReference then
|
|
|
|
# use that register
|
2014-06-14 09:59:25 +02:00
|
|
|
# In other words a simple level of indirection, or change from value to reference sematics.
|
|
|
|
|
|
|
|
class RegisterReference
|
2015-05-24 14:31:30 +02:00
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
attr_accessor :symbol
|
2015-05-24 14:31:30 +02:00
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
def initialize r
|
|
|
|
raise "wrong type for register init #{r}" unless r.is_a? Symbol
|
2015-06-01 07:34:17 +02:00
|
|
|
raise "double r #{r}" if r.to_s[0,1] == "rr"
|
|
|
|
raise "not reg #{r}" unless self.class.look_like_reg r
|
2014-06-14 09:59:25 +02:00
|
|
|
@symbol = r
|
|
|
|
end
|
|
|
|
|
2015-06-01 07:34:17 +02:00
|
|
|
def self.convert something
|
|
|
|
return something unless something.is_a? Symbol
|
|
|
|
return something unless look_like_reg(something)
|
|
|
|
return new(something)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.look_like_reg is_it
|
2015-06-26 19:35:16 +02:00
|
|
|
return false unless is_it.is_a? Symbol
|
2015-06-01 07:34:17 +02:00
|
|
|
if( [:lr , :pc].include? is_it )
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if( (is_it.to_s.length < 3) and (is_it.to_s[0] == "r"))
|
2015-06-26 19:35:16 +02:00
|
|
|
# could tighten this by checking that the rest is a number
|
2015-06-01 07:34:17 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
def == other
|
|
|
|
return false if other.nil?
|
|
|
|
return false if other.class != RegisterReference
|
|
|
|
symbol == other.symbol
|
|
|
|
end
|
|
|
|
|
|
|
|
#helper method to calculate with register symbols
|
|
|
|
def next_reg_use by = 1
|
|
|
|
int = @symbol[1,3].to_i
|
|
|
|
sym = "r#{int + by}".to_sym
|
|
|
|
RegisterReference.new( sym )
|
|
|
|
end
|
2015-05-24 14:07:07 +02:00
|
|
|
|
|
|
|
SELF_REG = :r0
|
|
|
|
MESSAGE_REG = :r1
|
|
|
|
FRAME_REG = :r2
|
|
|
|
NEW_MESSAGE_REG = :r3
|
|
|
|
|
|
|
|
TMP_REG = :r4
|
|
|
|
|
|
|
|
def self.self_reg
|
|
|
|
new SELF_REG
|
|
|
|
end
|
2015-05-24 14:31:30 +02:00
|
|
|
def self.message_reg
|
2015-05-24 14:07:07 +02:00
|
|
|
new MESSAGE_REG
|
|
|
|
end
|
|
|
|
def self.frame_reg
|
|
|
|
new FRAME_REG
|
|
|
|
end
|
2015-05-24 14:31:30 +02:00
|
|
|
def self.new_message_reg
|
2015-05-24 14:07:07 +02:00
|
|
|
new NEW_MESSAGE_REG
|
|
|
|
end
|
|
|
|
def self.tmp_reg
|
|
|
|
new TMP_REG
|
|
|
|
end
|
|
|
|
|
2015-06-19 18:50:53 +02:00
|
|
|
def sof_reference_name
|
|
|
|
@symbol
|
|
|
|
end
|
2015-05-24 14:07:07 +02:00
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|