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
|
2018-05-15 18:29:06 +02:00
|
|
|
# general external instance variable accessors. Ie only inside an object's functions
|
2017-04-12 13:45:02 +02:00
|
|
|
# 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-07-13 20:56:55 +02:00
|
|
|
# original_source: optinally another mom instruction that wil be passed down to created
|
2018-03-22 17:45:03 +01:00
|
|
|
# risc instructions. (Because SlotLoad is often used internally in mom)
|
2017-04-12 10:53:02 +02:00
|
|
|
class SlotLoad < Instruction
|
2018-11-14 11:41:13 +01:00
|
|
|
|
2018-03-22 17:45:03 +01:00
|
|
|
attr_reader :left , :right , :original_source
|
2018-11-14 11:41:13 +01:00
|
|
|
|
2018-03-22 17:45:03 +01:00
|
|
|
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-11-14 11:41:13 +01:00
|
|
|
# resolve the SlotLoad to the respective risc Instructions.
|
|
|
|
# calls sym_to_risc for most (symbols), and ConstantLoad for CacheEntry
|
|
|
|
# after loading the right into register
|
2018-03-17 06:43:44 +01:00
|
|
|
def to_risc(compiler)
|
2018-08-19 12:06:00 +02:00
|
|
|
const_reg = @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
|
2018-08-19 12:06:00 +02:00
|
|
|
sym_to_risc(compiler , const_reg)
|
2018-03-17 17:02:09 +01:00
|
|
|
when Parfait::CacheEntry
|
2018-07-16 18:00:04 +02:00
|
|
|
left = compiler.use_reg( :CacheEntry )
|
2018-08-19 12:06:00 +02:00
|
|
|
compiler.add_code Risc.load_constant(original_source, @left.known_object , left)
|
2018-08-19 14:36:51 +02:00
|
|
|
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, left_slots.first)
|
2018-03-17 17:02:09 +01:00
|
|
|
else
|
|
|
|
raise "We have left #{@left.known_object}"
|
|
|
|
end
|
2018-03-19 11:17:40 +01:00
|
|
|
compiler.reset_regs
|
2018-03-15 16:03:38 +01:00
|
|
|
end
|
2018-07-13 20:56:55 +02:00
|
|
|
|
2018-11-14 11:41:13 +01:00
|
|
|
# load the data in const_reg into the slot that is named by left symbols
|
|
|
|
# left may usually be only 3 long, as the first is known, then the second is loaded
|
|
|
|
# with type known type (as it comes from message)
|
|
|
|
#
|
|
|
|
# actual lifting is done by RegisterValue resolve_and_add
|
2018-08-19 12:06:00 +02:00
|
|
|
def sym_to_risc(compiler , const_reg)
|
2018-07-14 21:39:00 +02:00
|
|
|
left_slots = @left.slots.dup
|
2018-07-13 20:56:55 +02:00
|
|
|
raise "Not Message #{object}" unless @left.known_object == :message
|
|
|
|
left = Risc.message_reg
|
|
|
|
slot = left_slots.shift
|
2018-07-14 10:04:21 +02:00
|
|
|
while( !left_slots.empty? )
|
2018-08-19 12:06:00 +02:00
|
|
|
left = left.resolve_and_add( slot , compiler)
|
2018-07-14 10:04:21 +02:00
|
|
|
slot = left_slots.shift
|
2018-07-13 20:56:55 +02:00
|
|
|
end
|
2018-08-19 12:06:00 +02:00
|
|
|
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slot)
|
2018-07-13 20:56:55 +02:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:53:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2018-05-15 18:29:06 +02:00
|
|
|
require_relative "slot_definition"
|