Class: Carbon::Tacky::Block

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

Instance Method Summary collapse

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.

Parameters:

  • function (Tacky::Function)

    The function this block is a part of.

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

    The name of the block.

  • instructions (<Tacky::Instruction>) (defaults to: [])

    This should not be used.



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.

Returns:



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.

Returns:

  • ({::Numeric => ::LLVM::Value})


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.

Returns:

  • (::String)


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.

Yields:

  • (builder)

    To build the block.

Yield Parameters:

Returns:

  • (Tacky::Builder)

    If no block was given.

  • (::Object)

    Otherwise.



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).

Parameters:

See Also:



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

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

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.

Returns:

See Also:



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.

Returns:

  • (::Numeric)


47
48
49
# File 'lib/carbon/tacky/block.rb', line 47

def next
  @function.counter.increment
end