2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2015-07-17 12:21:57 +02:00
|
|
|
|
2018-03-19 16:50:11 +01:00
|
|
|
# A branch must branch to a label.
|
|
|
|
# Different Branches (derived classes) use different registers, the base
|
|
|
|
# just stores the Label
|
2015-07-17 12:21:57 +02:00
|
|
|
class Branch < Instruction
|
2018-03-19 16:50:11 +01:00
|
|
|
def initialize( source , label )
|
2015-07-18 10:21:49 +02:00
|
|
|
super(source)
|
2018-03-19 16:50:11 +01:00
|
|
|
@label = label
|
2015-07-17 12:21:57 +02:00
|
|
|
end
|
2015-10-23 20:27:36 +02:00
|
|
|
attr_reader :label
|
2015-07-18 10:21:49 +02:00
|
|
|
|
|
|
|
def to_s
|
2016-12-28 20:10:14 +01:00
|
|
|
"#{self.class.name.split("::").last}: #{label ? label.name : ''}"
|
2015-07-18 10:21:49 +02:00
|
|
|
end
|
2015-10-19 13:46:12 +02:00
|
|
|
alias :inspect :to_s
|
2015-10-23 20:27:36 +02:00
|
|
|
|
2018-03-19 16:50:11 +01:00
|
|
|
def length( labels = [])
|
2015-10-24 16:12:36 +02:00
|
|
|
ret = super(labels)
|
|
|
|
ret += self.label.length(labels) if self.label
|
|
|
|
ret
|
2015-10-23 20:27:36 +02:00
|
|
|
end
|
|
|
|
|
2017-01-04 20:32:09 +01:00
|
|
|
def to_arr( labels = [] )
|
2015-10-24 16:12:36 +02:00
|
|
|
ret = super(labels)
|
2017-01-04 20:32:09 +01:00
|
|
|
ret += self.label.to_arr(labels) if self.label
|
2015-10-24 16:12:36 +02:00
|
|
|
ret
|
2015-10-23 20:27:36 +02:00
|
|
|
end
|
2015-10-24 16:12:36 +02:00
|
|
|
|
|
|
|
def total_byte_length labels = []
|
2015-10-25 09:54:19 +01:00
|
|
|
ret = super(labels)
|
|
|
|
ret += self.label.total_byte_length(labels) if self.label
|
|
|
|
#puts "#{self.class.name} return #{ret}"
|
2015-10-24 16:12:36 +02:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
# labels have the same position as their next
|
2018-03-19 16:50:11 +01:00
|
|
|
def set_position( position , labels = [])
|
2016-12-28 20:40:06 +01:00
|
|
|
set_position self.label.set_position( position , labels ) if self.label
|
2015-10-24 16:12:36 +02:00
|
|
|
super(position,labels)
|
|
|
|
end
|
|
|
|
|
2018-03-19 16:50:11 +01:00
|
|
|
def assemble_all( io , labels = [])
|
2015-10-25 09:54:19 +01:00
|
|
|
self.assemble(io)
|
|
|
|
self.label.assemble_all(io,labels) if self.label
|
|
|
|
self.next.assemble_all(io, labels) if self.next
|
|
|
|
end
|
|
|
|
|
2018-03-19 16:50:11 +01:00
|
|
|
def each_label( labels =[] , &block)
|
2015-10-25 11:03:31 +01:00
|
|
|
super
|
|
|
|
self.label.each_label(labels , &block) if self.label
|
|
|
|
end
|
|
|
|
|
2015-10-07 09:02:51 +02:00
|
|
|
end
|
|
|
|
|
2018-03-21 07:21:10 +01:00
|
|
|
# dynamic version of an Unconditional branch that jumps to the contents
|
|
|
|
# of a register instead of a hardcoded address
|
|
|
|
# As Branches jump to Labels, this is not derived from Branch
|
|
|
|
# PS: to conditionally jump to a dynamic adddress we do a normal branch
|
|
|
|
# over the dynamic one and then a dynamic one. Save us having all types of branches
|
|
|
|
# in two versions
|
|
|
|
class DynamicJump < Instruction
|
|
|
|
def initialize( source , register )
|
|
|
|
super(source)
|
|
|
|
@register = register
|
|
|
|
end
|
|
|
|
attr_reader :register
|
|
|
|
end
|
|
|
|
|
2018-03-19 16:50:11 +01:00
|
|
|
# branch if two registers contain same value
|
|
|
|
class IsSame < Branch
|
|
|
|
attr_reader :left , :right
|
|
|
|
def initialize(source , left , right , label)
|
|
|
|
super(source , label)
|
|
|
|
end
|
|
|
|
end
|
2018-03-20 17:35:09 +01:00
|
|
|
|
|
|
|
class Unconditional < Branch
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-10-19 15:08:00 +02:00
|
|
|
class IsZero < Branch
|
2015-10-07 09:02:51 +02:00
|
|
|
end
|
|
|
|
|
2015-10-19 15:08:00 +02:00
|
|
|
class IsNotzero < Branch
|
2015-10-07 09:02:51 +02:00
|
|
|
end
|
|
|
|
|
2015-10-19 15:08:00 +02:00
|
|
|
class IsMinus < Branch
|
2015-10-07 09:02:51 +02:00
|
|
|
end
|
2015-07-18 10:21:49 +02:00
|
|
|
|
2015-10-19 15:08:00 +02:00
|
|
|
class IsPlus < Branch
|
2015-07-17 12:21:57 +02:00
|
|
|
end
|
2015-07-18 10:21:49 +02:00
|
|
|
|
2015-07-17 12:21:57 +02:00
|
|
|
end
|