2018-03-26 19:46:38 +03:00
|
|
|
require "util/list"
|
2014-05-03 15:13:44 +03:00
|
|
|
|
2018-03-18 10:36:01 +05:30
|
|
|
module Risc
|
2015-10-23 21:27:36 +03:00
|
|
|
|
2018-03-18 10:36:01 +05:30
|
|
|
# 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
|
|
|
|
#
|
2015-10-23 21:27:36 +03:00
|
|
|
# Branches fan out, Labels collect
|
|
|
|
# Labels are the only valid branch targets
|
2018-03-18 10:36:01 +05:30
|
|
|
#
|
2014-10-03 10:25:10 +03:00
|
|
|
class Instruction
|
2018-03-26 20:05:30 +03:00
|
|
|
include Util::List
|
2014-10-03 10:25:10 +03:00
|
|
|
|
2018-03-26 20:05:30 +03:00
|
|
|
def initialize( source , nekst = nil )
|
2015-10-28 21:38:23 +02:00
|
|
|
@source = source
|
|
|
|
@next = nekst
|
|
|
|
return unless source
|
2018-04-18 19:27:46 +03:00
|
|
|
raise "Source must be string or Instruction, not #{source.class}" unless source.is_a?(String) or
|
2018-07-30 14:10:24 +03:00
|
|
|
source.is_a?(Mom::Instruction) or source.is_a?(Parfait::Callable)
|
2015-07-18 11:21:49 +03:00
|
|
|
end
|
2015-07-27 12:13:39 +03:00
|
|
|
attr_reader :source
|
2015-07-18 11:21:49 +03:00
|
|
|
|
2018-03-26 14:54:41 +03:00
|
|
|
def to_arr
|
|
|
|
ret = []
|
|
|
|
self.each {|ins| ret << ins}
|
2015-10-23 21:27:36 +03:00
|
|
|
ret
|
2014-06-08 01:41:56 +03:00
|
|
|
end
|
2015-10-23 21:27:36 +03:00
|
|
|
|
2018-06-17 13:53:17 +03:00
|
|
|
# just part of the protocol, noop in this case
|
|
|
|
def precheck
|
|
|
|
end
|
|
|
|
|
2018-03-25 19:38:59 +03:00
|
|
|
def to_cpu( translator )
|
|
|
|
translator.translate( self )
|
|
|
|
end
|
|
|
|
|
2018-03-22 18:38:19 +02: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 18:05:22 +03:00
|
|
|
return "(from: #{source[0..50]})" if source.is_a?(String)
|
2018-03-22 18:38:19 +02:00
|
|
|
"(from: #{source.class.name.split("::").last})"
|
|
|
|
end
|
2014-05-02 08:02:25 +03:00
|
|
|
end
|
2015-05-24 20:00:11 +03:00
|
|
|
|
2014-05-02 08:02:25 +03:00
|
|
|
end
|
2014-10-03 11:05:17 +03:00
|
|
|
|
2016-12-11 14:06:09 +02:00
|
|
|
require_relative "instructions/setter"
|
2016-12-11 14:12:35 +02:00
|
|
|
require_relative "instructions/getter"
|
2016-12-25 18:02:39 +02:00
|
|
|
require_relative "instructions/reg_to_slot"
|
2016-12-25 18:05:39 +02:00
|
|
|
require_relative "instructions/slot_to_reg"
|
2016-12-25 18:11:58 +02:00
|
|
|
require_relative "instructions/reg_to_byte"
|
|
|
|
require_relative "instructions/byte_to_reg"
|
2014-10-03 11:07:18 +03:00
|
|
|
require_relative "instructions/load_constant"
|
2018-03-31 12:38:30 +03:00
|
|
|
require_relative "instructions/load_data"
|
2015-06-22 22:48:42 +03:00
|
|
|
require_relative "instructions/syscall"
|
2014-10-04 12:52:47 +03:00
|
|
|
require_relative "instructions/function_call"
|
|
|
|
require_relative "instructions/function_return"
|
2018-03-21 16:02:46 +05:30
|
|
|
require_relative "instructions/transfer"
|
2015-10-23 21:27:36 +03:00
|
|
|
require_relative "instructions/label"
|
2015-07-17 13:21:57 +03:00
|
|
|
require_relative "instructions/branch"
|
2019-09-15 19:57:15 +03:00
|
|
|
require_relative "instructions/dynamic_jump"
|
2015-08-04 22:01:20 +03:00
|
|
|
require_relative "instructions/operator_instruction"
|