Class: Carbon::Tacky::Function
- Inherits:
-
Object
- Object
- Carbon::Tacky::Function
- Defined in:
- lib/carbon/tacky/function.rb
Overview
A pseudo function. This is used for definitions in order to better serialize them. However, each instruction uses a Concrete::Type instead of the LLVM type that it corresponds to, allowing expansion only upon generation.
Instance Attribute Summary collapse
-
#blocks ⇒ <Tacky::Block>
readonly
The basic blocks of the function.
-
#counter ⇒ Counter
readonly
The instruction id counter.
Instance Method Summary collapse
-
#add(name = "") ⇒ Tacky::Block
Creates a new Block with the given name, adding it to the block list, and returns it.
-
#call(function, build, generics) ⇒ void
private
Creates the function for LLVM.
-
#dependencies ⇒ Set<Concrete::Type>
private
Returns a set of dependencies that the function depends on.
-
#find(name) ⇒ Tacky::Block?
Finds the block with the given name, if it exists.
-
#initialize(parameters, blocks = []) ⇒ Function
constructor
Creates a function with the given parameters and blocks.
-
#params ⇒ Tacky::Parameter
The parameters of the function.
Constructor Details
#initialize(parameters, blocks = []) ⇒ Function
Creates a function with the given parameters and blocks. The parameters are required; the blocks should not be given.
30 31 32 33 34 35 36 37 |
# File 'lib/carbon/tacky/function.rb', line 30 def initialize(parameters, blocks = []) @blocks = blocks @parameters = parameters @counter = Counter.new params freeze build(&Proc.new) if block_given? end |
Instance Attribute Details
#blocks ⇒ <Tacky::Block> (readonly)
The basic blocks of the function. The first block is expected to be the entry block; the rest can be in any order.
17 18 19 |
# File 'lib/carbon/tacky/function.rb', line 17 def blocks @blocks end |
#counter ⇒ Counter (readonly)
The instruction id counter.
22 23 24 |
# File 'lib/carbon/tacky/function.rb', line 22 def counter @counter end |
Instance Method Details
#add(name = "") ⇒ Tacky::Block
Creates a new Block with the given name, adding it to the block list, and returns it.
44 45 46 47 48 |
# File 'lib/carbon/tacky/function.rb', line 44 def add(name = "") block = Block.new(self, name) @blocks << block block end |
#call(function, build, generics) ⇒ 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.
Creates the function for LLVM. This has three main steps: first, mark the function parameters with names. Second, create the function blocks for instructional use. Third, call the constituant blocks for them to build themselves.
83 84 85 86 87 88 89 90 |
# File 'lib/carbon/tacky/function.rb', line 83 def call(function, build, generics) context = Context.new(self, build, generics) mark_function_params(context, function) @blocks.each do |block| context.blocks[block] = function.basic_blocks.append(block.name) end.each { |block| block.call(context) } 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.
Returns a set of dependencies that the function depends on. The set is built by asking the constituant blocks for their dependencies, and merging them all into one set. If no blocks exist, or there are no dependencies, an empty set is returned.
67 68 69 |
# File 'lib/carbon/tacky/function.rb', line 67 def dependencies @blocks.map(&:dependencies).inject(Set.new, :merge) end |
#find(name) ⇒ Tacky::Block?
Finds the block with the given name, if it exists. Note that multiple blocks can have the same name and still be disparate.
56 57 58 |
# File 'lib/carbon/tacky/function.rb', line 56 def find(name) @blocks.find { |b| b.name == name } end |
#params ⇒ Tacky::Parameter
The parameters of the function. These are enclosed with a Parameter to allow the name of said parameters to be set. The parameters are given an index and a type.
97 98 99 100 101 |
# File 'lib/carbon/tacky/function.rb', line 97 def params @params ||= @parameters.each_with_index.map do |type, i| Tacky::Parameter.new(i, type) end end |