class Carbon::Tacky::Instruction
An instruction. This is the building block of the rest of the Tacky
structures. Each instruction performs some sort of action.
Constants
- NAMED_INSTRUCTIONS
Instructions that can be represented by this class that can have a name parameter.
@return [<::String>]
Attributes
The name of the instruction. This is limited to a specific set; see the method names on {Tacky::Builder} for what they can be.
@return [::String]
Public Class Methods
Initializes the instruction with the given id, name, and parameters.
@param id [::Numeric] The ID of the instruction. This is the number
of the instruction in the block.
@param name [::Symbol, ::String] The name of the instruction. @param parameters [<Reference, Parameter
, Value
, ::Object>] The
parameters to be used with the instruction.
# 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
Public Instance Methods
Builds the instruction. If the instruction is special, i.e. it starts with an underscore, it is handled seperately. Otherwise, it is handled normally.
@api private @see generate_null
@see generate_sizeof
@see generate_normal
@param context [Tacky::Context] The context. @param block [::LLVM::BasicBlock] The block. @return [LLVM::Value]
# 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
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.
@api private @return [Set<Concrete::Type>]
# File lib/carbon/tacky/instruction.rb, line 79 def dependencies @parameters.select { |p| p.is_a?(Concrete::Type) }.inject(Set.new, :<<) end
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.
@return [::String] The name of the instruction.
# 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
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.
@param name [::String] The new name of the instruction. @return [::String] The new name of the instruction.
# 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