Class: Carbon::Tacky::Instruction

Inherits:
Value
  • Object
show all
Includes:
Generation
Defined in:
lib/carbon/tacky/instruction.rb,
lib/carbon/tacky/instruction/generation.rb

Overview

An instruction. This is the building block of the rest of the Tacky structures. Each instruction performs some sort of action.

Defined Under Namespace

Modules: Generation

Constant Summary collapse

NAMED_INSTRUCTIONS =

Instructions that can be represented by this class that can have a name parameter.

Returns:

  • (<::String>)
%w(
  add alloca and array_alloca array_malloc ashr bit_cast exact_sdiv
  extract_element extract_value fadd fcmp fdiv fmul fp2si fp2ui fp_cast
  fp_ext fp_trunc frem fsub gep global_string global_string_pointer icmp
  inbounds_gep insert_element insert_value int2ptr int_cast invoke
  is_not_null is_null load lshr malloc mul neg not nsw_add nsw_mul
  nsw_neg nsw_sub nuw_add nuw_mul nuw_neg nuw_sub or phi pointer_cast
  ptr2int ptr_diff sdiv select sext sext_or_bit_cast shl shuffle_vector
  si2fp srem struct_gep sub trunc trunc_or_bit_cast udiv ui2fp urem xor
  zext zext_or_bit_cast
).freeze

Instance Attribute Summary collapse

Attributes inherited from Value

#type, #value

Instance Method Summary collapse

Constructor Details

#initialize(id, name, parameters, reference) ⇒ Instruction

Initializes the instruction with the given id, name, and parameters.

Parameters:

  • id (::Numeric)

    The ID of the instruction. This is the number of the instruction in the block.

  • name (::Symbol, ::String)

    The name of the instruction.

  • parameters (<Reference, Parameter, Value, ::Object>)

    The parameters to be used with the instruction.



46
47
48
49
50
51
# File 'lib/carbon/tacky/instruction.rb', line 46

def initialize(id, name, parameters, reference)
  @value = id
  @instruction = name.to_s
  @parameters = parameters
  @reference = reference
end

Instance Attribute Details

#instruction::String (readonly)

The name of the instruction. This is limited to a specific set; see the method names on Builder for what they can be.

Returns:

  • (::String)


32
33
34
# File 'lib/carbon/tacky/instruction.rb', line 32

def instruction
  @instruction
end

#parameters<Reference, Parameter, Value, ::Object> (readonly)

The parameters of the instruction.

Returns:



37
38
39
# File 'lib/carbon/tacky/instruction.rb', line 37

def parameters
  @parameters
end

Instance Method Details

#call(context, block) ⇒ LLVM::Value

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.

Builds the instruction. If the instruction is special, i.e. it starts with an underscore, it is handled seperately. Otherwise, it is handled normally.

Parameters:

  • context (Tacky::Context)

    The context.

  • block (::LLVM::BasicBlock)

    The block.

Returns:

  • (LLVM::Value)

See Also:

  • Carbon::Tacky::Instruction::Generation#generate_null
  • Carbon::Tacky::Instruction::Generation#generate_sizeof
  • Carbon::Tacky::Instruction::Generation#generate_normal


64
65
66
67
68
69
70
71
# File 'lib/carbon/tacky/instruction.rb', line 64

def call(context, block)
  case @instruction
  when "_null"   then generate_null(context, block)
  when "_sizeof" then generate_sizeof(context, block)
  when "_deref"  then generate_deref(context, block)
  else generate_normal(context, block)
  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.

The dependencies of the instruction. If any of the parameters are a Concrete::Type, it is set as a dependency, and the result is put into a set and returned.

Returns:



79
80
81
# File 'lib/carbon/tacky/instruction.rb', line 79

def dependencies
  @parameters.select { |p| p.is_a?(Concrete::Type) }.inject(Set.new, :<<)
end

#name::String

Retrieves the name of the instruction. The name of the instruction is essentally the name used by the return value that represents the instruction. If this instruction is not a named instruction (i.e. #instruction is not in NAMED_INSTRUCTIONS), or the last parameter isn't a string, it returns an empty string (""). Otherwise, it returns the last parameter.

Returns:

  • (::String)

    The name of the instruction.



91
92
93
94
95
96
97
98
# File 'lib/carbon/tacky/instruction.rb', line 91

def name
  if NAMED_INSTRUCTIONS.include?(@instruction) &&
     @parameters.last.is_a?(::String)
    @parameters.last
  else
    ""
  end
end

#name=(name) ⇒ ::String

Sets the name of the instruction. The name of the instruction is essentially the name used by the return value that represents the instruction. If this instruction is not a named instruction (i.e. #instruction is not in NAMED_INSTRUCTIONS), then this does nothing. If the last parameter is a string, this sets the last parameter to the new name. Otherwise, it adds the name to the end of the parameters list.

Parameters:

  • name (::String)

    The new name of the instruction.

Returns:

  • (::String)

    The new name of the instruction.



110
111
112
113
114
115
116
117
118
119
# File 'lib/carbon/tacky/instruction.rb', line 110

def name=(name)
  return name unless NAMED_INSTRUCTIONS.include?(@instruction)
  if @parameters.last.is_a?(::String)
    @parameters[-1] = name
  else
    @parameters << name
  end

  name
end