2018-03-19 16:49:46 +01:00
|
|
|
module Mom
|
|
|
|
|
2018-05-29 16:03:55 +02:00
|
|
|
# A Label is the only legal target for a branch (in Mom, in Risc a BinaryCode is ok too)
|
2018-03-19 16:49:46 +01:00
|
|
|
#
|
|
|
|
# In the dynamic view (runtime) where the instructions form a graph,
|
|
|
|
# branches fan out, Labels collect. In other words a branch is the place where
|
|
|
|
# several roads lead off, and a Label where several roads arrive.
|
|
|
|
#
|
|
|
|
# A Label has a name which is mainly used for debugging.
|
|
|
|
#
|
2018-08-19 16:29:04 +02:00
|
|
|
# A Mom::Label converts one2one to a Risc::Label. So in a way it could not be more
|
|
|
|
# simple.
|
|
|
|
# Alas, since almost by definition several roads lead to this label, all those
|
|
|
|
# several converted instructions must also point to the identical label on the
|
|
|
|
# risc level.
|
|
|
|
#
|
|
|
|
# This is achieved by caching the created Risc::Label in an instance variable.
|
|
|
|
# All branches that lead to this label can thus safely call the to_risc and
|
|
|
|
# whoever calls first triggers the labels creation, but all get the same label.
|
|
|
|
#
|
|
|
|
# Off course some specific place still has to be responsible for actually
|
|
|
|
# adding the label to the instruction list (usually an if/while)
|
|
|
|
|
2018-03-19 16:49:46 +01:00
|
|
|
class Label < Instruction
|
|
|
|
attr_reader :name
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
2018-04-17 19:26:15 +02:00
|
|
|
def to_s
|
|
|
|
"Label: #{name}"
|
|
|
|
end
|
|
|
|
|
2018-08-19 16:29:04 +02:00
|
|
|
# generate the risc label lazily
|
2018-08-29 20:06:29 +02:00
|
|
|
def risc_label(comiler)
|
2018-05-30 09:54:18 +02:00
|
|
|
@risc_label ||= Risc.label(self,name)
|
2018-08-29 20:06:29 +02:00
|
|
|
comiler.add_constant(@risc_label.address)
|
|
|
|
@risc_label
|
2018-03-19 16:49:46 +01:00
|
|
|
end
|
2018-08-19 16:29:04 +02:00
|
|
|
|
|
|
|
# add the risc_label to the compiler (instruction flow)
|
|
|
|
# should only be called once
|
|
|
|
def to_risc(compiler)
|
2018-08-29 20:06:29 +02:00
|
|
|
compiler.add_code( risc_label(compiler) )
|
2018-08-19 16:29:04 +02:00
|
|
|
end
|
2018-03-19 16:49:46 +01:00
|
|
|
end
|
|
|
|
end
|