add abstract base class for set_byte and set_slot called setter

This commit is contained in:
Torsten Ruger 2016-12-11 14:06:09 +02:00
parent 1dbd8c86e0
commit ef285a146f
4 changed files with 38 additions and 30 deletions

View File

@ -117,6 +117,7 @@ module Register
end end
require_relative "instructions/setter"
require_relative "instructions/set_slot" require_relative "instructions/set_slot"
require_relative "instructions/get_slot" require_relative "instructions/get_slot"
require_relative "instructions/set_byte" require_relative "instructions/set_byte"

View File

@ -4,22 +4,8 @@ module Register
# indexes are 1 based ! # indexes are 1 based !
class SetByte < Instruction class SetByte < Setter
# If you had a c array (off int8) and index offset (>0)
# the instruction would do array[index] = register
# So SetByte 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
raise "index 0 " if index == 0
raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index)
raise "Not register #{register}" unless RegisterValue.look_like_reg(register)
raise "Not register #{array}" unless RegisterValue.look_like_reg(array)
end
attr_accessor :register , :array , :index
def to_s def to_s
"SetByte: #{register} -> #{array} [#{index}]" "SetByte: #{register} -> #{array} [#{index}]"
end end

View File

@ -4,30 +4,22 @@ module Register
# GetSlot moves data into a register from memory. # GetSlot moves data into a register from memory.
# Both use a base memory (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
#
# This is because that is what cpu's can do. In programming terms this would be accessing # 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. # an element in an array, in the case of SetSlot setting the register in the array.
# btw: to move data between registers, use RegisterTransfer # btw: to move data between registers, use RegisterTransfer
class SetSlot < Instruction class SetSlot < Setter
# If you had a c array and index offset # If you had a c array and index offset
# the instruction would do array[index] = register # the instruction would do array[index] = register
# So SetSlot means the register (first argument) moves to the slot (array and index) # So SetSlot means the register (first argument) moves to the slot (array and index)
def initialize source , register , array , index
super(source) # def initialize source , register , array , index
@register = register # super
@array = array # end
@index = index # attr_accessor :register , :array , :index
raise "index 0 " if index == 0
raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index)
raise "Not register #{register}" unless RegisterValue.look_like_reg(register)
raise "Not register #{array}" unless RegisterValue.look_like_reg(array)
end
attr_accessor :register , :array , :index
def to_s def to_s
"SetSlot: #{register} -> #{array} [#{index}]" "SetSlot: #{register} -> #{array} [#{index}]"
end end

View File

@ -0,0 +1,29 @@
module Register
# Setter is a base class for set instructions (SetSlot and SetByte , possibly more coming)
#
# The instruction that is modelled is loading data from an array into a register
#
# Setter has a
# - Register that the data is moved to
# - an array where the data comes from
# - and (array) index
class Setter < Instruction
# If you had a c array and index offset
# the instruction would do array[index] = register
# So Setter 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
raise "index 0 " if index == 0
raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index)
raise "Not register #{register}" unless RegisterValue.look_like_reg(register)
raise "Not register #{array}" unless RegisterValue.look_like_reg(array)
end
attr_accessor :register , :array , :index
end
end