Class: Carbon::Tacky::Function

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • parameters (<Concrete::Type>)

    The parameters that are passed to the function.

  • blocks (<Tacky::Block>) (defaults to: [])

    Should not be used.



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.

Returns:



17
18
19
# File 'lib/carbon/tacky/function.rb', line 17

def blocks
  @blocks
end

#counterCounter (readonly)

The instruction id counter.

Returns:



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.

Parameters:

  • name (::String) (defaults to: "")

    The name of the new block.

Returns:



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.

Parameters:

  • function (::LLVM::Function)

    The LLVM function.

  • build (Concrete::Build)

    The build. This should contain all relevant information for building this function.

  • generics ({::String => Concrete::Type})

    The generics that are being applied to the function.



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

#dependenciesSet<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.

Returns:



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.

Parameters:

  • name (::String)

    The name of the block to find.

Returns:

  • (Tacky::Block)

    If the block can be found.

  • (nil)

    Otherwise.



56
57
58
# File 'lib/carbon/tacky/function.rb', line 56

def find(name)
  @blocks.find { |b| b.name == name }
end

#paramsTacky::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.

Returns:



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