2014-08-22 17:40:09 +03:00
|
|
|
module Register
|
2014-06-14 10:59:25 +03:00
|
|
|
|
2015-10-10 21:38:55 +03:00
|
|
|
# RegisterValue is like a variable name, a storage location. The location is a register off course.
|
2014-06-14 10:59:25 +03:00
|
|
|
|
2015-10-10 21:38:55 +03:00
|
|
|
class RegisterValue
|
2015-05-24 15:31:30 +03:00
|
|
|
|
2015-10-14 13:45:46 +03:00
|
|
|
attr_accessor :symbol , :type , :value
|
2015-05-24 15:31:30 +03:00
|
|
|
|
2015-10-14 13:45:46 +03:00
|
|
|
def initialize r , type , value = nil
|
2014-06-14 10:59:25 +03:00
|
|
|
raise "wrong type for register init #{r}" unless r.is_a? Symbol
|
2015-06-01 08:34:17 +03:00
|
|
|
raise "double r #{r}" if r.to_s[0,1] == "rr"
|
|
|
|
raise "not reg #{r}" unless self.class.look_like_reg r
|
2015-11-10 11:48:53 +02:00
|
|
|
type = :Integer if type == :int
|
|
|
|
raise "Legacy type error, should be class name not ref" if (type == :ref)
|
2015-10-14 16:16:03 +03:00
|
|
|
@type = type
|
2014-06-14 10:59:25 +03:00
|
|
|
@symbol = r
|
2015-10-14 13:45:46 +03:00
|
|
|
@value = value
|
2014-06-14 10:59:25 +03:00
|
|
|
end
|
|
|
|
|
2015-07-24 13:23:56 +03:00
|
|
|
def to_s
|
2015-10-17 10:03:56 +03:00
|
|
|
s = "#{symbol}:#{type}"
|
|
|
|
s += ":#{value}" if value
|
|
|
|
s
|
2015-06-01 08:34:17 +03:00
|
|
|
end
|
|
|
|
|
2015-11-14 00:22:23 +02:00
|
|
|
def reg_no
|
2015-11-14 13:05:49 +02:00
|
|
|
@symbol.to_s[1 .. -1].to_i
|
2015-11-14 00:22:23 +02:00
|
|
|
end
|
|
|
|
|
2015-06-01 08:34:17 +03:00
|
|
|
def self.look_like_reg is_it
|
2015-10-10 21:38:55 +03:00
|
|
|
return true if is_it.is_a? RegisterValue
|
2015-06-26 20:35:16 +03:00
|
|
|
return false unless is_it.is_a? Symbol
|
2015-06-01 08:34:17 +03:00
|
|
|
if( [:lr , :pc].include? is_it )
|
|
|
|
return true
|
|
|
|
end
|
2015-07-24 17:23:44 +03:00
|
|
|
if( (is_it.to_s.length <= 3) and (is_it.to_s[0] == "r"))
|
2015-06-26 20:35:16 +03:00
|
|
|
# could tighten this by checking that the rest is a number
|
2015-06-01 08:34:17 +03:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-06-14 10:59:25 +03:00
|
|
|
def == other
|
|
|
|
return false if other.nil?
|
2015-10-10 21:38:55 +03:00
|
|
|
return false if other.class != RegisterValue
|
2014-06-14 10:59:25 +03:00
|
|
|
symbol == other.symbol
|
|
|
|
end
|
|
|
|
|
|
|
|
#helper method to calculate with register symbols
|
2015-10-15 09:32:47 +03:00
|
|
|
def next_reg_use type , value = nil
|
2014-06-14 10:59:25 +03:00
|
|
|
int = @symbol[1,3].to_i
|
2015-11-07 21:55:04 +02:00
|
|
|
raise "No more registers #{self}" if int > 12
|
2015-10-10 19:14:27 +03:00
|
|
|
sym = "r#{int + 1}".to_sym
|
2015-10-15 09:32:47 +03:00
|
|
|
RegisterValue.new( sym , type, value)
|
2014-06-14 10:59:25 +03:00
|
|
|
end
|
2015-05-24 15:07:07 +03:00
|
|
|
|
2015-06-19 19:50:53 +03:00
|
|
|
def sof_reference_name
|
|
|
|
@symbol
|
|
|
|
end
|
2015-05-24 15:07:07 +03:00
|
|
|
|
2015-10-16 17:13:08 +03:00
|
|
|
def self.convert(unused)
|
|
|
|
unused
|
|
|
|
end
|
2014-06-14 10:59:25 +03:00
|
|
|
end
|
|
|
|
|
2015-06-30 09:38:32 +03:00
|
|
|
# Here we define the mapping from virtual machine objects, to register machine registers
|
|
|
|
#
|
2015-06-29 10:55:22 +03:00
|
|
|
|
2015-06-30 09:38:32 +03:00
|
|
|
# The register we use to store the current message object is :r0
|
|
|
|
def self.message_reg
|
2015-10-14 16:16:03 +03:00
|
|
|
RegisterValue.new :r0 , :Message
|
2015-06-30 09:38:32 +03:00
|
|
|
end
|
2015-06-29 10:55:22 +03:00
|
|
|
|
2015-06-30 09:38:32 +03:00
|
|
|
# The register we use to store the new message object is :r3
|
|
|
|
# The new message is the one being built, to be sent
|
2015-06-29 10:55:22 +03:00
|
|
|
def self.new_message_reg
|
2015-10-18 17:20:19 +03:00
|
|
|
RegisterValue.new :r1 , :Message
|
2015-06-29 10:55:22 +03:00
|
|
|
end
|
2015-06-30 09:38:32 +03:00
|
|
|
|
|
|
|
# The first scratch register. There is a next_reg_use to get a next and next.
|
|
|
|
# Current thinking is that scratch is schatch between instructions
|
2015-10-15 09:07:47 +03:00
|
|
|
def self.tmp_reg type , value = nil
|
2015-10-18 17:20:19 +03:00
|
|
|
RegisterValue.new :r2 , type , value
|
2015-06-29 10:55:22 +03:00
|
|
|
end
|
|
|
|
|
2015-06-30 09:38:32 +03:00
|
|
|
# The first arg is a class name (possibly lowercase) and the second an instance variable name.
|
2016-02-25 11:50:10 -08:00
|
|
|
# By looking up the class and the type for that class, we can resolve the instance
|
2015-06-30 09:38:32 +03:00
|
|
|
# variable name to an index.
|
|
|
|
# The class can be mapped to a register, and so we get a memory address (reg+index)
|
2015-06-29 10:55:22 +03:00
|
|
|
def self.resolve_index( clazz_name , instance_name )
|
|
|
|
return instance_name unless instance_name.is_a? Symbol
|
2015-06-29 20:57:16 +03:00
|
|
|
real_name = clazz_name.to_s.split('_').last.capitalize.to_sym
|
2015-06-29 10:55:22 +03:00
|
|
|
clazz = Parfait::Space.object_space.get_class_by_name(real_name)
|
|
|
|
raise "Class name not given #{real_name}" unless clazz
|
2016-02-25 12:16:13 -08:00
|
|
|
index = clazz.instance_type.variable_index( instance_name )
|
2015-06-29 20:57:16 +03:00
|
|
|
raise "Instance name=#{instance_name} not found on #{real_name}" unless index.is_a?(Numeric)
|
2016-02-25 11:50:10 -08:00
|
|
|
return index # the type word is at index 0, but type is a list and starts at 1 == type
|
2015-06-29 10:55:22 +03:00
|
|
|
end
|
|
|
|
|
2015-06-30 09:38:32 +03:00
|
|
|
# if a symbol is given, it may be one of the four objects that the vm knows.
|
|
|
|
# These are mapped to register references.
|
2016-12-21 18:51:22 +02:00
|
|
|
# The valid symbols (:message, :self,:named_list,:new_message) are the same that are returned
|
2015-06-30 09:38:32 +03:00
|
|
|
# by the slots. All data (at any time) is in one of the instance variables of these four
|
|
|
|
# objects. Register defines module methods with the same names (and _reg)
|
2015-06-29 10:55:22 +03:00
|
|
|
def self.resolve_to_register reference
|
|
|
|
register = reference
|
|
|
|
if reference.is_a? Symbol
|
|
|
|
case reference
|
|
|
|
when :message
|
|
|
|
register = message_reg
|
2015-06-29 20:57:16 +03:00
|
|
|
when :new_message
|
|
|
|
register = new_message_reg
|
2015-06-29 10:55:22 +03:00
|
|
|
else
|
|
|
|
raise "not recognized register reference #{reference}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return register
|
|
|
|
end
|
2015-08-07 16:46:55 +03:00
|
|
|
|
2014-06-14 10:59:25 +03:00
|
|
|
end
|