2019-10-03 20:07:55 +02:00
|
|
|
module SlotMachine
|
2017-04-12 10:53:02 +02:00
|
|
|
|
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
|
|
|
#
|
2019-10-03 20:07:55 +02:00
|
|
|
# The value loaded (the right hand side) can be a constant (SlotMachine::Constant) or come from
|
2020-02-11 10:19:52 +01:00
|
|
|
# another Slot (Slot)
|
2017-04-12 13:45:02 +02:00
|
|
|
#
|
2020-02-11 10:19:52 +01:00
|
|
|
# The Slot on the left hand side is always a Slot.
|
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.
|
|
|
|
#
|
2020-02-11 10:19:52 +01:00
|
|
|
# @left: A Slot, or an array that can be passed to the constructor of the
|
|
|
|
# Slot (see there)
|
2017-04-12 13:45:02 +02:00
|
|
|
#
|
2020-02-11 10:19:52 +01:00
|
|
|
# @right: A Slot with slots or a SlotMachine::Constant
|
2019-10-03 20:07:55 +02:00
|
|
|
# original_source: optinally another slot_machine instruction that will be passed down
|
|
|
|
# to created risc instructions. (Because SlotLoad is often used internally)
|
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
|
|
|
|
2019-08-07 11:06:06 +02:00
|
|
|
def initialize(source , left , right, original_source = nil)
|
|
|
|
super(source)
|
2017-04-12 13:45:02 +02:00
|
|
|
@left , @right = left , right
|
2020-02-15 15:02:03 +01:00
|
|
|
@left = Slotted.for(@left.shift , @left) if @left.is_a? Array
|
|
|
|
@right = Slotted.for(@right.shift , @right) if @right.is_a? Array
|
2020-02-17 08:26:50 +01:00
|
|
|
raise "right not SlotMachine, #{@right.to_s}" unless @right.is_a?( Slotted )
|
|
|
|
raise "left not SlotMachine, #{@left.to_s}" unless @left.is_a?( Slotted )
|
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)
|
2020-02-11 10:03:51 +01:00
|
|
|
@left.reduce_and_load(const_reg , compiler , original_source )
|
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
|
|
|
|
2017-04-12 10:53:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|