2014-10-03 10:07:18 +02:00
|
|
|
module Register
|
2015-06-21 20:00:16 +02:00
|
|
|
|
|
|
|
# GetSlot moves data into a register from memory.
|
|
|
|
# SetSlot moves data into memory from a register.
|
|
|
|
# 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
|
2014-10-03 10:07:18 +02:00
|
|
|
#
|
2015-06-21 20:00:16 +02:00
|
|
|
# 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 GetSlot setting the value in the array.
|
|
|
|
|
|
|
|
# btw: to move data between registers, use RegisterTransfer
|
|
|
|
|
2014-10-03 10:07:18 +02:00
|
|
|
class GetSlot < Instruction
|
2015-06-21 20:00:16 +02:00
|
|
|
|
|
|
|
# If you had a c array and index offset
|
|
|
|
# the instruction would do register = array[index]
|
2015-10-13 16:30:39 +02:00
|
|
|
# The arguments are in the order that makes sense for the Instruction name
|
2015-06-21 20:00:16 +02:00
|
|
|
# So GetSlot means the slot (array and index) moves to the register (last argument)
|
2015-07-27 11:13:39 +02:00
|
|
|
def initialize source , array , index , register
|
|
|
|
super(source)
|
2015-06-21 20:00:16 +02:00
|
|
|
@array = array
|
2014-10-03 10:07:18 +02:00
|
|
|
@index = index
|
2015-07-27 11:13:39 +02:00
|
|
|
@register = register
|
2015-11-07 16:34:41 +01:00
|
|
|
raise "index 0 " if index == 0
|
|
|
|
raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index)
|
2015-10-22 17:16:29 +02:00
|
|
|
raise "Not register #{register}" unless RegisterValue.look_like_reg(register)
|
|
|
|
raise "Not register #{array}" unless RegisterValue.look_like_reg(array)
|
2014-10-03 10:07:18 +02:00
|
|
|
end
|
2015-07-27 11:13:39 +02:00
|
|
|
attr_accessor :array , :index , :register
|
2015-07-24 12:23:56 +02:00
|
|
|
|
|
|
|
def to_s
|
2015-10-17 09:03:56 +02:00
|
|
|
"GetSlot: #{array}[#{index}] -> #{register}"
|
2015-07-24 12:23:56 +02:00
|
|
|
end
|
|
|
|
|
2014-10-03 10:07:18 +02:00
|
|
|
end
|
2015-06-30 08:43:50 +02:00
|
|
|
|
|
|
|
# Produce a GetSlot instruction.
|
2015-07-27 11:13:39 +02:00
|
|
|
# Array and to are registers or symbols that can be transformed to a register by resolve_to_register
|
2015-06-30 08:43:50 +02:00
|
|
|
# index resolves with resolve_index.
|
2015-07-27 11:13:39 +02:00
|
|
|
def self.get_slot source , array , index , to
|
|
|
|
index = resolve_index( array , index)
|
|
|
|
array = resolve_to_register array
|
2015-06-30 08:43:50 +02:00
|
|
|
to = resolve_to_register to
|
2015-07-27 11:13:39 +02:00
|
|
|
GetSlot.new( source , array , index , to)
|
2015-06-30 08:43:50 +02:00
|
|
|
end
|
2014-10-03 10:07:18 +02:00
|
|
|
end
|