-
Notifications
You must be signed in to change notification settings - Fork 80
/
const_vector.go
62 lines (54 loc) · 1.35 KB
/
const_vector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package constant
import (
"fmt"
"strings"
"github.com/llir/llvm/ir/types"
)
// --- [ Vector constants ] ----------------------------------------------------
// Vector is an LLVM IR vector constant.
type Vector struct {
// Vector type.
Typ *types.VectorType
// Vector elements.
Elems []Constant
}
// NewVector returns a new vector constant based on the given vector type and
// elements. The vector type is infered from the type of the elements if t is
// nil.
func NewVector(t *types.VectorType, elems ...Constant) *Vector {
c := &Vector{
Elems: elems,
Typ: t,
}
// Compute type.
c.Type()
return c
}
// String returns the LLVM syntax representation of the constant as a type-value
// pair.
func (c *Vector) String() string {
return fmt.Sprintf("%s %s", c.Type(), c.Ident())
}
// Type returns the type of the constant.
func (c *Vector) Type() types.Type {
// Cache type if not present.
if c.Typ == nil {
elemType := c.Elems[0].Type()
c.Typ = types.NewVector(uint64(len(c.Elems)), elemType)
}
return c.Typ
}
// Ident returns the identifier associated with the constant.
func (c *Vector) Ident() string {
// '<' Elems=(TypeConst separator ',')* '>'
buf := &strings.Builder{}
buf.WriteString("<")
for i, elem := range c.Elems {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(elem.String())
}
buf.WriteString(">")
return buf.String()
}