-
Notifications
You must be signed in to change notification settings - Fork 80
/
expr_vector.go
186 lines (156 loc) · 5.09 KB
/
expr_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package constant
import (
"fmt"
"github.com/llir/llvm/ir/types"
)
// --- [ Vector expressions ] --------------------------------------------------
// ~~~ [ extractelement ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprExtractElement is an LLVM IR extractelement expression.
type ExprExtractElement struct {
// Vector.
X Constant
// Element index.
Index Constant
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewExtractElement returns a new extractelement expression based on the given
// vector and element index.
func NewExtractElement(x, index Constant) *ExprExtractElement {
e := &ExprExtractElement{X: x, Index: index}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprExtractElement) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprExtractElement) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
t, ok := e.X.Type().(*types.VectorType)
if !ok {
panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.X.Type()))
}
e.Typ = t.ElemType
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprExtractElement) Ident() string {
// 'extractelement' '(' X=TypeConst ',' Index=TypeConst ')'
return fmt.Sprintf("extractelement (%s, %s)", e.X, e.Index)
}
// Simplify returns an equivalent (and potentially simplified) constant to the
// constant expression.
func (e *ExprExtractElement) Simplify() Constant {
//panic("not yet implemented")
// TODO: implement
return e
}
// ~~~ [ insertelement ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprInsertElement is an LLVM IR insertelement expression.
type ExprInsertElement struct {
// Vector.
X Constant
// Element to insert.
Elem Constant
// Element index.
Index Constant
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewInsertElement returns a new insertelement expression based on the given
// vector, element and element index.
func NewInsertElement(x, elem, index Constant) *ExprInsertElement {
e := &ExprInsertElement{X: x, Elem: elem, Index: index}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprInsertElement) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprInsertElement) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
t, ok := e.X.Type().(*types.VectorType)
if !ok {
panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.X.Type()))
}
e.Typ = t
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprInsertElement) Ident() string {
// 'insertelement' '(' X=TypeConst ',' Elem=TypeConst ',' Index=TypeConst ')'
return fmt.Sprintf("insertelement (%s, %s, %s)", e.X, e.Elem, e.Index)
}
// Simplify returns an equivalent (and potentially simplified) constant to the
// constant expression.
func (e *ExprInsertElement) Simplify() Constant {
//panic("not yet implemented")
// TODO: implement
return e
}
// ~~~ [ shufflevector ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprShuffleVector is an LLVM IR shufflevector expression.
type ExprShuffleVector struct {
// Vectors.
X, Y Constant
// Shuffle mask.
Mask Constant
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewShuffleVector returns a new shufflevector expression based on the given
// vectors and shuffle mask.
func NewShuffleVector(x, y, mask Constant) *ExprShuffleVector {
e := &ExprShuffleVector{X: x, Y: y, Mask: mask}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprShuffleVector) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprShuffleVector) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
xType, ok := e.X.Type().(*types.VectorType)
if !ok {
panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.X.Type()))
}
maskType, ok := e.Mask.Type().(*types.VectorType)
if !ok {
panic(fmt.Errorf("invalid vector type; expected *types.VectorType, got %T", e.Mask.Type()))
}
e.Typ = types.NewVector(maskType.Len, xType.ElemType)
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprShuffleVector) Ident() string {
// 'shufflevector' '(' X=TypeConst ',' Y=TypeConst ',' Mask=TypeConst ')'
return fmt.Sprintf("shufflevector (%s, %s, %s)", e.X, e.Y, e.Mask)
}
// Simplify returns an equivalent (and potentially simplified) constant to the
// constant expression.
func (e *ExprShuffleVector) Simplify() Constant {
//panic("not yet implemented")
// TODO: implement
return e
}