2017-04-12 10:53:02 +02:00
|
|
|
module Mom
|
|
|
|
|
2018-04-07 22:07:44 +02:00
|
|
|
# SlotLoad is for moving data into a slot, either from another slot, or constant
|
2017-08-29 17:28:25 +02:00
|
|
|
# A Slot is basically an instance variable, but it must be of known type
|
2017-04-12 13:45:02 +02:00
|
|
|
#
|
2018-03-15 16:03:38 +01:00
|
|
|
# The value loaded (the right hand side) can be a constant (Mom::Constant) or come from
|
|
|
|
# another Slot (SlotDefinition)
|
2017-04-12 13:45:02 +02:00
|
|
|
#
|
2018-03-15 16:03:38 +01:00
|
|
|
# The Slot on the left hand side is always a SlotDefinition.
|
2017-04-12 13:45:02 +02:00
|
|
|
# The only known object (*) for the left side is the current message, which is a bit like
|
2018-03-18 06:21:46 +01:00
|
|
|
# the oo version of a Stack (Stack Register, Frame Pointer, ..)
|
2017-04-12 13:45:02 +02:00
|
|
|
# (* off course all class objects are global, and so they are allowed too)
|
|
|
|
#
|
|
|
|
# A maybe not immediately obvious corrolar of this design is the total absence of
|
|
|
|
# general purpose instance variable accessors. Ie only inside an object's functions
|
|
|
|
# can a method access instance variables, because only inside the method is the type
|
|
|
|
# guaranteed.
|
|
|
|
# From the outside a send is neccessary, both for get and set, (which goes through the method
|
|
|
|
# resolution and guarantees the correct method for a type), in other words perfect data hiding.
|
|
|
|
#
|
2018-03-15 16:03:38 +01:00
|
|
|
# @left: A SlotDefinition, or an array that can be passed to the constructor of the
|
|
|
|
# SlotDefinition (see there)
|
2017-04-12 13:45:02 +02:00
|
|
|
#
|
2018-03-19 16:24:32 +01:00
|
|
|
# @right: A SlotDefinition with slots or a Mom::Constant
|
2018-03-22 17:45:03 +01:00
|
|
|
# original_source: optinally another mom instrucion that wil be passed down to created
|
|
|
|
# risc instructions. (Because SlotLoad is often used internally in mom)
|
2017-04-12 10:53:02 +02:00
|
|
|
class SlotLoad < Instruction
|
2018-03-22 17:45:03 +01:00
|
|
|
attr_reader :left , :right , :original_source
|
|
|
|
def initialize(left , right, original_source = nil)
|
2017-04-12 13:45:02 +02:00
|
|
|
@left , @right = left , right
|
2018-03-20 09:18:17 +01:00
|
|
|
@left = SlotDefinition.new(@left.shift , @left) if @left.is_a? Array
|
|
|
|
@right = SlotDefinition.new(@right.shift , @right) if @right.is_a? Array
|
|
|
|
raise "right not Mom, #{@right.to_s}" unless @right.is_a?( SlotDefinition )
|
2018-03-22 17:45:03 +01:00
|
|
|
@original_source = original_source || self
|
2017-04-12 13:45:02 +02:00
|
|
|
end
|
2018-03-15 16:03:38 +01:00
|
|
|
|
2018-04-17 19:26:15 +02:00
|
|
|
def to_s
|
|
|
|
"SlotLoad #{right} -> #{left}"
|
|
|
|
end
|
|
|
|
|
2018-03-17 06:43:44 +01:00
|
|
|
def to_risc(compiler)
|
2018-03-22 17:45:03 +01:00
|
|
|
const = @right.to_register(compiler , original_source)
|
2018-03-20 09:18:17 +01:00
|
|
|
left_slots = @left.slots
|
2018-03-17 17:02:09 +01:00
|
|
|
case @left.known_object
|
|
|
|
when Symbol
|
|
|
|
left = Risc.message_reg
|
2018-03-20 09:18:17 +01:00
|
|
|
left_index = Risc.resolve_to_index(@left.known_object , left_slots.first)
|
|
|
|
if left_slots.length > 1
|
2018-03-19 11:17:40 +01:00
|
|
|
# swap the existing target (with a new reg) and update the index
|
2018-03-31 18:17:55 +02:00
|
|
|
new_left = compiler.use_reg( :Object )
|
2018-03-22 17:45:03 +01:00
|
|
|
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
|
2018-03-19 11:17:40 +01:00
|
|
|
left = new_left
|
2018-04-05 19:10:00 +02:00
|
|
|
left_index = Risc.resolve_to_index(left_slots[0] , left_slots[1] ,compiler)
|
2018-03-20 19:01:20 +01:00
|
|
|
if left_slots.length > 2
|
|
|
|
#same again, once more updating target
|
2018-03-31 18:17:55 +02:00
|
|
|
new_left = compiler.use_reg( :Object )
|
2018-03-22 17:45:03 +01:00
|
|
|
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
|
2018-03-20 19:01:20 +01:00
|
|
|
left = new_left
|
2018-04-05 19:10:00 +02:00
|
|
|
left_index = Risc.resolve_to_index(left_slots[1] , left_slots[2] ,compiler)
|
2018-03-20 19:01:20 +01:00
|
|
|
end
|
|
|
|
raise "more slots not implemented #{left_slots}" if left_slots.length > 3
|
2018-03-19 11:17:40 +01:00
|
|
|
end
|
2018-03-17 17:02:09 +01:00
|
|
|
when Parfait::CacheEntry
|
2018-03-31 18:17:55 +02:00
|
|
|
left = compiler.use_reg( :Object )
|
2018-03-24 16:55:01 +01:00
|
|
|
const << Risc.load_constant(original_source, @left.known_object , left)
|
2018-03-20 09:18:17 +01:00
|
|
|
left_index = Risc.resolve_to_index(:cache_entry , left_slots.first)
|
2018-03-17 17:02:09 +01:00
|
|
|
else
|
|
|
|
raise "We have left #{@left.known_object}"
|
|
|
|
end
|
2018-03-22 17:45:03 +01:00
|
|
|
const << Risc.reg_to_slot(original_source, const.register , left, left_index)
|
2018-03-19 11:17:40 +01:00
|
|
|
compiler.reset_regs
|
2018-03-15 16:03:38 +01:00
|
|
|
return const
|
|
|
|
end
|
2017-04-12 10:53:02 +02:00
|
|
|
end
|
|
|
|
|
2017-09-04 20:31:49 +02:00
|
|
|
class SlotDefinition
|
|
|
|
attr_reader :known_object , :slots
|
2018-03-15 16:03:38 +01:00
|
|
|
# is an array of symbols, that specifies the first the object, and then the Slot.
|
|
|
|
# The first element is either a known type name (Capitalized symbol of the class name) ,
|
|
|
|
# or the symbol :message
|
|
|
|
# And subsequent symbols must be instance variables on the previous type.
|
2018-03-19 11:17:40 +01:00
|
|
|
# Examples: [:message , :receiver] or [:Space , :next_message]
|
2017-09-04 20:31:49 +02:00
|
|
|
def initialize( object , slots)
|
|
|
|
@known_object , @slots = object , slots
|
|
|
|
slot = [slot] unless slot.is_a?(Array)
|
2018-03-17 17:02:09 +01:00
|
|
|
raise "Not known #{slots}" unless object
|
2018-03-22 18:14:22 +01:00
|
|
|
raise "No slots #{object}" unless slots
|
2017-09-04 20:31:49 +02:00
|
|
|
end
|
2018-03-19 16:24:32 +01:00
|
|
|
|
2018-04-17 19:26:15 +02:00
|
|
|
def to_s
|
|
|
|
names = [known_name] + @slots
|
|
|
|
"[#{names.join(',')}]"
|
|
|
|
end
|
|
|
|
|
|
|
|
def known_name
|
|
|
|
case known_object
|
|
|
|
when Constant , Parfait::Object
|
|
|
|
known_object.class.short_name
|
|
|
|
when Risc::Label
|
|
|
|
known_object.to_s
|
|
|
|
when Symbol
|
|
|
|
known_object
|
|
|
|
else
|
|
|
|
"unknown"
|
|
|
|
end
|
|
|
|
end
|
2018-03-19 16:24:32 +01:00
|
|
|
def to_register(compiler, instruction)
|
2018-03-31 18:17:55 +02:00
|
|
|
type = known_object.respond_to?(:ct_type) ? known_object.ct_type : :Object
|
2018-03-19 16:24:32 +01:00
|
|
|
right = compiler.use_reg( type )
|
2018-03-20 09:18:17 +01:00
|
|
|
case known_object
|
2018-03-31 12:25:59 +02:00
|
|
|
when Constant
|
|
|
|
parfait = known_object.to_parfait(compiler)
|
|
|
|
const = Risc.load_constant(instruction, parfait , right)
|
|
|
|
raise "Can't have slots into Constants" if slots.length > 0
|
|
|
|
when Parfait::Object , Risc::Label
|
2018-03-21 22:08:06 +01:00
|
|
|
const = Risc.load_constant(instruction, known_object , right)
|
2018-03-22 18:14:22 +01:00
|
|
|
if slots.length > 0
|
|
|
|
# desctructively replace the existing value to be loaded if more slots
|
2018-04-05 19:10:00 +02:00
|
|
|
index = Risc.resolve_to_index(known_object , slots[0] ,compiler)
|
2018-03-22 18:14:22 +01:00
|
|
|
const << Risc::SlotToReg.new( instruction , right ,index, right)
|
|
|
|
end
|
2018-03-19 16:24:32 +01:00
|
|
|
when Symbol
|
2018-03-20 09:18:17 +01:00
|
|
|
const = Risc::SlotToReg.new( instruction , Risc.resolve_to_register(known_object) ,
|
|
|
|
Risc.resolve_to_index(:message , slots[0]), right)
|
2018-03-19 16:24:32 +01:00
|
|
|
else
|
2018-03-20 09:18:17 +01:00
|
|
|
raise "We have a #{self} #{known_object}"
|
2018-03-19 16:24:32 +01:00
|
|
|
end
|
2018-03-22 17:54:07 +01:00
|
|
|
if slots.length > 1
|
|
|
|
# desctructively replace the existing value to be loaded if more slots
|
2018-04-05 19:10:00 +02:00
|
|
|
index = Risc.resolve_to_index(slots[0] , slots[1] ,compiler)
|
2018-03-22 17:54:07 +01:00
|
|
|
const << Risc::SlotToReg.new( instruction , right ,index, right)
|
2018-04-08 21:59:42 +02:00
|
|
|
if slots.length > 2
|
|
|
|
raise "3 slots only for type #{slots}" unless slots[2] == :type
|
|
|
|
const << Risc::SlotToReg.new( instruction , right , 1, right)
|
|
|
|
end
|
2018-03-22 17:54:07 +01:00
|
|
|
end
|
2018-03-19 16:24:32 +01:00
|
|
|
const
|
|
|
|
end
|
2017-09-04 20:31:49 +02:00
|
|
|
end
|
2017-04-12 10:53:02 +02:00
|
|
|
end
|