/
arithmetic.go
363 lines (328 loc) · 11.3 KB
/
arithmetic.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codegen
import (
"fmt"
"reflect"
"llvm/bindings/go/llvm"
)
// Zero returns a zero-value of the specified type.
func (b *Builder) Zero(ty Type) *Value {
if ty == b.m.Types.Void {
return nil
}
return b.val(ty, llvm.ConstNull(ty.llvmTy())).SetName("zero")
}
// One returns a one-value of the specified bool, int or float type.
func (b *Builder) One(ty Type) *Value {
var value *Value
switch {
case ty == b.m.Types.Bool:
value = b.val(ty, llvm.ConstInt(ty.llvmTy(), 1, false))
case IsInteger(ty):
value = b.val(ty, llvm.ConstInt(ty.llvmTy(), 1, false))
case IsFloat(ty):
value = b.val(ty, llvm.ConstFloat(ty.llvmTy(), 1))
default:
fail("One does not support type %T", ty)
}
value.SetName("1")
return value
}
// Not returns !x. The type of x must be Bool.
func (b *Builder) Not(x *Value) *Value {
assertTypesEqual(x.ty, b.m.Types.Bool)
return b.val(b.m.Types.Bool, b.llvm.CreateNot(x.llvm, "!"+x.Name()))
}
// Invert returns ~x.
func (b *Builder) Invert(x *Value) *Value {
return b.val(x.ty, b.llvm.CreateNot(x.llvm, ""))
}
// Negate returns -x. The type of x must be a signed integer or float.
func (b *Builder) Negate(x *Value) *Value {
ty := Scalar(x.Type())
name := "-" + x.Name()
switch {
case IsSignedInteger(ty):
return b.val(ty, b.llvm.CreateNeg(x.llvm, name))
case IsFloat(ty):
return b.val(ty, b.llvm.CreateFNeg(x.llvm, name))
default:
panic(fmt.Errorf("Cannot divide values of type %v", ty))
}
}
func (b *Builder) cmp(x *Value, op string, y *Value, ucmp, scmp llvm.IntPredicate, fcmp llvm.FloatPredicate) *Value {
assertTypesEqual(x.Type(), y.Type())
ty := x.Type()
if vec, isVec := ty.(Vector); isVec {
ty = vec.Element
}
var v *Value
switch {
case IsSignedInteger(ty):
v = b.val(b.m.Types.Bool, b.llvm.CreateICmp(scmp, x.llvm, y.llvm, ""))
case IsUnsignedInteger(ty), IsPointer(ty), IsBool(ty):
v = b.val(b.m.Types.Bool, b.llvm.CreateICmp(ucmp, x.llvm, y.llvm, ""))
case IsFloat(ty):
v = b.val(b.m.Types.Bool, b.llvm.CreateFCmp(fcmp, x.llvm, y.llvm, ""))
default:
panic(fmt.Errorf("Cannot compare %v types with %v", ty.TypeName(), op))
}
v.SetName(x.Name() + op + y.Name())
return v
}
// Equal returns x == y. The types of the two values must be equal.
func (b *Builder) Equal(x, y *Value) *Value {
ty := x.Type()
if ty, ok := ty.(*Struct); ok {
assertTypesEqual(x.Type(), y.Type())
var eq *Value
for i, f := range ty.Fields() {
x, y := x.Extract(f.Name), y.Extract(f.Name)
if i == 0 {
eq = b.Equal(x, y)
} else {
eq = b.And(eq, b.Equal(x, y))
}
}
return eq
}
return b.cmp(x, "==", y, llvm.IntEQ, llvm.IntEQ, llvm.FloatOEQ)
}
// NotEqual returns x != y. The types of the two values must be equal.
func (b *Builder) NotEqual(x, y *Value) *Value {
ty := x.Type()
if ty, ok := ty.(*Struct); ok {
assertTypesEqual(x.Type(), y.Type())
var neq *Value
for i, f := range ty.Fields() {
x, y := x.Extract(f.Name), y.Extract(f.Name)
if i == 0 {
neq = b.NotEqual(x, y)
} else {
neq = b.Or(neq, b.NotEqual(x, y))
}
}
return neq
}
return b.cmp(x, "!=", y, llvm.IntNE, llvm.IntNE, llvm.FloatONE)
}
// GreaterThan returns x > y. The types of the two values must be equal.
func (b *Builder) GreaterThan(x, y *Value) *Value {
return b.cmp(x, ">", y, llvm.IntUGT, llvm.IntSGT, llvm.FloatOGT)
}
// LessThan returns x < y. The types of the two values must be equal.
func (b *Builder) LessThan(x, y *Value) *Value {
return b.cmp(x, "<", y, llvm.IntULT, llvm.IntSLT, llvm.FloatOLT)
}
// GreaterOrEqualTo returns x >= y. The types of the two values must be equal.
func (b *Builder) GreaterOrEqualTo(x, y *Value) *Value {
return b.cmp(x, ">=", y, llvm.IntUGE, llvm.IntSGE, llvm.FloatOGE)
}
// LessOrEqualTo returns x <= y. The types of the two values must be equal.
func (b *Builder) LessOrEqualTo(x, y *Value) *Value {
return b.cmp(x, "<=", y, llvm.IntULE, llvm.IntSLE, llvm.FloatOLE)
}
// Add returns x + y. The types of the two values must be equal.
func (b *Builder) Add(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
ty := Scalar(x.Type())
switch {
case IsInteger(ty):
return b.val(x.Type(), b.llvm.CreateAdd(x.llvm, y.llvm, x.Name()+"+"+y.Name()))
case IsFloat(ty):
return b.val(x.Type(), b.llvm.CreateFAdd(x.llvm, y.llvm, x.Name()+"+"+y.Name()))
default:
panic(fmt.Errorf("Cannot add values of type %v", ty))
}
}
// AddS returns x + y. The types of the two values must be equal.
func (b *Builder) AddS(x *Value, y interface{}) *Value {
return b.Add(x, b.Scalar(y))
}
// Sub returns x - y. The types of the two values must be equal.
func (b *Builder) Sub(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
ty := Scalar(x.Type())
switch {
case IsInteger(ty):
return b.val(x.Type(), b.llvm.CreateSub(x.llvm, y.llvm, x.Name()+"-"+y.Name()))
case IsFloat(ty):
return b.val(x.Type(), b.llvm.CreateFSub(x.llvm, y.llvm, x.Name()+"-"+y.Name()))
default:
panic(fmt.Errorf("Cannot subtract values of type %v", ty))
}
}
// SubS returns x - y. The types of the two values must be equal.
func (b *Builder) SubS(x *Value, y interface{}) *Value {
return b.Sub(x, b.Scalar(y))
}
// Mul returns x * y. The types of the two values must be equal.
func (b *Builder) Mul(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
ty := Scalar(x.Type())
switch {
case IsInteger(ty):
return b.val(x.Type(), b.llvm.CreateMul(x.llvm, y.llvm, x.Name()+"*"+y.Name()))
case IsFloat(ty):
return b.val(x.Type(), b.llvm.CreateFMul(x.llvm, y.llvm, x.Name()+"*"+y.Name()))
default:
panic(fmt.Errorf("Cannot multiply values of type %v", ty))
}
}
// MulS returns x * y. The types of the two values must be equal.
func (b *Builder) MulS(x *Value, y interface{}) *Value {
return b.Mul(x, b.Scalar(y))
}
// Div returns x / y. The types of the two values must be equal.
func (b *Builder) Div(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
ty := Scalar(x.Type())
name := x.Name() + "/" + y.Name()
switch {
case IsSignedInteger(ty):
return b.val(ty, b.llvm.CreateSDiv(x.llvm, y.llvm, name))
case IsUnsignedInteger(ty):
return b.val(ty, b.llvm.CreateUDiv(x.llvm, y.llvm, name))
case IsFloat(ty):
return b.val(ty, b.llvm.CreateFDiv(x.llvm, y.llvm, name))
default:
panic(fmt.Errorf("Cannot divide values of type %v", ty))
}
}
// Rem returns x % y. The types of the two values must be equal.
func (b *Builder) Rem(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
ty := Scalar(x.Type())
name := x.Name() + "%" + y.Name()
switch {
case IsSignedInteger(ty):
return b.val(ty, b.llvm.CreateSRem(x.llvm, y.llvm, name))
case IsUnsignedInteger(ty):
return b.val(ty, b.llvm.CreateURem(x.llvm, y.llvm, name))
case IsFloat(ty):
return b.val(ty, b.llvm.CreateFRem(x.llvm, y.llvm, name))
default:
panic(fmt.Errorf("Cannot divide values of type %v", ty))
}
}
// DivS returns x / y. The types of the two values must be equal.
func (b *Builder) DivS(x *Value, y interface{}) *Value {
return b.Div(x, b.Scalar(y))
}
// And performs a bitwise-and of the two integers.
// The types of the two values must be equal.
func (b *Builder) And(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
return b.val(x.Type(), b.llvm.CreateAnd(x.llvm, y.llvm, x.Name()+"&"+y.Name()))
}
// Or performs a bitwise-or of the two integers.
// The types of the two values must be equal.
func (b *Builder) Or(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
return b.val(x.Type(), b.llvm.CreateOr(x.llvm, y.llvm, x.Name()+"|"+y.Name()))
}
// Xor performs a bitwise-xor of the two integers.
// The types of the two values must be equal.
func (b *Builder) Xor(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
return b.val(x.Type(), b.llvm.CreateXor(x.llvm, y.llvm, x.Name()+"^"+y.Name()))
}
// Shuffle performs a vector-shuffle operation of the two vector values x and y
// with the specified indices.
func (b *Builder) Shuffle(x, y, indices *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
assertVectorsSameLength(x.Type(), y.Type())
dataTy, ok := x.Type().(Vector)
if !ok {
panic(fmt.Errorf("Shuffle must be passed vector types, got: %v", x.Type()))
}
indicesTy, ok := indices.Type().(Vector)
if !ok || indicesTy.Element != b.m.Types.Int32 {
panic(fmt.Errorf("Shuffle indices must be a vector of Int32, got: %v", indices.Type()))
}
ty := b.m.Types.Vector(dataTy.Element, indicesTy.Count)
return b.val(ty, b.llvm.CreateShuffleVector(x.llvm, y.llvm, indices.llvm, "shuffle"))
}
// ShiftLeft performs a bit-shift left by shift bits.
func (b *Builder) ShiftLeft(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
return b.val(x.Type(), b.llvm.CreateShl(x.llvm, y.llvm, x.Name()+"<<"+y.Name()))
}
// ShiftRight performs a bit-shift right by shift bits.
func (b *Builder) ShiftRight(x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
ty := x.Type()
switch {
case IsSignedInteger(ty):
return b.val(ty, b.llvm.CreateAShr(x.llvm, y.llvm, x.Name()+">>"+y.Name()))
case IsUnsignedInteger(ty):
return b.val(ty, b.llvm.CreateLShr(x.llvm, y.llvm, x.Name()+">>"+y.Name()))
default:
panic(fmt.Errorf("Cannot divide values of type %v", ty))
}
}
// Select returns (cond ? x : y). x and y must be of the same type.
func (b *Builder) Select(cond, x, y *Value) *Value {
assertTypesEqual(x.Type(), y.Type())
assertTypesEqual(cond.Type(), b.m.Types.Bool)
return b.val(x.Type(), b.llvm.CreateSelect(cond.llvm, x.llvm, y.llvm,
fmt.Sprintf("(%v?%v:%v)", cond.Name(), x.Name(), y.Name())))
}
// Scalar returns a constant scalar with the value v.
func (b *Builder) Scalar(v interface{}) *Value {
ty := b.m.Types.TypeOf(v)
switch {
case IsStruct(ty):
ty := ty.(*Struct)
val := llvm.Undef(ty.llvm)
r := reflect.ValueOf(v)
for i, c := 0, r.NumField(); i < c; i++ {
f := b.Scalar(r.Field(i).Interface()).llvm
val = b.llvm.CreateInsertValue(val, f, i, "")
}
return b.val(ty, val)
default:
return b.m.Scalar(v).Value(b)
}
}
// Vector returns a constant vector with the specified values.
func (b *Builder) Vector(el0 interface{}, els ...interface{}) *Value {
types := make([]Type, len(els)+1)
values := make([]llvm.Value, len(els)+1)
v := b.Scalar(el0)
types[0] = v.Type()
values[0] = v.llvm
allSameType := true
for i, el := range els {
v := b.Scalar(el)
types[i+1] = v.Type()
values[i+1] = v.llvm
allSameType = types[0] == types[i+1]
}
if !allSameType {
fail("Vector passed mix of element types: %v", types)
}
return b.val(b.m.Types.Vector(types[0], len(values)), llvm.ConstVector(values, false))
}
// VectorN returns a constant vector of n elements with the same value v in each
// element.
func (b *Builder) VectorN(n int, v interface{}) *Value {
values := make([]llvm.Value, n)
s := b.Scalar(v)
for i := range values {
values[i] = s.llvm
}
return b.val(b.m.Types.Vector(s.Type(), len(values)), llvm.ConstVector(values, false))
}