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

     1  package constant
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/llir/llvm/ir/types"
     7  )
     8  
     9  // --- [ Vector expressions ] --------------------------------------------------
    10  
    11  // ~~~ [ extractelement ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    12  
    13  // ExprExtractElement is an LLVM IR extractelement expression.
    14  type ExprExtractElement struct {
    15  	// Vector.
    16  	X Constant
    17  	// Element index.
    18  	Index Constant
    19  
    20  	// extra.
    21  
    22  	// Type of result produced by the constant expression.
    23  	Typ types.Type
    24  }
    25  
    26  // NewExtractElement returns a new extractelement expression based on the given
    27  // vector and element index.
    28  func NewExtractElement(x, index Constant) *ExprExtractElement {
    29  	e := &ExprExtractElement{X: x, Index: index}
    30  	// Compute type.
    31  	e.Type()
    32  	return e
    33  }
    34  
    35  // String returns the LLVM syntax representation of the constant expression as a
    36  // type-value pair.
    37  func (e *ExprExtractElement) String() string {
    38  	return fmt.Sprintf("%s %s", e.Type(), e.Ident())
    39  }
    40  
    41  // Type returns the type of the constant expression.
    42  func (e *ExprExtractElement) Type() types.Type {
    43  	// Cache type if not present.
    44  	if e.Typ == nil {
    45  		t, ok := e.X.Type().(*types.VectorType)
    46  		if !ok {
    47  			panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.X.Type()))
    48  		}
    49  		e.Typ = t.ElemType
    50  	}
    51  	return e.Typ
    52  }
    53  
    54  // Ident returns the identifier associated with the constant expression.
    55  func (e *ExprExtractElement) Ident() string {
    56  	// 'extractelement' '(' X=TypeConst ',' Index=TypeConst ')'
    57  	return fmt.Sprintf("extractelement (%s, %s)", e.X, e.Index)
    58  }
    59  
    60  // ~~~ [ insertelement ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    61  
    62  // ExprInsertElement is an LLVM IR insertelement expression.
    63  type ExprInsertElement struct {
    64  	// Vector.
    65  	X Constant
    66  	// Element to insert.
    67  	Elem Constant
    68  	// Element index.
    69  	Index Constant
    70  
    71  	// extra.
    72  
    73  	// Type of result produced by the constant expression.
    74  	Typ types.Type
    75  }
    76  
    77  // NewInsertElement returns a new insertelement expression based on the given
    78  // vector, element and element index.
    79  func NewInsertElement(x, elem, index Constant) *ExprInsertElement {
    80  	e := &ExprInsertElement{X: x, Elem: elem, Index: index}
    81  	// Compute type.
    82  	e.Type()
    83  	return e
    84  }
    85  
    86  // String returns the LLVM syntax representation of the constant expression as a
    87  // type-value pair.
    88  func (e *ExprInsertElement) String() string {
    89  	return fmt.Sprintf("%s %s", e.Type(), e.Ident())
    90  }
    91  
    92  // Type returns the type of the constant expression.
    93  func (e *ExprInsertElement) Type() types.Type {
    94  	// Cache type if not present.
    95  	if e.Typ == nil {
    96  		t, ok := e.X.Type().(*types.VectorType)
    97  		if !ok {
    98  			panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.X.Type()))
    99  		}
   100  		e.Typ = t
   101  	}
   102  	return e.Typ
   103  }
   104  
   105  // Ident returns the identifier associated with the constant expression.
   106  func (e *ExprInsertElement) Ident() string {
   107  	// 'insertelement' '(' X=TypeConst ',' Elem=TypeConst ',' Index=TypeConst ')'
   108  	return fmt.Sprintf("insertelement (%s, %s, %s)", e.X, e.Elem, e.Index)
   109  }
   110  
   111  // ~~~ [ shufflevector ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   112  
   113  // ExprShuffleVector is an LLVM IR shufflevector expression.
   114  type ExprShuffleVector struct {
   115  	// Vectors.
   116  	X, Y Constant
   117  	// Shuffle mask.
   118  	Mask Constant
   119  
   120  	// extra.
   121  
   122  	// Type of result produced by the constant expression.
   123  	Typ types.Type
   124  }
   125  
   126  // NewShuffleVector returns a new shufflevector expression based on the given
   127  // vectors and shuffle mask.
   128  func NewShuffleVector(x, y, mask Constant) *ExprShuffleVector {
   129  	e := &ExprShuffleVector{X: x, Y: y, Mask: mask}
   130  	// Compute type.
   131  	e.Type()
   132  	return e
   133  }
   134  
   135  // String returns the LLVM syntax representation of the constant expression as a
   136  // type-value pair.
   137  func (e *ExprShuffleVector) String() string {
   138  	return fmt.Sprintf("%s %s", e.Type(), e.Ident())
   139  }
   140  
   141  // Type returns the type of the constant expression.
   142  func (e *ExprShuffleVector) Type() types.Type {
   143  	// Cache type if not present.
   144  	if e.Typ == nil {
   145  		xType, ok := e.X.Type().(*types.VectorType)
   146  		if !ok {
   147  			panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.X.Type()))
   148  		}
   149  		maskType, ok := e.Mask.Type().(*types.VectorType)
   150  		if !ok {
   151  			panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.Mask.Type()))
   152  		}
   153  		e.Typ = types.NewVector(maskType.Len, xType.ElemType)
   154  	}
   155  	return e.Typ
   156  }
   157  
   158  // Ident returns the identifier associated with the constant expression.
   159  func (e *ExprShuffleVector) Ident() string {
   160  	// 'shufflevector' '(' X=TypeConst ',' Y=TypeConst ',' Mask=TypeConst ')'
   161  	return fmt.Sprintf("shufflevector (%s, %s, %s)", e.X, e.Y, e.Mask)
   162  }