rubyx/lib/risc/instructions/operator_instruction.rb

50 lines
1.5 KiB
Ruby
Raw Normal View History

module Risc
2015-08-04 21:01:20 +02:00
def self.operators
[:+, :-, :>>, :<<, :*, :&, :|]
end
# Operator instructions on the first two registers given
# # Result into the last register
2018-03-24 16:53:27 +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 "+" , "-", ">>", "<<", "*", "&", "|"
#
# Result may be nil, then register is autogenerated
#
2015-08-04 21:01:20 +02:00
class OperatorInstruction < Instruction
def initialize( source , operator , left , right , result = nil)
2015-08-04 21:01:20 +02:00
super(source)
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
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
attr_reader :operator, :left , :right , :result
2015-08-04 21:01:20 +02:00
# return an array of names of registers that is used by the instruction
def register_attributes
[:left , :right, :result]
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
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