rubyx/lib/register/instructions/set_slot.rb

47 lines
1.6 KiB
Ruby
Raw Normal View History

module Register
# SetSlot moves data into memory from a register.
# GetSlot moves data into a register from memory.
# Both use a base memory (a register)
# While the virtual machine has only one instruction (Set) to move data between slots,
# the register has two, namely GetSlot and SetSlot
#
# This is because that is what cpu's can do. In programming terms this would be accessing
# an element in an array, in the case of SetSlot setting the register in the array.
# btw: to move data between registers, use RegisterTransfer
class SetSlot < Instruction
# If you had a c array and index offset
# the instruction would do array[index] = register
2015-06-29 19:56:11 +02:00
# So SetSlot means the register (first argument) moves to the slot (array and index)
def initialize source , register , array , index
super(source)
@register = register
@array = array
@index = index
2015-06-29 19:56:11 +02:00
raise "not integer #{index}" unless index.is_a? Numeric
raise "Not register #{register}" unless Register::RegisterValue.look_like_reg(register)
raise "Not register #{array}" unless Register::RegisterValue.look_like_reg(array)
end
attr_accessor :register , :array , :index
2015-07-24 12:23:56 +02:00
def to_s
2015-07-25 08:30:58 +02:00
"SetSlot: #{register} -> #{array} [#{index}]"
2015-07-24 12:23:56 +02:00
end
end
# Produce a SetSlot instruction.
# From and to are registers or symbols that can be transformed to a register by resolve_to_register
# index resolves with resolve_index.
def self.set_slot source , from , to , index
from = resolve_to_register from
index = resolve_index( to , index)
to = resolve_to_register to
SetSlot.new( source, from , to , index)
end
end