From 5ce7b6c7c965e089c679e1fd556bbecbf35bdc74 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 29 Jun 2015 10:55:22 +0300 Subject: [PATCH] removing those ugly slot index constants The constants were bad enough, but they were also at the wrong level Now register level is defining mappings from symbol names to indexes, by using the layout --- lib/register/register_reference.rb | 79 ++++++++++++++++++-------- lib/virtual/slots/frame_slot.rb | 4 +- lib/virtual/slots/message_slot.rb | 27 +++------ lib/virtual/slots/next_message_slot.rb | 14 ++--- lib/virtual/slots/self_slot.rb | 2 +- lib/virtual/slots/slot.rb | 8 +-- 6 files changed, 77 insertions(+), 57 deletions(-) diff --git a/lib/register/register_reference.rb b/lib/register/register_reference.rb index d6c9ee83..6748cb32 100644 --- a/lib/register/register_reference.rb +++ b/lib/register/register_reference.rb @@ -48,33 +48,66 @@ module Register RegisterReference.new( sym ) end - MESSAGE_REGISTER = :r0 - SELF_REGISTER = :r1 - FRAME_REGISTER = :r2 - NEW_MESSAGE_REGISTER = :r3 - - TMP_REGISTER = :r4 - - def self.self_reg - new SELF_REGISTER - end - def self.message_reg - new MESSAGE_REGISTER - end - def self.frame_reg - new FRAME_REGISTER - end - def self.new_message_reg - new NEW_MESSAGE_REGISTER - end - def self.tmp_reg - new TMP_REGISTER - end - def sof_reference_name @symbol end end + MESSAGE_REGISTER = :r0 + SELF_REGISTER = :r1 + FRAME_REGISTER = :r2 + NEW_MESSAGE_REGISTER = :r3 + + TMP_REGISTER = :r4 + + def self.self_reg + RegisterReference.new SELF_REGISTER + end + def self.message_reg + RegisterReference.new MESSAGE_REGISTER + end + def self.frame_reg + RegisterReference.new FRAME_REGISTER + end + def self.new_message_reg + RegisterReference.new NEW_MESSAGE_REGISTER + end + def self.tmp_reg + RegisterReference.new TMP_REGISTER + end + + def self.get_slot from , index , to + index = resolve_index( from , index) + from = resolve_to_register from + to = resolve_to_register to + GetSlot.new( from , index , to) + end + + def self.resolve_index( clazz_name , instance_name ) + return instance_name unless instance_name.is_a? Symbol + real_name = "#{clazz_name}".capitalize.to_sym + clazz = Parfait::Space.object_space.get_class_by_name(real_name) + raise "Class name not given #{real_name}" unless clazz + index = clazz.object_layout.index_of( instance_name ) + raise "Instance name=#{instance_name} not found on #{real_name}" unless index + return index + end + + def self.resolve_to_register reference + register = reference + if reference.is_a? Symbol + case reference + when :message + register = message_reg + when :self + register = self_reg + when :frame + register = frame_reg + else + raise "not recognized register reference #{reference}" + end + end + return register + end end diff --git a/lib/virtual/slots/frame_slot.rb b/lib/virtual/slots/frame_slot.rb index e8ce6076..d237ed17 100644 --- a/lib/virtual/slots/frame_slot.rb +++ b/lib/virtual/slots/frame_slot.rb @@ -2,9 +2,9 @@ module Virtual # Slots in the Frame a re represented by instances of FrameSlot - # Slots in the Frame are local or temporary varialbes in a message + # Slots in the Frame are local or temporary variables in a message class FrameSlot < Slot - def initialize index , type = Unknown, value = nil + def initialize type = Unknown, value = nil super end end diff --git a/lib/virtual/slots/message_slot.rb b/lib/virtual/slots/message_slot.rb index 13a001a0..11a23ee0 100644 --- a/lib/virtual/slots/message_slot.rb +++ b/lib/virtual/slots/message_slot.rb @@ -1,18 +1,5 @@ module Virtual - #TODO : this constant approach is a bit old, from before PArfait adapter - # nowadays these are unneccessary as we can resolve the names by using the - # layout of the class. (get Class from space) - TYPE_INDEX = 0 - LAYOUT_INDEX = 1 - CALLER_INDEX = 2 - RETURN_INDEX = 3 - EXCEPTION_INDEX = 4 - SELF_INDEX = 5 - NAME_INDEX = 6 - FRAME_INDEX = 7 - ARGUMENT_START = 8 - # The current Message is one of four objects the virtual machine knows # # Slots represent instance variables of objects, so MessageSlots @@ -20,31 +7,31 @@ module Virtual # The Message has a layout as per the constant above class MessageSlot < Slot - def initialize index , type = Unknown , value = nil - super(index ,type , value ) + def initialize type = Unknown , value = nil + super(type , value ) end end # named classes exist for slots that often accessed - # Return is the MessageSlot(RETURN_INDEX) + # Return is the return of MessageSlot class Return < MessageSlot def initialize type = Unknown, value = nil - super( RETURN_INDEX , type , value ) + super( type , value ) end end - # Self is the MessageSlot(SELF_INDEX) + # Self is the self in MessageSlot class Self < MessageSlot def initialize type = Unknown, value = nil - super( SELF_INDEX , type , value ) + super( type , value ) end end # MessageName of the current message class MessageName < MessageSlot def initialize type = Unknown, value = nil - super( NAME_INDEX , type , value ) + super( type , value ) end end end diff --git a/lib/virtual/slots/next_message_slot.rb b/lib/virtual/slots/next_message_slot.rb index 3e0aa217..b2471244 100644 --- a/lib/virtual/slots/next_message_slot.rb +++ b/lib/virtual/slots/next_message_slot.rb @@ -7,31 +7,31 @@ module Virtual # The Message has a layout as per the constant above class NextMessageSlot < Slot - def initialize index , type = Unknown, value = nil - super(index , type , value ) + def initialize type = Unknown, value = nil + super( type , value ) end end # named classes exist for slots that often accessed - # NextReturn is the NextMessageSlot(RETURN_INDEX) + # NextReturn is the return of NextMessageSlot class NextReturn < NextMessageSlot def initialize type = Unknown, value = nil - super( RETURN_INDEX, type , value ) + super( type , value ) end end - # NextSelf is the NextMessageSlot(SELF_INDEX) + # NextSelf is the self of NextMessageSlot class NextSelf < NextMessageSlot def initialize type = Unknown, value = nil - super( SELF_INDEX , type , value ) + super( type , value ) end end # NextMessageName of the next message class NextMessageName < NextMessageSlot def initialize type = Unknown, value = nil - super( NAME_INDEX, type , value ) + super( type , value ) end end end diff --git a/lib/virtual/slots/self_slot.rb b/lib/virtual/slots/self_slot.rb index d0b38987..4133bd12 100644 --- a/lib/virtual/slots/self_slot.rb +++ b/lib/virtual/slots/self_slot.rb @@ -14,7 +14,7 @@ module Virtual # that object # class SelfSlot < Slot - def initialize index , type = Unknown, value = nil + def initialize type = Unknown, value = nil super end end diff --git a/lib/virtual/slots/slot.rb b/lib/virtual/slots/slot.rb index 19bee9e5..2f585394 100644 --- a/lib/virtual/slots/slot.rb +++ b/lib/virtual/slots/slot.rb @@ -2,7 +2,8 @@ module Virtual # A slot is a slot in an object. It is the storage location for a value. # (Remember, values are typed) # From a memory perspective a slot is an index into an array (the object) - # We are not modelling the array here, but the index into it. + # The mapping into arrays is a straightforward matter, but happens in the + # next level down, the register machine. # Four known objects exist and those correspond to subclasses: # - the message that has been received: MessageSlot @@ -17,12 +18,11 @@ module Virtual class Slot < Object - attr_accessor :index , :type , :value + attr_accessor :type , :value private #abstract base class - def initialize index , type , value - @index = index + def initialize type , value @type = type @value = value end