introduce base class for get_slot and get_byte called getter

This commit is contained in:
Torsten Ruger 2016-12-11 14:12:35 +02:00
parent ef285a146f
commit e479b00b29
5 changed files with 51 additions and 30 deletions

View File

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

View File

@ -5,23 +5,16 @@ module Register
# indexes are 1 based (as for slots) , which means we sacrifice a byte of every word
# for our sanity
class GetByte < Instruction
class GetByte < Getter
# If you had a c array (of int8) and index offset
# the instruction would do register = array[index]
# The arguments are in the order that makes sense for the Instruction name
# So GetSlot means the slot (array and index) moves to the register (last argument)
def initialize source , array , index , register
super(source)
@array = array
@index = index
@register = register
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 :array , :index , :register
# def initialize source , array , index , register
# super
# end
# attr_accessor :array , :index , :register
def to_s
"GetByte: #{array}[#{index}] -> #{register}"

View File

@ -4,31 +4,21 @@ module Register
# 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
#
# 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
class GetSlot < Instruction
class GetSlot < Getter
# If you had a c array and index offset
# the instruction would do register = array[index]
# The arguments are in the order that makes sense for the Instruction name
# So GetSlot means the slot (array and index) moves to the register (last argument)
def initialize source , array , index , register
super(source)
@array = array
@index = index
@register = register
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 :array , :index , :register
# def initialize source , array , index , register
# super
# end
# attr_accessor :array , :index , :register
def to_s
"GetSlot: #{array}[#{index}] -> #{register}"

View File

@ -0,0 +1,34 @@
module Register
# Getter is a base class for get instructions (GetSlot and GetByte , possibly more coming)
#
# The instruction that is modelled is loading data from an array into a register
#
# Getter has a
# - an array where the data comes from
# - and (array) index
# - Register that the data is moved to
# Getter and Setter api follow the pattern from -> to
class Getter < Instruction
# If you had a c array and index offset
# the instruction would do register = array[index]
# The arguments are in the order that makes sense for the Instruction name
# So GetSlot means the slot (array and index) moves to the register (last argument)
def initialize source , array , index , register
super(source)
@array = array
@index = index
@register = register
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 :array , :index , :register
end
end

View File

@ -1,12 +1,15 @@
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
# The instruction that is modelled is loading data from a register into an array
#
# Setter has a
# - Register that the data is moved to
# - an array where the data comes from
# - Register that the data is comes from
# - an array where the data goes
# - and (array) index
# Getter and Setter api follow the pattern from -> to
class Setter < Instruction
# If you had a c array and index offset