/
inst_other.go
803 lines (702 loc) · 22.7 KB
/
inst_other.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
package ir
import (
"fmt"
"strings"
"github.com/llir/llvm/ir/enum"
"github.com/llir/llvm/ir/types"
"github.com/llir/llvm/ir/value"
)
// --- [ Other instructions ] --------------------------------------------------
// ~~~ [ icmp ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstICmp is an LLVM IR icmp instruction.
type InstICmp struct {
// Name of local variable associated with the result.
LocalIdent
// Integer comparison predicate.
Pred enum.IPred
// Integer scalar or vector operands.
X, Y value.Value // integer scalar, pointer, integer vector or pointer vector.
// extra.
// Type of result produced by the instruction.
Typ types.Type // boolean or boolean vector
// (optional) Metadata.
Metadata
}
// NewICmp returns a new icmp instruction based on the given integer comparison
// predicate and integer scalar or vector operands.
func NewICmp(pred enum.IPred, x, y value.Value) *InstICmp {
inst := &InstICmp{Pred: pred, X: x, Y: y}
// Compute type.
inst.Type()
return inst
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstICmp) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction. The result type is either boolean
// type or vector of booleans type.
func (inst *InstICmp) Type() types.Type {
// Cache type if not present.
if inst.Typ == nil {
switch xType := inst.X.Type().(type) {
case *types.IntType, *types.PointerType:
inst.Typ = types.I1
case *types.VectorType:
inst.Typ = types.NewVector(xType.Len, types.I1)
default:
panic(fmt.Errorf("invalid icmp operand type; expected *types.IntType, *types.PointerType or *types.VectorType, got %T", xType))
}
}
return inst.Typ
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'icmp' Pred=IPred X=TypeValue ',' Y=Value Metadata=(',' MetadataAttachment)+?
func (inst *InstICmp) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
fmt.Fprintf(buf, "icmp %s %s, %s", inst.Pred, inst.X, inst.Y.Ident())
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstICmp) Operands() []*value.Value {
return []*value.Value{&inst.X, &inst.Y}
}
// ~~~ [ fcmp ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstFCmp is an LLVM IR fcmp instruction.
type InstFCmp struct {
// Name of local variable associated with the result.
LocalIdent
// Floating-point comparison predicate.
Pred enum.FPred
// Floating-point scalar or vector operands.
X, Y value.Value // floating-point scalar or floating-point vector
// extra.
// Type of result produced by the instruction.
Typ types.Type // boolean or boolean vector
// (optional) Fast math flags.
FastMathFlags []enum.FastMathFlag
// (optional) Metadata.
Metadata
}
// NewFCmp returns a new fcmp instruction based on the given floating-point
// comparison predicate and floating-point scalar or vector operands.
func NewFCmp(pred enum.FPred, x, y value.Value) *InstFCmp {
inst := &InstFCmp{Pred: pred, X: x, Y: y}
// Compute type.
inst.Type()
return inst
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstFCmp) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction. The result type is either boolean
// type or vector of booleans type.
func (inst *InstFCmp) Type() types.Type {
// Cache type if not present.
if inst.Typ == nil {
switch xType := inst.X.Type().(type) {
case *types.FloatType:
inst.Typ = types.I1
case *types.VectorType:
inst.Typ = types.NewVector(xType.Len, types.I1)
default:
panic(fmt.Errorf("invalid fcmp operand type; expected *types.FloatType or *types.VectorType, got %T", xType))
}
}
return inst.Typ
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'fcmp' FastMathFlags=FastMathFlag* Pred=FPred X=TypeValue ',' Y=Value Metadata=(',' MetadataAttachment)+?
func (inst *InstFCmp) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
buf.WriteString("fcmp")
for _, flag := range inst.FastMathFlags {
fmt.Fprintf(buf, " %s", flag)
}
fmt.Fprintf(buf, " %s %s, %s", inst.Pred, inst.X, inst.Y.Ident())
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstFCmp) Operands() []*value.Value {
return []*value.Value{&inst.X, &inst.Y}
}
// ~~~ [ phi ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstPhi is an LLVM IR phi instruction.
type InstPhi struct {
// Name of local variable associated with the result.
LocalIdent
// Incoming values.
Incs []*Incoming
// extra.
// Type of result produced by the instruction.
Typ types.Type // type of incoming value
// (optional) Fast math flags.
FastMathFlags []enum.FastMathFlag
// (optional) Metadata.
Metadata
}
// NewPhi returns a new phi instruction based on the given incoming values.
func NewPhi(incs ...*Incoming) *InstPhi {
inst := &InstPhi{Incs: incs}
// Compute type.
inst.Type()
return inst
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstPhi) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction. The result type is the type of the
// incoming value.
func (inst *InstPhi) Type() types.Type {
// Cache type if not present.
if inst.Typ == nil {
inst.Typ = inst.Incs[0].X.Type()
}
return inst.Typ
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'phi' Typ=Type Incs=(Inc separator ',')+ Metadata=(',' MetadataAttachment)+?
func (inst *InstPhi) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
buf.WriteString("phi ")
for _, flag := range inst.FastMathFlags {
buf.WriteString(flag.String())
buf.WriteString(" ")
}
buf.WriteString(inst.Typ.String())
buf.WriteString(" ")
for i, inc := range inst.Incs {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(inc.String())
}
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstPhi) Operands() []*value.Value {
ops := make([]*value.Value, 0, 2*len(inst.Incs))
for i := range inst.Incs {
ops = append(ops, &inst.Incs[i].X)
ops = append(ops, &inst.Incs[i].Pred)
}
return ops
}
// ___ [ Incoming value ] ______________________________________________________
// Incoming is an incoming value of a phi instruction.
type Incoming struct {
// Incoming value.
X value.Value
// Predecessor basic block of the incoming value.
Pred value.Value // *ir.Block
}
// NewIncoming returns a new incoming value based on the given value and
// predecessor basic block.
func NewIncoming(x value.Value, pred *Block) *Incoming {
return &Incoming{X: x, Pred: pred}
}
// String returns the string representation of the incoming value.
func (inc *Incoming) String() string {
// '[' X=Value ',' Pred=LocalIdent ']'
return fmt.Sprintf("[ %s, %s ]", inc.X.Ident(), inc.Pred.Ident())
}
// ~~~ [ select ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstSelect is an LLVM IR select instruction.
type InstSelect struct {
// Name of local variable associated with the result.
LocalIdent
// Selection condition.
Cond value.Value // boolean or boolean vector
// True condition value.
ValueTrue value.Value
// False condition value.
ValueFalse value.Value
// extra.
// Type of result produced by the instruction.
Typ types.Type
// (optional) Fast math flags.
FastMathFlags []enum.FastMathFlag
// (optional) Metadata.
Metadata
}
// NewSelect returns a new select instruction based on the given selection
// condition and true and false condition values.
func NewSelect(cond, valueTrue, valueFalse value.Value) *InstSelect {
inst := &InstSelect{Cond: cond, ValueTrue: valueTrue, ValueFalse: valueFalse}
// Compute type.
inst.Type()
return inst
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstSelect) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction.
func (inst *InstSelect) Type() types.Type {
// Cache type if not present.
if inst.Typ == nil {
inst.Typ = inst.ValueTrue.Type()
}
return inst.Typ
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'select' FastMathFlags=FastMathFlag* Cond=TypeValue ',' ValueTrue=TypeValue ',' ValueFalse=TypeValue Metadata=(',' MetadataAttachment)+?
func (inst *InstSelect) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
buf.WriteString("select")
for _, flag := range inst.FastMathFlags {
fmt.Fprintf(buf, " %s", flag)
}
fmt.Fprintf(buf, " %s, %s, %s", inst.Cond, inst.ValueTrue, inst.ValueFalse)
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstSelect) Operands() []*value.Value {
return []*value.Value{&inst.Cond, &inst.ValueTrue, &inst.ValueFalse}
}
// ~~~ [ freeze ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstFreeze is an LLVM IR freeze instruction.
type InstFreeze struct {
// Name of local variable associated with the result.
LocalIdent
// Operand.
X value.Value
// extra.
// Type of result produced by the instruction.
Typ types.Type
// (optional) Metadata.
Metadata
}
// NewInstFreeze returns a new freeze instruction based on the given
// operand.
func NewInstFreeze(x value.Value) *InstFreeze {
inst := &InstFreeze{X: x}
// Compute type.
inst.Type()
return inst
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstFreeze) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction.
func (inst *InstFreeze) Type() types.Type {
// Cache type if not present.
if inst.Typ == nil {
inst.Typ = inst.X.Type()
}
return inst.Typ
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'freeze' Type Value
func (inst *InstFreeze) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
fmt.Fprintf(buf, "freeze %s", inst.X)
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstFreeze) Operands() []*value.Value {
return []*value.Value{&inst.X}
}
// ~~~ [ call ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstCall is an LLVM IR call instruction.
type InstCall struct {
// Name of local variable associated with the result.
LocalIdent
// Callee.
// TODO: specify the set of underlying types of Callee.
Callee value.Value
// Function arguments.
//
// Arg has one of the following underlying types:
// value.Value
// *ir.Arg
// TODO: add metadata value?
Args []value.Value
// extra.
// Type of result produced by the instruction.
Typ types.Type
// (optional) Tail; zero if not present.
Tail enum.Tail
// (optional) Fast math flags.
FastMathFlags []enum.FastMathFlag
// (optional) Calling convention; zero if not present.
CallingConv enum.CallingConv
// (optional) Return attributes.
ReturnAttrs []ReturnAttribute
// (optional) Address space; zero if not present.
AddrSpace types.AddrSpace
// (optional) Function attributes.
FuncAttrs []FuncAttribute
// (optional) Operand bundles.
OperandBundles []*OperandBundle
// (optional) Metadata.
Metadata
}
// NewCall returns a new call instruction based on the given callee and function
// arguments.
//
// TODO: specify the set of underlying types of callee.
func NewCall(callee value.Value, args ...value.Value) *InstCall {
inst := &InstCall{Callee: callee, Args: args}
// Compute type.
inst.Type()
return inst
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstCall) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction.
func (inst *InstCall) Type() types.Type {
// Cache type if not present.
if inst.Typ == nil {
sig := inst.Sig()
inst.Typ = sig.RetType
}
return inst.Typ
}
// LLString returns the LLVM syntax representation of the instruction.
//
// Tailopt 'call' FastMathFlags=FastMathFlag* CallingConvopt ReturnAttrs=ReturnAttribute* AddrSpaceopt Typ=Type Callee=Value '(' Args ')' FuncAttrs=FuncAttribute* OperandBundles=('[' (OperandBundle separator ',')+ ']')? Metadata=(',' MetadataAttachment)+?
func (inst *InstCall) LLString() string {
buf := &strings.Builder{}
if !inst.Type().Equal(types.Void) {
fmt.Fprintf(buf, "%s = ", inst.Ident())
}
if inst.Tail != enum.TailNone {
fmt.Fprintf(buf, "%s ", inst.Tail)
}
buf.WriteString("call")
for _, flag := range inst.FastMathFlags {
fmt.Fprintf(buf, " %s", flag)
}
if inst.CallingConv != enum.CallingConvNone {
fmt.Fprintf(buf, " %s", callingConvString(inst.CallingConv))
}
for _, attr := range inst.ReturnAttrs {
fmt.Fprintf(buf, " %s", attr)
}
// (optional) Address space.
if inst.AddrSpace != 0 {
fmt.Fprintf(buf, " %s", inst.AddrSpace)
}
// Use function signature instead of return type for variadic functions.
calleeType := inst.Type()
if sig := inst.Sig(); sig.Variadic {
calleeType = sig
}
fmt.Fprintf(buf, " %s %s(", calleeType, inst.Callee.Ident())
for i, arg := range inst.Args {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(arg.String())
}
buf.WriteString(")")
for _, attr := range inst.FuncAttrs {
fmt.Fprintf(buf, " %s", attr)
}
if len(inst.OperandBundles) > 0 {
buf.WriteString(" [ ")
for i, operandBundle := range inst.OperandBundles {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(operandBundle.String())
}
buf.WriteString(" ]")
}
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstCall) Operands() []*value.Value {
ops := make([]*value.Value, 0, 1+len(inst.Args))
ops = append(ops, &inst.Callee)
for i := range inst.Args {
ops = append(ops, &inst.Args[i])
}
return ops
}
// Sig returns the function signature of the callee.
func (inst *InstCall) Sig() *types.FuncType {
t, ok := inst.Callee.Type().(*types.PointerType)
if !ok {
panic(fmt.Errorf("invalid callee type; expected *types.PointerType, got %T", inst.Callee.Type()))
}
sig, ok := t.ElemType.(*types.FuncType)
if !ok {
panic(fmt.Errorf("invalid callee type; expected *types.FuncType, got %T", t.ElemType))
}
return sig
}
// ~~~ [ va_arg ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstVAArg is an LLVM IR va_arg instruction.
type InstVAArg struct {
// Name of local variable associated with the result.
LocalIdent
// Variable argument list.
ArgList value.Value
// Argument type.
ArgType types.Type
// extra.
// (optional) Metadata.
Metadata
}
// NewVAArg returns a new va_arg instruction based on the given variable
// argument list and argument type.
func NewVAArg(argList value.Value, argType types.Type) *InstVAArg {
return &InstVAArg{ArgList: argList, ArgType: argType}
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstVAArg) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction.
func (inst *InstVAArg) Type() types.Type {
return inst.ArgType
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'va_arg' ArgList=TypeValue ',' ArgType=Type Metadata=(',' MetadataAttachment)+?
func (inst *InstVAArg) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
fmt.Fprintf(buf, "va_arg %s, %s", inst.ArgList, inst.ArgType)
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstVAArg) Operands() []*value.Value {
return []*value.Value{&inst.ArgList}
}
// ~~~ [ landingpad ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstLandingPad is an LLVM IR landingpad instruction.
type InstLandingPad struct {
// Name of local variable associated with the result.
LocalIdent
// Result type.
ResultType types.Type
// (optional) Cleanup landing pad.
Cleanup bool
// Filter and catch clauses; zero or more if Cleanup is true, otherwise one
// or more.
Clauses []*Clause
// extra.
// (optional) Metadata.
Metadata
}
// NewLandingPad returns a new landingpad instruction based on the given result
// type and filter/catch clauses.
func NewLandingPad(resultType types.Type, clauses ...*Clause) *InstLandingPad {
return &InstLandingPad{ResultType: resultType, Clauses: clauses}
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstLandingPad) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction.
func (inst *InstLandingPad) Type() types.Type {
return inst.ResultType
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'landingpad' ResultType=Type Cleanupopt Clauses=Clause* Metadata=(',' MetadataAttachment)+?
func (inst *InstLandingPad) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
fmt.Fprintf(buf, "landingpad %s", inst.ResultType)
if inst.Cleanup {
buf.WriteString("\n\t\tcleanup")
}
for _, clause := range inst.Clauses {
fmt.Fprintf(buf, "\n\t\t%s", clause)
}
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstLandingPad) Operands() []*value.Value {
ops := make([]*value.Value, 0, len(inst.Clauses))
for i := range inst.Clauses {
ops = append(ops, &inst.Clauses[i].X)
}
return ops
}
// ___ [ Landingpad clause ] ___________________________________________________
// Clause is a landingpad catch or filter clause.
type Clause struct {
// Clause type (catch or filter).
Type enum.ClauseType
// Operand.
X value.Value
}
// NewClause returns a new landingpad clause based on the given clause type and
// operand.
func NewClause(clauseType enum.ClauseType, x value.Value) *Clause {
return &Clause{Type: clauseType, X: x}
}
// String returns the string representation of the landingpad clause.
func (clause *Clause) String() string {
return fmt.Sprintf("%s %s", clause.Type, clause.X)
}
// ~~~ [ catchpad ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstCatchPad is an LLVM IR catchpad instruction.
type InstCatchPad struct {
// Name of local variable associated with the result.
LocalIdent
// Parent catchswitch terminator.
CatchSwitch value.Value // *ir.TermCatchSwitch
// Exception arguments.
//
// Arg has one of the following underlying types:
// value.Value
// TODO: add metadata value?
Args []value.Value
// extra.
// (optional) Metadata.
Metadata
}
// NewCatchPad returns a new catchpad instruction based on the given parent
// catchswitch terminator and exception arguments.
func NewCatchPad(catchSwitch *TermCatchSwitch, args ...value.Value) *InstCatchPad {
return &InstCatchPad{CatchSwitch: catchSwitch, Args: args}
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstCatchPad) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction.
func (inst *InstCatchPad) Type() types.Type {
return types.Token
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'catchpad' 'within' CatchSwitch=LocalIdent '[' Args=(ExceptionArg separator ',')* ']' Metadata=(',' MetadataAttachment)+?
func (inst *InstCatchPad) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
fmt.Fprintf(buf, "catchpad within %s [", inst.CatchSwitch.Ident())
for i, arg := range inst.Args {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(arg.String())
}
buf.WriteString("]")
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstCatchPad) Operands() []*value.Value {
ops := make([]*value.Value, 0, 1+len(inst.Args))
ops = append(ops, &inst.CatchSwitch)
for i := range inst.Args {
ops = append(ops, &inst.Args[i])
}
return ops
}
// ~~~ [ cleanuppad ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InstCleanupPad is an LLVM IR cleanuppad instruction.
type InstCleanupPad struct {
// Name of local variable associated with the result.
LocalIdent
// Parent exception pad.
ParentPad value.Value // ir.ExceptionPad
// Exception arguments.
//
// Arg has one of the following underlying types:
// value.Value
// TODO: add metadata value?
Args []value.Value
// extra.
// (optional) Metadata.
Metadata
}
// NewCleanupPad returns a new cleanuppad instruction based on the given
// parent exception pad and exception arguments.
func NewCleanupPad(parentPad ExceptionPad, args ...value.Value) *InstCleanupPad {
return &InstCleanupPad{ParentPad: parentPad, Args: args}
}
// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstCleanupPad) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}
// Type returns the type of the instruction.
func (inst *InstCleanupPad) Type() types.Type {
return types.Token
}
// LLString returns the LLVM syntax representation of the instruction.
//
// 'cleanuppad' 'within' ParentPad=ExceptionPad '[' Args=(ExceptionArg separator ',')* ']' Metadata=(',' MetadataAttachment)+?
func (inst *InstCleanupPad) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
fmt.Fprintf(buf, "cleanuppad within %s [", inst.ParentPad.Ident())
for i, arg := range inst.Args {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(arg.String())
}
buf.WriteString("]")
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
// Operands returns a mutable list of operands of the given instruction.
func (inst *InstCleanupPad) Operands() []*value.Value {
ops := make([]*value.Value, 0, 1+len(inst.Args))
ops = append(ops, &inst.ParentPad)
for i := range inst.Args {
ops = append(ops, &inst.Args[i])
}
return ops
}