add get/set byte instructions

it seems all cpus have them anyway so best use them
it was a pain to do this on word level, hard to write, hard to debug and
quite unnecessarily slow
This commit is contained in:
Torsten Ruger 2015-11-19 10:07:27 +02:00
parent 5369dc3d52
commit ff65952a3e
2 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,40 @@
module Register
# GetByte moves a single byte into a register from memory.
# indexes are 1 based (as for slots) , which means we sacrifice a byte of every word
# for our sanity
class GetByte < Instruction
# 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 to_s
"GetByte: #{array}[#{index}] -> #{register}"
end
end
# Produce a GetByte instruction.
# from and to are translated (from symbol to register if neccessary)
# but index is left as is.
def self.get_byte source , array , index , to
from = resolve_to_register from
to = resolve_to_register to
GetByte.new( source , array , index , to)
end
end

View File

@ -0,0 +1,39 @@
module Register
# SetByte moves a byte into memory from a register.
# indexes are 1 based !
class SetByte < Instruction
# 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
"SetByte: #{register} -> #{array} [#{index}]"
end
end
# Produce a SetByte instruction.
# from and to are translated (from symbol to register if neccessary)
# but index is left as is.
def self.set_byte source , from , to , index
from = resolve_to_register from
index = resolve_index( to , index)
to = resolve_to_register to
SetByte.new( source, from , to , index)
end
end