/
expr_binary.go
555 lines (456 loc) · 13.8 KB
/
expr_binary.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package constant
import (
"fmt"
"strings"
"github.com/llir/llvm/ir/enum"
"github.com/llir/llvm/ir/types"
)
// --- [ Binary expressions ] --------------------------------------------------
// ~~~ [ add ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprAdd is an LLVM IR add expression.
type ExprAdd struct {
// Operands.
X, Y Constant // integer scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
// (optional) Integer overflow flags.
OverflowFlags []enum.OverflowFlag
}
// NewAdd returns a new add expression based on the given operands.
func NewAdd(x, y Constant) *ExprAdd {
e := &ExprAdd{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprAdd) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprAdd) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprAdd) Ident() string {
// 'add' OverflowFlags=OverflowFlag* '(' X=TypeConst ',' Y=TypeConst ')'
buf := &strings.Builder{}
buf.WriteString("add")
for _, flag := range e.OverflowFlags {
fmt.Fprintf(buf, " %s", flag)
}
fmt.Fprintf(buf, " (%s, %s)", e.X, e.Y)
return buf.String()
}
// ~~~ [ fadd ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprFAdd is an LLVM IR fadd expression.
type ExprFAdd struct {
// Operands.
X, Y Constant // floating-point scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewFAdd returns a new fadd expression based on the given operands.
func NewFAdd(x, y Constant) *ExprFAdd {
e := &ExprFAdd{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprFAdd) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprFAdd) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprFAdd) Ident() string {
// 'fadd' '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("fadd (%s, %s)", e.X, e.Y)
}
// ~~~ [ sub ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprSub is an LLVM IR sub expression.
type ExprSub struct {
// Operands.
X, Y Constant // integer scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
// (optional) Integer overflow flags.
OverflowFlags []enum.OverflowFlag
}
// NewSub returns a new sub expression based on the given operands.
func NewSub(x, y Constant) *ExprSub {
e := &ExprSub{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprSub) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprSub) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprSub) Ident() string {
// 'sub' OverflowFlags=OverflowFlag* '(' X=TypeConst ',' Y=TypeConst ')'
buf := &strings.Builder{}
buf.WriteString("sub")
for _, flag := range e.OverflowFlags {
fmt.Fprintf(buf, " %s", flag)
}
fmt.Fprintf(buf, " (%s, %s)", e.X, e.Y)
return buf.String()
}
// ~~~ [ fsub ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprFSub is an LLVM IR fsub expression.
type ExprFSub struct {
// Operands.
X, Y Constant // floating-point scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewFSub returns a new fsub expression based on the given operands.
func NewFSub(x, y Constant) *ExprFSub {
e := &ExprFSub{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprFSub) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprFSub) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprFSub) Ident() string {
// 'fsub' '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("fsub (%s, %s)", e.X, e.Y)
}
// ~~~ [ mul ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprMul is an LLVM IR mul expression.
type ExprMul struct {
// Operands.
X, Y Constant // integer scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
// (optional) Integer overflow flags.
OverflowFlags []enum.OverflowFlag
}
// NewMul returns a new mul expression based on the given operands.
func NewMul(x, y Constant) *ExprMul {
e := &ExprMul{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprMul) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprMul) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprMul) Ident() string {
// 'mul' OverflowFlags=OverflowFlag* '(' X=TypeConst ',' Y=TypeConst ')'
buf := &strings.Builder{}
buf.WriteString("mul")
for _, flag := range e.OverflowFlags {
fmt.Fprintf(buf, " %s", flag)
}
fmt.Fprintf(buf, " (%s, %s)", e.X, e.Y)
return buf.String()
}
// ~~~ [ fmul ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprFMul is an LLVM IR fmul expression.
type ExprFMul struct {
// Operands.
X, Y Constant // floating-point scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewFMul returns a new fmul expression based on the given operands.
func NewFMul(x, y Constant) *ExprFMul {
e := &ExprFMul{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprFMul) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprFMul) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprFMul) Ident() string {
// 'fmul' '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("fmul (%s, %s)", e.X, e.Y)
}
// ~~~ [ udiv ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprUDiv is an LLVM IR udiv expression.
type ExprUDiv struct {
// Operands.
X, Y Constant // integer scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
// (optional) The result is a poison value if X is not a multiple of Y.
Exact bool
}
// NewUDiv returns a new udiv expression based on the given operands.
func NewUDiv(x, y Constant) *ExprUDiv {
e := &ExprUDiv{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprUDiv) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprUDiv) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprUDiv) Ident() string {
// 'udiv' Exactopt '(' X=TypeConst ',' Y=TypeConst ')'
buf := &strings.Builder{}
buf.WriteString("udiv")
if e.Exact {
buf.WriteString(" exact")
}
fmt.Fprintf(buf, " (%s, %s)", e.X, e.Y)
return buf.String()
}
// ~~~ [ sdiv ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprSDiv is an LLVM IR sdiv expression.
type ExprSDiv struct {
// Operands.
X, Y Constant // integer scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
// (optional) The result is a poison value if the result would be rounded.
Exact bool
}
// NewSDiv returns a new sdiv expression based on the given operands.
func NewSDiv(x, y Constant) *ExprSDiv {
e := &ExprSDiv{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprSDiv) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprSDiv) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprSDiv) Ident() string {
// 'sdiv' Exactopt '(' X=TypeConst ',' Y=TypeConst ')'
buf := &strings.Builder{}
buf.WriteString("sdiv")
if e.Exact {
buf.WriteString(" exact")
}
fmt.Fprintf(buf, " (%s, %s)", e.X, e.Y)
return buf.String()
}
// ~~~ [ fdiv ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprFDiv is an LLVM IR fdiv expression.
type ExprFDiv struct {
// Operands.
X, Y Constant // floating-point scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewFDiv returns a new fdiv expression based on the given operands.
func NewFDiv(x, y Constant) *ExprFDiv {
e := &ExprFDiv{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprFDiv) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprFDiv) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprFDiv) Ident() string {
// 'fdiv' '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("fdiv (%s, %s)", e.X, e.Y)
}
// ~~~ [ urem ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprURem is an LLVM IR urem expression.
type ExprURem struct {
// Operands.
X, Y Constant // integer scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewURem returns a new urem expression based on the given operands.
func NewURem(x, y Constant) *ExprURem {
e := &ExprURem{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprURem) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprURem) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprURem) Ident() string {
// 'urem' '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("urem (%s, %s)", e.X, e.Y)
}
// ~~~ [ srem ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprSRem is an LLVM IR srem expression.
type ExprSRem struct {
// Operands.
X, Y Constant // integer scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewSRem returns a new srem expression based on the given operands.
func NewSRem(x, y Constant) *ExprSRem {
e := &ExprSRem{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprSRem) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprSRem) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprSRem) Ident() string {
// 'srem' '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("srem (%s, %s)", e.X, e.Y)
}
// ~~~ [ frem ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprFRem is an LLVM IR frem expression.
type ExprFRem struct {
// Operands.
X, Y Constant // floating-point scalar or vector constants
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewFRem returns a new frem expression based on the given operands.
func NewFRem(x, y Constant) *ExprFRem {
e := &ExprFRem{X: x, Y: y}
// Compute type.
e.Type()
return e
}
// String returns the LLVM syntax representation of the constant expression as a
// type-value pair.
func (e *ExprFRem) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprFRem) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
e.Typ = e.X.Type()
}
return e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprFRem) Ident() string {
// 'frem' '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("frem (%s, %s)", e.X, e.Y)
}