rubyx/lib/slot_machine/instruction/slot_load.rb

85 lines
3.5 KiB
Ruby
Raw Normal View History

2019-10-03 20:07:55 +02:00
module SlotMachine
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
# another Slot (SlotDefinition)
2017-04-12 13:45:02 +02: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
# 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
# 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.
#
# @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
#
2019-10-03 20:07:55 +02:00
# @right: A SlotDefinition with slots or a SlotMachine::Constant
# original_source: optinally another slot_machine instruction that will be passed down
# to created risc instructions. (Because SlotLoad is often used internally)
class SlotLoad < Instruction
2018-11-14 11:41:13 +01:00
attr_reader :left , :right , :original_source
2018-11-14 11:41:13 +01:00
def initialize(source , left , right, original_source = nil)
super(source)
2017-04-12 13:45:02 +02:00
@left , @right = left , right
@left = SlotDefinition.for(@left.shift , @left) if @left.is_a? Array
@right = SlotDefinition.for(@right.shift , @right) if @right.is_a? Array
2019-10-03 20:07:55 +02:00
raise "right not SlotMachine, #{@right.to_s}" unless @right.is_a?( SlotDefinition )
@original_source = original_source || self
2017-04-12 13:45:02 +02:00
end
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
def to_risc(compiler)
const_reg = @right.to_register(compiler , original_source)
2018-03-20 09:18:17 +01:00
left_slots = @left.slots
case @left.known_object
when Symbol
sym_to_risc(compiler , const_reg)
when Parfait::CacheEntry
left = compiler.use_reg( :CacheEntry )
compiler.add_code Risc.load_constant(original_source, @left.known_object , left)
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, left_slots.first)
else
raise "We have left #{@left.known_object}"
end
compiler.reset_regs
end
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
def sym_to_risc(compiler , const_reg)
left_slots = @left.slots.dup
raise "Not Message #{object}" unless @left.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
require_relative "slot_definition"
require_relative "message_definition"