2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2015-08-04 21:01:20 +02:00
|
|
|
|
2018-04-19 21:13:52 +02:00
|
|
|
def self.operators
|
|
|
|
[:+, :-, :>>, :<<, :*, :&, :|]
|
|
|
|
end
|
2020-03-14 11:22:37 +01:00
|
|
|
|
|
|
|
# Operator instructions on the first two registers given
|
|
|
|
# # Result into the last register
|
2018-03-24 16:53:27 +01:00
|
|
|
#
|
2020-03-14 11:22:37 +01:00
|
|
|
# result = left OP right
|
2018-03-24 16:53:27 +01:00
|
|
|
#
|
|
|
|
# With OP being the normal logical and mathematical operations provided by
|
|
|
|
# cpus. Ie "+" , "-", ">>", "<<", "*", "&", "|"
|
|
|
|
#
|
2020-03-14 11:22:37 +01:00
|
|
|
# Result may be nil, then register is autogenerated
|
|
|
|
#
|
2015-08-04 21:01:20 +02:00
|
|
|
class OperatorInstruction < Instruction
|
2020-03-14 11:22:37 +01:00
|
|
|
def initialize( source , operator , left , right , result = nil)
|
2015-08-04 21:01:20 +02:00
|
|
|
super(source)
|
2019-10-03 23:36:49 +02:00
|
|
|
operator = operator.value if operator.is_a?(Sol::Constant)
|
2015-08-04 21:01:20 +02:00
|
|
|
@operator = operator
|
2019-09-11 18:23:56 +02:00
|
|
|
raise "unsuported operator :#{operator}:#{operator.class}:" unless Risc.operators.include?(operator)
|
2015-08-04 21:01:20 +02:00
|
|
|
@left = left
|
|
|
|
@right = right
|
2020-03-14 11:22:37 +01:00
|
|
|
raise "Not register #{left}" unless left.is_a?(RegisterValue)
|
|
|
|
raise "Not register #{right}" unless right.is_a?(RegisterValue)
|
|
|
|
unless result
|
|
|
|
result = RegisterValue.new("op_#{operator}_#{object_id.to_s(16)}".to_sym , :Integer)
|
|
|
|
end
|
|
|
|
raise "Not register #{result}" unless result.is_a?(RegisterValue)
|
|
|
|
@result = result
|
2015-08-04 21:01:20 +02:00
|
|
|
end
|
2020-03-14 11:22:37 +01:00
|
|
|
attr_reader :operator, :left , :right , :result
|
2015-08-04 21:01:20 +02:00
|
|
|
|
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
|
|
|
|
[:left , :right, :result]
|
2020-03-18 16:49:23 +01:00
|
|
|
end
|
|
|
|
|
2015-08-04 21:01:20 +02:00
|
|
|
def to_s
|
2018-03-22 17:38:19 +01:00
|
|
|
class_source "#{left} #{operator} #{right}"
|
2015-08-04 21:01:20 +02:00
|
|
|
end
|
|
|
|
|
2015-11-21 13:17:54 +01:00
|
|
|
end
|
2020-03-14 11:22:37 +01:00
|
|
|
def self.op( source , operator , left , right , result = nil)
|
|
|
|
OperatorInstruction.new( source , operator , left , right , result)
|
2015-08-04 21:01:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|