github.com/llir/llvm@v0.3.6/ir/inst_unary.go (about)

     1  package ir
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/llir/llvm/ir/enum"
     8  	"github.com/llir/llvm/ir/types"
     9  	"github.com/llir/llvm/ir/value"
    10  )
    11  
    12  // --- [ Unary instructions ] --------------------------------------------------
    13  
    14  // ~~~ [ fneg ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    15  
    16  // InstFNeg is an LLVM IR fneg instruction.
    17  type InstFNeg struct {
    18  	// Name of local variable associated with the result.
    19  	LocalIdent
    20  	// Operand.
    21  	X value.Value // floating-point scalar or floating-point vector
    22  
    23  	// extra.
    24  
    25  	// Type of result produced by the instruction.
    26  	Typ types.Type
    27  	// (optional) Fast math flags.
    28  	FastMathFlags []enum.FastMathFlag
    29  	// (optional) Metadata.
    30  	Metadata
    31  }
    32  
    33  // NewFNeg returns a new fneg instruction based on the given operand.
    34  func NewFNeg(x value.Value) *InstFNeg {
    35  	inst := &InstFNeg{X: x}
    36  	// Compute type.
    37  	inst.Type()
    38  	return inst
    39  }
    40  
    41  // String returns the LLVM syntax representation of the instruction as a
    42  // type-value pair.
    43  func (inst *InstFNeg) String() string {
    44  	return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
    45  }
    46  
    47  // Type returns the type of the instruction.
    48  func (inst *InstFNeg) Type() types.Type {
    49  	// Cache type if not present.
    50  	if inst.Typ == nil {
    51  		inst.Typ = inst.X.Type()
    52  	}
    53  	return inst.Typ
    54  }
    55  
    56  // LLString returns the LLVM syntax representation of the instruction.
    57  //
    58  // 'fneg' FastMathFlags=FastMathFlag* X=TypeValue Metadata=(',' MetadataAttachment)+?
    59  func (inst *InstFNeg) LLString() string {
    60  	buf := &strings.Builder{}
    61  	fmt.Fprintf(buf, "%s = ", inst.Ident())
    62  	buf.WriteString("fneg")
    63  	for _, flag := range inst.FastMathFlags {
    64  		fmt.Fprintf(buf, " %s", flag)
    65  	}
    66  	fmt.Fprintf(buf, " %s", inst.X)
    67  	for _, md := range inst.Metadata {
    68  		fmt.Fprintf(buf, ", %s", md)
    69  	}
    70  	return buf.String()
    71  }
    72  
    73  // Operands returns a mutable list of operands of the given instruction.
    74  func (inst *InstFNeg) Operands() []*value.Value {
    75  	return []*value.Value{&inst.X}
    76  }