2018-03-26 18:46:38 +02:00
|
|
|
require "util/list"
|
2014-05-03 14:13:44 +02:00
|
|
|
|
2018-03-18 06:06:01 +01:00
|
|
|
module Risc
|
2015-10-23 20:27:36 +02:00
|
|
|
|
2018-03-18 06:06:01 +01:00
|
|
|
# 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
|
2019-10-03 19:55:41 +02:00
|
|
|
# While SlotMachine works with objects, the register machine has registers,
|
2018-03-18 06:06:01 +01:00
|
|
|
# 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
|
|
|
|
#
|
2015-10-23 20:27:36 +02:00
|
|
|
# Branches fan out, Labels collect
|
|
|
|
# Labels are the only valid branch targets
|
2018-03-18 06:06:01 +01:00
|
|
|
#
|
2014-10-03 09:25:10 +02:00
|
|
|
class Instruction
|
2018-03-26 19:05:30 +02:00
|
|
|
include Util::List
|
2014-10-03 09:25:10 +02:00
|
|
|
|
2018-03-26 19:05:30 +02:00
|
|
|
def initialize( source , nekst = nil )
|
2015-10-28 20:38:23 +01:00
|
|
|
@source = source
|
|
|
|
@next = nekst
|
|
|
|
return unless source
|
2018-04-18 18:27:46 +02:00
|
|
|
raise "Source must be string or Instruction, not #{source.class}" unless source.is_a?(String) or
|
2019-10-03 19:55:41 +02:00
|
|
|
source.is_a?(SlotMachine::Instruction) or source.is_a?(Parfait::Callable)
|
2015-07-18 10:21:49 +02:00
|
|
|
end
|
2015-07-27 11:13:39 +02:00
|
|
|
attr_reader :source
|
2015-07-18 10:21:49 +02:00
|
|
|
|
2018-03-26 13:54:41 +02:00
|
|
|
def to_arr
|
|
|
|
ret = []
|
|
|
|
self.each {|ins| ret << ins}
|
2015-10-23 20:27:36 +02:00
|
|
|
ret
|
2014-06-08 00:41:56 +02:00
|
|
|
end
|
2015-10-23 20:27:36 +02:00
|
|
|
|
2018-06-17 12:53:17 +02:00
|
|
|
# just part of the protocol, noop in this case
|
|
|
|
def precheck
|
|
|
|
end
|
|
|
|
|
2020-03-18 16:49:23 +01:00
|
|
|
# return an array of names of registers that is used by the instruction
|
2020-03-20 12:58:40 +01:00
|
|
|
def register_attributes
|
2020-03-18 16:49:23 +01:00
|
|
|
raise "Not implemented in #{self.class}"
|
|
|
|
end
|
|
|
|
|
2020-03-20 12:58:40 +01:00
|
|
|
# return an array of names of registers that is used by the instruction
|
|
|
|
def register_names
|
|
|
|
register_attributes.collect {|attr| get_register(attr)}
|
|
|
|
end
|
|
|
|
|
|
|
|
# get the register value that has the name (ie attribute by that name)
|
|
|
|
# return the registers name ie RegisterValue's symbol
|
|
|
|
def get_register(name)
|
|
|
|
instance_variable_get("@#{name}".to_sym).symbol
|
|
|
|
end
|
|
|
|
|
|
|
|
# set all registers that has the name "name"
|
|
|
|
# going through the register_names and setting all where the
|
|
|
|
# get_register would return name
|
|
|
|
# Set to the value given as second arg.
|
|
|
|
def set_registers(name , value)
|
|
|
|
register_attributes.each do |attr|
|
|
|
|
reg = instance_variable_get("@#{attr}".to_sym)
|
|
|
|
next unless reg.symbol == name
|
|
|
|
reg.set_name(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-25 18:38:59 +02:00
|
|
|
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
|
2018-05-23 17:05:22 +02:00
|
|
|
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
|
2014-10-03 10:05:17 +02:00
|
|
|
|
2020-03-02 16:58:13 +01:00
|
|
|
require_relative "setter"
|
|
|
|
require_relative "getter"
|
|
|
|
require_relative "reg_to_slot"
|
|
|
|
require_relative "slot_to_reg"
|
|
|
|
require_relative "reg_to_byte"
|
|
|
|
require_relative "byte_to_reg"
|
|
|
|
require_relative "load_constant"
|
|
|
|
require_relative "load_data"
|
|
|
|
require_relative "syscall"
|
|
|
|
require_relative "function_call"
|
|
|
|
require_relative "function_return"
|
|
|
|
require_relative "transfer"
|
|
|
|
require_relative "label"
|
|
|
|
require_relative "branch"
|
|
|
|
require_relative "dynamic_jump"
|
|
|
|
require_relative "operator_instruction"
|