move slot out of instruction dir
since it is not an instruction, just a helper also doing this before rewriting slot recursively
This commit is contained in:
@ -1,49 +0,0 @@
|
||||
module SlotMachine
|
||||
class ConstantSlot < Slot
|
||||
# get the right definition, depending on the object
|
||||
def self.for(object , slots)
|
||||
case object
|
||||
when :message
|
||||
MessageSlot.new(slots)
|
||||
when Constant
|
||||
ConstantSlot.new(object , slots)
|
||||
else
|
||||
Slot.new(object,slots)
|
||||
end
|
||||
end
|
||||
|
||||
def initialize( object , slots)
|
||||
super(object, slots)
|
||||
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)
|
||||
if known_object.respond_to?(:ct_type)
|
||||
type = known_object.ct_type
|
||||
else
|
||||
type = :Object
|
||||
end
|
||||
right = compiler.use_reg( type )
|
||||
case known_object
|
||||
when Constant
|
||||
parfait = known_object.to_parfait(compiler)
|
||||
const = Risc.load_constant(source, parfait , right)
|
||||
compiler.add_code const
|
||||
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
|
||||
end
|
||||
return const.register
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -1,47 +0,0 @@
|
||||
module SlotMachine
|
||||
class MessageSlot < Slot
|
||||
|
||||
def initialize(slots)
|
||||
super(:message , slots)
|
||||
end
|
||||
|
||||
def known_name
|
||||
:message
|
||||
end
|
||||
|
||||
# load the slots into a register
|
||||
# the code is added to compiler
|
||||
# the register returned
|
||||
def to_register(compiler, source)
|
||||
type = :Message
|
||||
right = compiler.use_reg( type )
|
||||
slots = @slots.dup
|
||||
left = Risc.message_reg
|
||||
left = left.resolve_and_add( slots.shift , compiler)
|
||||
reg = compiler.current.register
|
||||
while( !slots.empty? )
|
||||
left = left.resolve_and_add( slots.shift , compiler)
|
||||
end
|
||||
return reg
|
||||
end
|
||||
|
||||
# load the data in const_reg into the slot that is named by slot symbols
|
||||
# actual lifting is done by RegisterValue resolve_and_add
|
||||
#
|
||||
# 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 )
|
||||
left_slots = slots.dup
|
||||
raise "Not Message #{object}" unless known_object == :message
|
||||
left = Risc.message_reg
|
||||
slot = left_slots.shift
|
||||
while( !left_slots.empty? )
|
||||
left = left.resolve_and_add( slot , compiler)
|
||||
slot = left_slots.shift
|
||||
end
|
||||
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slot)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -1,46 +0,0 @@
|
||||
module SlotMachine
|
||||
class ObjectSlot < Slot
|
||||
|
||||
def initialize( object , slots)
|
||||
super(object , slots )
|
||||
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
|
||||
right = compiler.use_reg( type )
|
||||
const = Risc.load_constant(source, known_object , right)
|
||||
compiler.add_code const
|
||||
if slots.length > 0
|
||||
# desctructively replace the existing value to be loaded if more slots
|
||||
compiler.add_code Risc.slot_to_reg( source , right ,slots[0], right)
|
||||
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)
|
||||
compiler.add_code Risc::SlotToReg.new( source , right ,index, right)
|
||||
if slots.length > 2
|
||||
raise "3 slots only for type #{slots}" unless slots[2] == :type
|
||||
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
|
||||
end
|
||||
end
|
||||
return const.register
|
||||
end
|
||||
|
||||
# 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)
|
||||
left = compiler.use_reg( :CacheEntry )
|
||||
compiler.add_code Risc.load_constant(original_source, known_object , left)
|
||||
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slots.first)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,52 +0,0 @@
|
||||
module SlotMachine
|
||||
# A Slot defines a slot. A bit like a variable name but for objects.
|
||||
#
|
||||
# PS: for the interested: A "development" of Smalltalk was the
|
||||
# prototype based language (read: JavaScript equivalent)
|
||||
# called Self https://en.wikipedia.org/wiki/Self_(programming_language)
|
||||
#
|
||||
# Slots 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 Slot
|
||||
# get the right definition, depending on the object
|
||||
def self.for(object , slots)
|
||||
case object
|
||||
when :message
|
||||
MessageSlot.new(slots)
|
||||
when Constant
|
||||
ConstantSlot.new(object , slots)
|
||||
when Parfait::Object , Risc::Label
|
||||
ObjectSlot.new(object , slots)
|
||||
else
|
||||
raise "not supported type #{object}"
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
"[#{names.join(', ')}]"
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
@ -54,7 +54,3 @@ module SlotMachine
|
||||
end
|
||||
|
||||
end
|
||||
require_relative "slot"
|
||||
require_relative "message_slot"
|
||||
require_relative "constant_slot"
|
||||
require_relative "object_slot"
|
||||
|
Reference in New Issue
Block a user