2018-05-15 18:29:06 +02:00
|
|
|
module Mom
|
|
|
|
# A SlotDefinition defines a slot. A bit like a variable name but for objects.
|
|
|
|
#
|
|
|
|
# PS: for the interested: A "developement" of Smalltalk was the
|
|
|
|
# prototype based language (read: JavaScript equivalent)
|
|
|
|
# called Self https://en.wikipedia.org/wiki/Self_(programming_language)
|
|
|
|
#
|
|
|
|
# SlotDefinitions are the instance names of objects. But since the language is dynamic
|
|
|
|
# what is it that we can say about instance names at runtime?
|
|
|
|
# Start with a known object like the Message (in register one), we know all it's
|
|
|
|
# variables. But there is a Message in there, and for that we know the instances
|
|
|
|
# too. And off course for _all_ objects we know where the type is.
|
|
|
|
#
|
|
|
|
# The definiion is an array of symbols that we can resolve to SlotLoad
|
|
|
|
# Instructions. Or in the case of constants to ConstantLoad
|
|
|
|
#
|
|
|
|
class SlotDefinition
|
|
|
|
attr_reader :known_object , :slots
|
|
|
|
# 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.
|
|
|
|
# Examples: [:message , :receiver] or [:Space , :next_message]
|
|
|
|
def initialize( object , slots)
|
|
|
|
raise "No slots #{object}" unless slots
|
|
|
|
slots = [slots] unless slots.is_a?(Array)
|
|
|
|
@known_object , @slots = object , slots
|
|
|
|
raise "Not known #{slots}" unless object
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
names = [known_name] + @slots
|
2018-07-03 18:15:36 +02:00
|
|
|
"[#{names.join(', ')}]"
|
2018-05-15 18:29:06 +02:00
|
|
|
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-08-19 11:56:44 +02:00
|
|
|
|
|
|
|
# load the slots into a register
|
|
|
|
# the code is added to compiler
|
|
|
|
# the register returned
|
2018-07-14 21:39:00 +02:00
|
|
|
def to_register(compiler, source)
|
2018-07-16 18:00:04 +02:00
|
|
|
if known_object.respond_to?(:ct_type)
|
|
|
|
type = known_object.ct_type
|
|
|
|
elsif(known_object.respond_to?(:get_type))
|
|
|
|
type = known_object.get_type
|
|
|
|
else
|
|
|
|
type = :Object
|
|
|
|
end
|
2018-05-15 18:29:06 +02:00
|
|
|
right = compiler.use_reg( type )
|
|
|
|
case known_object
|
|
|
|
when Constant
|
|
|
|
parfait = known_object.to_parfait(compiler)
|
2018-08-19 11:56:44 +02:00
|
|
|
const = Risc.load_constant(source, parfait , right)
|
|
|
|
compiler.add_code const
|
2019-08-18 19:35:01 +02:00
|
|
|
if slots.length == 1
|
|
|
|
raise "only type allowed for constants, not #{slots[0]}" unless slots[0] == :type
|
|
|
|
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
|
|
|
|
end
|
|
|
|
raise "Can't have slots into Constants #{slots}" if slots.length > 1
|
2018-05-15 18:29:06 +02:00
|
|
|
when Parfait::Object , Risc::Label
|
2018-08-19 14:36:51 +02:00
|
|
|
const = const = Risc.load_constant(source, known_object , right)
|
|
|
|
compiler.add_code const
|
2018-05-15 18:29:06 +02:00
|
|
|
if slots.length > 0
|
|
|
|
# desctructively replace the existing value to be loaded if more slots
|
2018-08-19 11:56:44 +02:00
|
|
|
compiler.add_code Risc.slot_to_reg( source , right ,slots[0], right)
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
when Symbol
|
2018-07-14 21:39:00 +02:00
|
|
|
return sym_to_risc(compiler , source)
|
2018-05-15 18:29:06 +02:00
|
|
|
else
|
|
|
|
raise "We have a #{self} #{known_object}"
|
|
|
|
end
|
|
|
|
if slots.length > 1
|
|
|
|
# desctructively replace the existing value to be loaded if more slots
|
|
|
|
index = Risc.resolve_to_index(slots[0] , slots[1] ,compiler)
|
2018-08-19 11:56:44 +02:00
|
|
|
compiler.add_code Risc::SlotToReg.new( source , right ,index, right)
|
2018-05-15 18:29:06 +02:00
|
|
|
if slots.length > 2
|
|
|
|
raise "3 slots only for type #{slots}" unless slots[2] == :type
|
2018-08-19 11:56:44 +02:00
|
|
|
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
end
|
2018-08-19 11:56:44 +02:00
|
|
|
return const.register
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
2018-07-14 21:39:00 +02:00
|
|
|
|
|
|
|
# resolve the slots one by one to slot_to_reg instructions using the
|
|
|
|
# type information inferred from their names / type hierachy
|
|
|
|
def sym_to_risc(compiler , source)
|
|
|
|
slots = @slots.dup
|
|
|
|
raise "Not Message #{object}" unless @known_object == :message
|
|
|
|
left = Risc.message_reg
|
2018-08-19 11:56:44 +02:00
|
|
|
left = left.resolve_and_add( slots.shift , compiler)
|
|
|
|
reg = compiler.current.register
|
2018-07-14 21:39:00 +02:00
|
|
|
while( !slots.empty? )
|
2018-08-19 11:56:44 +02:00
|
|
|
left = left.resolve_and_add( slots.shift , compiler)
|
2018-07-14 21:39:00 +02:00
|
|
|
end
|
2018-08-19 11:56:44 +02:00
|
|
|
return reg
|
2018-07-14 21:39:00 +02:00
|
|
|
end
|
|
|
|
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
end
|