2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2016-12-25 17:11:58 +01:00
|
|
|
# Setter is a base class for set instructions (RegToSlot and RegToByte , possibly more coming)
|
2016-12-11 13:06:09 +01:00
|
|
|
#
|
2016-12-11 13:12:35 +01:00
|
|
|
# The instruction that is modelled is loading data from a register into an array
|
2016-12-11 13:06:09 +01:00
|
|
|
#
|
|
|
|
# Setter has a
|
2017-01-19 08:02:29 +01:00
|
|
|
# - Risc that the data is comes from
|
2016-12-11 13:12:35 +01:00
|
|
|
# - an array where the data goes
|
2016-12-11 13:06:09 +01:00
|
|
|
# - and (array) index
|
2016-12-11 13:12:35 +01:00
|
|
|
|
|
|
|
# Getter and Setter api follow the pattern from -> to
|
|
|
|
|
2016-12-11 13:06:09 +01:00
|
|
|
class Setter < Instruction
|
|
|
|
|
|
|
|
# If you had a c array and index offset
|
|
|
|
# the instruction would do array[index] = register
|
2018-03-14 13:09:49 +01:00
|
|
|
# The arguments are in the order that makes sense for the Instruction name
|
2018-03-22 17:38:19 +01:00
|
|
|
# So RegToSlot means the register (first argument) moves to the slot (array and index)
|
2018-03-14 13:09:49 +01:00
|
|
|
def initialize( source , register , array , index )
|
2016-12-11 13:06:09 +01:00
|
|
|
super(source)
|
|
|
|
@register = register
|
|
|
|
@array = array
|
|
|
|
@index = index
|
2018-08-19 14:36:51 +02:00
|
|
|
raise "Not integer or reg index #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index)
|
2018-06-29 10:39:07 +02:00
|
|
|
raise "Not register #{register}" unless RegisterValue.look_like_reg(register)
|
2018-08-19 14:36:51 +02:00
|
|
|
raise "Not slot/register #{array}" unless RegisterValue.look_like_reg(array)
|
2016-12-11 13:06:09 +01:00
|
|
|
end
|
|
|
|
attr_accessor :register , :array , :index
|
|
|
|
|
2020-03-18 16:49:23 +01:00
|
|
|
# return an array of names of registers that is used by the instruction
|
|
|
|
def register_names
|
|
|
|
names = [array.symbol , register.symbol]
|
|
|
|
names << index.symbol if index.is_a?(RegisterValue)
|
|
|
|
names
|
|
|
|
end
|
|
|
|
|
2016-12-15 23:30:26 +01:00
|
|
|
def to_s
|
2018-03-22 17:38:19 +01:00
|
|
|
class_source "#{register} -> #{array}[#{index}]"
|
2016-12-15 23:30:26 +01:00
|
|
|
end
|
|
|
|
|
2016-12-11 13:06:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|