Class: Carbon::Tacky::Block
- Inherits:
-
Object
- Object
- Carbon::Tacky::Block
- Defined in:
- lib/carbon/tacky/block.rb
Overview
Defines a "block" in Tacky. This corresponds to LLVM's "basic block" for functions. This only contains a list of instructions and a name, and can be referenced by that name.
Instance Attribute Summary collapse
-
#instructions ⇒ <Tacky::Instruction>
readonly
A list of instructions that are executed by this block.
-
#mapping ⇒ {::Numeric => ::LLVM::Value}
readonly
Temporarily used during building to store the mapping of instruction ids to their LLVM counterparts.
-
#name ⇒ ::String
readonly
The "name" of the block.
Instance Method Summary collapse
-
#build {|builder| ... } ⇒ Tacky::Builder, ::Object
Builds the block.
-
#call(context) ⇒ void
private
Builds the block with the given context.
-
#dependencies ⇒ Set<Concrete::Type>
private
Retrieves the dependencies that the block has.
-
#initialize(function, name = "", instructions = []) ⇒ Block
constructor
Initializes the block with the given name and instructions.
-
#next ⇒ ::Numeric
Increment the function's counter, returning the next valid instruction id.
Constructor Details
#initialize(function, name = "", instructions = []) ⇒ Block
Initializes the block with the given name and instructions. If
no name is given, it defaults to an empty string (""
). Instructions
should not be passed by initialization.
37 38 39 40 41 |
# File 'lib/carbon/tacky/block.rb', line 37 def initialize(function, name = "", instructions = []) @function = function @name = name @instructions = instructions end |
Instance Attribute Details
#instructions ⇒ <Tacky::Instruction> (readonly)
A list of instructions that are executed by this block. This shoudln't be directly modified, and instead should be modified via #build.
15 16 17 |
# File 'lib/carbon/tacky/block.rb', line 15 def instructions @instructions end |
#mapping ⇒ {::Numeric => ::LLVM::Value} (readonly)
Temporarily used during building to store the mapping of instruction ids to their LLVM counterparts.
27 28 29 |
# File 'lib/carbon/tacky/block.rb', line 27 def mapping @mapping end |
#name ⇒ ::String (readonly)
The "name" of the block. This is used to make the resulting LLVM IR more legible.
21 22 23 |
# File 'lib/carbon/tacky/block.rb', line 21 def name @name end |
Instance Method Details
#build {|builder| ... } ⇒ Tacky::Builder, ::Object
Builds the block. If a block is given, it yields a Carbon::Tacky::Builder; otherwise, it returns one.
87 88 89 90 91 92 93 |
# File 'lib/carbon/tacky/block.rb', line 87 def build if block_given? yield Tacky::Builder.new(self) else Tacky::Builder.new(self) end end |
#call(context) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Builds the block with the given context. This iterates over all of the instructions, calling them, and storing the results in the function instruction table (Context#instructions).
72 73 74 75 76 77 78 |
# File 'lib/carbon/tacky/block.rb', line 72 def call(context) this = context.blocks.fetch(self) @instructions.each do |inst| context.instructions[inst.value] = inst.call(context, this) end end |
#dependencies ⇒ Set<Concrete::Type>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the dependencies that the block has. This is determined by asking any of the instructions if they have any dependencies, returning an empty set if all of them have none.
59 60 61 |
# File 'lib/carbon/tacky/block.rb', line 59 def dependencies @instructions.map(&:dependencies).inject(Set.new, :merge) end |
#next ⇒ ::Numeric
Increment the function's counter, returning the next valid instruction id.
47 48 49 |
# File 'lib/carbon/tacky/block.rb', line 47 def next @function.counter.increment end |