2017-01-19 09:02:29 +02:00
|
|
|
module Risc
|
2015-07-17 13:21:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
# a branch must branch to a block.
|
|
|
|
class Branch < Instruction
|
2015-07-18 11:21:49 +03:00
|
|
|
def initialize source , to
|
|
|
|
super(source)
|
2015-10-23 21:27:36 +03:00
|
|
|
@label = to
|
2015-07-17 13:21:57 +03:00
|
|
|
end
|
2015-10-23 21:27:36 +03:00
|
|
|
attr_reader :label
|
2015-07-18 11:21:49 +03:00
|
|
|
|
|
|
|
def to_s
|
2016-12-28 21:10:14 +02:00
|
|
|
"#{self.class.name.split("::").last}: #{label ? label.name : ''}"
|
2015-07-18 11:21:49 +03:00
|
|
|
end
|
2015-10-19 14:46:12 +03:00
|
|
|
alias :inspect :to_s
|
2015-10-23 21:27:36 +03:00
|
|
|
|
|
|
|
def length labels = []
|
2015-10-24 17:12:36 +03:00
|
|
|
ret = super(labels)
|
|
|
|
ret += self.label.length(labels) if self.label
|
|
|
|
ret
|
2015-10-23 21:27:36 +03:00
|
|
|
end
|
|
|
|
|
2017-01-04 21:32:09 +02:00
|
|
|
def to_arr( labels = [] )
|
2015-10-24 17:12:36 +03:00
|
|
|
ret = super(labels)
|
2017-01-04 21:32:09 +02:00
|
|
|
ret += self.label.to_arr(labels) if self.label
|
2015-10-24 17:12:36 +03:00
|
|
|
ret
|
2015-10-23 21:27:36 +03:00
|
|
|
end
|
2015-10-24 17:12:36 +03:00
|
|
|
|
|
|
|
def total_byte_length labels = []
|
2015-10-25 10:54:19 +02:00
|
|
|
ret = super(labels)
|
|
|
|
ret += self.label.total_byte_length(labels) if self.label
|
|
|
|
#puts "#{self.class.name} return #{ret}"
|
2015-10-24 17:12:36 +03:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
# labels have the same position as their next
|
|
|
|
def set_position position , labels = []
|
2016-12-28 21:40:06 +02:00
|
|
|
set_position self.label.set_position( position , labels ) if self.label
|
2015-10-24 17:12:36 +03:00
|
|
|
super(position,labels)
|
|
|
|
end
|
|
|
|
|
2015-10-25 10:54:19 +02:00
|
|
|
def assemble_all io , labels = []
|
|
|
|
self.assemble(io)
|
|
|
|
self.label.assemble_all(io,labels) if self.label
|
|
|
|
self.next.assemble_all(io, labels) if self.next
|
|
|
|
end
|
|
|
|
|
2015-10-25 12:03:31 +02:00
|
|
|
def each_label labels =[] , &block
|
|
|
|
super
|
|
|
|
self.label.each_label(labels , &block) if self.label
|
|
|
|
end
|
|
|
|
|
2015-10-07 10:02:51 +03:00
|
|
|
end
|
|
|
|
|
2015-10-19 16:08:00 +03:00
|
|
|
class IsZero < Branch
|
2015-10-07 10:02:51 +03:00
|
|
|
end
|
|
|
|
|
2015-10-19 16:08:00 +03:00
|
|
|
class IsNotzero < Branch
|
2015-10-07 10:02:51 +03:00
|
|
|
end
|
|
|
|
|
2015-10-19 16:08:00 +03:00
|
|
|
class IsMinus < Branch
|
2015-10-07 10:02:51 +03:00
|
|
|
end
|
2015-07-18 11:21:49 +03:00
|
|
|
|
2015-10-19 16:08:00 +03:00
|
|
|
class IsPlus < Branch
|
2015-07-17 13:21:57 +03:00
|
|
|
end
|
2015-07-18 11:21:49 +03:00
|
|
|
|
2015-07-17 13:21:57 +03:00
|
|
|
end
|