2020-02-10 13:28:48 +01:00
|
|
|
module SlotMachine
|
2020-02-17 08:26:50 +01:00
|
|
|
class SlottedObject < Slotted
|
2020-02-10 13:28:48 +01:00
|
|
|
|
2020-02-15 09:26:49 +01:00
|
|
|
attr_reader :known_object
|
|
|
|
|
2020-02-10 13:28:48 +01:00
|
|
|
def initialize( object , slots)
|
2020-02-15 09:26:49 +01:00
|
|
|
super(slots)
|
|
|
|
@known_object = object
|
|
|
|
raise "Not known #{slots}" unless object
|
2020-02-10 13:28:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def known_name
|
|
|
|
known_object.class.short_name
|
|
|
|
end
|
|
|
|
|
|
|
|
# load the slots into a register
|
|
|
|
# the code is added to compiler
|
|
|
|
# the register returned
|
|
|
|
def to_register(compiler, source)
|
|
|
|
type = known_object.get_type
|
2020-02-27 10:57:18 +01:00
|
|
|
raise "not sym for #{known_object}" if type.is_a?(String)
|
2020-02-29 16:19:53 +01:00
|
|
|
last = Risc.load_constant(source, known_object)
|
|
|
|
compiler.add_code last
|
2020-02-17 08:29:45 +01:00
|
|
|
if slots_length > 1
|
2020-02-29 16:19:53 +01:00
|
|
|
last = Risc.slot_to_reg( source , last.register ,slots.name)
|
|
|
|
compiler.add_code(last)
|
2020-02-10 13:28:48 +01:00
|
|
|
end
|
2020-02-17 08:29:45 +01:00
|
|
|
if slots_length > 2
|
2020-02-29 16:19:53 +01:00
|
|
|
last = Risc.slot_to_reg( source , last.register , slots.next_slot.name )
|
|
|
|
compiler.add_code(last)
|
2020-02-10 13:28:48 +01:00
|
|
|
end
|
2020-02-29 16:19:53 +01:00
|
|
|
if slots_length > 3
|
|
|
|
raise "3 slots only for type #{slots}" unless slots.next_slot.next_slot.name == :type
|
|
|
|
last = Risc.slot_to_reg( source , last.register , slots.next_slot.name )
|
|
|
|
compiler.add_code(last)
|
|
|
|
end
|
|
|
|
return last.register
|
2020-02-10 13:28:48 +01:00
|
|
|
end
|
|
|
|
|
2020-02-11 10:03:51 +01:00
|
|
|
# Note: this is the left hand case, the right hand being to_register
|
|
|
|
# They are very similar (apart from the final reg_to_slot here) and should
|
|
|
|
# most likely be united
|
|
|
|
def reduce_and_load(const_reg , compiler , original_source )
|
|
|
|
raise "only cache" unless known_object.is_a?( Parfait::CacheEntry)
|
2020-03-01 15:41:58 +01:00
|
|
|
load = Risc.load_constant(original_source, known_object )
|
|
|
|
left = load.register
|
|
|
|
compiler.add_code load
|
2020-02-17 08:29:45 +01:00
|
|
|
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slots.name)
|
2020-02-11 10:03:51 +01:00
|
|
|
end
|
2020-02-10 13:28:48 +01:00
|
|
|
end
|
|
|
|
end
|