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

     1  package ir
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/llir/llvm/ir/types"
     8  )
     9  
    10  // InlineAsm is an inline assembler expression.
    11  type InlineAsm struct {
    12  	// Assembly instructions.
    13  	Asm string
    14  	// Constraints.
    15  	Constraint string
    16  
    17  	// extra.
    18  
    19  	// Type of result produced by the inline assembler expression.
    20  	Typ types.Type
    21  	// (optional) Side effect.
    22  	SideEffect bool
    23  	// (optional) Stack alignment.
    24  	AlignStack bool
    25  	// (optional) Intel dialect.
    26  	IntelDialect bool
    27  }
    28  
    29  // NewInlineAsm returns a new inline assembler expression based on the given
    30  // type, assembly instructions and constraints.
    31  func NewInlineAsm(typ types.Type, asm, constraint string) *InlineAsm {
    32  	return &InlineAsm{Typ: typ, Asm: asm, Constraint: constraint}
    33  }
    34  
    35  // String returns the LLVM syntax representation of the inline assembler
    36  // expression as a type-value pair.
    37  func (asm *InlineAsm) String() string {
    38  	return fmt.Sprintf("%s %s", asm.Type(), asm.Ident())
    39  }
    40  
    41  // Type returns the type of the inline assembler expression.
    42  func (asm *InlineAsm) Type() types.Type {
    43  	return asm.Typ
    44  }
    45  
    46  // Ident returns the identifier associated with the inline assembler expression.
    47  func (asm *InlineAsm) Ident() string {
    48  	// "asm" OptSideEffect OptAlignStack OptIntelDialect StringLit "," StringLit
    49  	buf := &strings.Builder{}
    50  	buf.WriteString("asm")
    51  	if asm.SideEffect {
    52  		buf.WriteString(" sideeffect")
    53  	}
    54  	if asm.AlignStack {
    55  		buf.WriteString(" alignstack")
    56  	}
    57  	if asm.IntelDialect {
    58  		buf.WriteString(" inteldialect")
    59  	}
    60  	fmt.Fprintf(buf, " %s, %s", quote(asm.Asm), quote(asm.Constraint))
    61  	return buf.String()
    62  }