rubyx/lib/risc/instruction.rb

75 lines
2.5 KiB
Ruby
Raw Normal View History

require "util/list"
2014-05-03 14:13:44 +02:00
module Risc
# the register machine has at least 8 registers, named r0-r5 , :lr and :pc
# (for historical reasons , r for register, pc for ProgramCounter ie next instruction address
# and lr means LinkRegister, ie the location where to return to when in a function)
#
# We can load and store their contents, move data between them and
# access (get/set) memory at a constant offset from a register
# While Mom works with objects, the register machine has registers,
# but we keep the names for better understanding, r2-5 are temporary/scratch
# There is no direct memory access, only through registers
# Constants can/must be loaded into registers before use
# At compile time, Instructions form a linked list (:next attribute is the link)
# At run time Instructions are traversesed as a graph
#
# Branches fan out, Labels collect
# Labels are the only valid branch targets
#
class Instruction
include Util::List
def initialize( source , nekst = nil )
2015-10-28 20:38:23 +01:00
@source = source
@next = nekst
return unless source
raise "Source must be string or Instruction, not #{source.class}" unless source.is_a?(String) or
source.is_a?(Mom::Instruction) or source.is_a?(Parfait::CallableMethod)
2015-07-18 10:21:49 +02:00
end
attr_reader :source
2015-07-18 10:21:49 +02:00
def to_arr
ret = []
self.each {|ins| ret << ins}
ret
end
# just part of the protocol, noop in this case
def precheck
end
def to_cpu( translator )
translator.translate( self )
end
2018-03-22 17:38:19 +01:00
def class_source( derived)
"#{self.class.name.split("::").last}: #{derived} #{source_mini}"
end
def source_mini
return "(no source)" unless source
return "(from: #{source[0..50]})" if source.is_a?(String)
2018-03-22 17:38:19 +01:00
"(from: #{source.class.name.split("::").last})"
end
2014-05-02 07:02:25 +02:00
end
2015-05-24 19:00:11 +02:00
2014-05-02 07:02:25 +02:00
end
require_relative "instructions/setter"
require_relative "instructions/getter"
require_relative "instructions/reg_to_slot"
require_relative "instructions/slot_to_reg"
2016-12-25 17:11:58 +01:00
require_relative "instructions/reg_to_byte"
require_relative "instructions/byte_to_reg"
require_relative "instructions/load_constant"
require_relative "instructions/load_data"
2015-06-22 21:48:42 +02:00
require_relative "instructions/syscall"
2014-10-04 11:52:47 +02:00
require_relative "instructions/function_call"
require_relative "instructions/function_return"
require_relative "instructions/transfer"
require_relative "instructions/label"
require_relative "instructions/branch"
2015-08-04 21:01:20 +02:00
require_relative "instructions/operator_instruction"