-
Notifications
You must be signed in to change notification settings - Fork 80
/
expr_other.go
183 lines (153 loc) · 4.88 KB
/
expr_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
package constant
import (
"fmt"
"github.com/llir/llvm/ir/enum"
"github.com/llir/llvm/ir/types"
)
// --- [ Other expressions ] ---------------------------------------------------
// ~~~ [ icmp ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprICmp is an LLVM IR icmp expression.
type ExprICmp struct {
// Integer comparison predicate.
Pred enum.IPred
// Integer scalar or vector operands.
X, Y Constant
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewICmp returns a new icmp expression based on the given integer comparison
// predicate and integer scalar or vector operands.
func NewICmp(pred enum.IPred, x, y Constant) *ExprICmp {
e := &ExprICmp{Pred: pred, 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 *ExprICmp) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprICmp) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
switch xType := e.X.Type().(type) {
case *types.IntType, *types.PointerType:
e.Typ = types.I1
case *types.VectorType:
e.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 e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprICmp) Ident() string {
// 'icmp' Pred=IPred '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("icmp %s (%s, %s)", e.Pred, e.X, e.Y)
}
// Simplify returns an equivalent (and potentially simplified) constant to the
// constant expression.
func (e *ExprICmp) Simplify() Constant {
//panic("not yet implemented")
// TODO: implement
return e
}
// ~~~ [ fcmp ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprFCmp is an LLVM IR fcmp expression.
type ExprFCmp struct {
// Floating-point comparison predicate.
Pred enum.FPred
// Floating-point scalar or vector operands.
X, Y Constant
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewFCmp returns a new fcmp expression based on the given floating-point
// comparison predicate and floating-point scalar or vector operands.
func NewFCmp(pred enum.FPred, x, y Constant) *ExprFCmp {
e := &ExprFCmp{Pred: pred, 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 *ExprFCmp) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprFCmp) Type() types.Type {
// Cache type if not present.
if e.Typ == nil {
switch xType := e.X.Type().(type) {
case *types.FloatType:
e.Typ = types.I1
case *types.VectorType:
e.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 e.Typ
}
// Ident returns the identifier associated with the constant expression.
func (e *ExprFCmp) Ident() string {
// 'fcmp' Pred=FPred '(' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("fcmp %s (%s, %s)", e.Pred, e.X, e.Y)
}
// Simplify returns an equivalent (and potentially simplified) constant to the
// constant expression.
func (e *ExprFCmp) Simplify() Constant {
//panic("not yet implemented")
// TODO: implement
return e
}
// ~~~ [ select ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ExprSelect is an LLVM IR select expression.
type ExprSelect struct {
// Selection condition.
Cond Constant
// Operands.
X, Y Constant
// extra.
// Type of result produced by the constant expression.
Typ types.Type
}
// NewSelect returns a new select expression based on the given selection
// condition and operands.
func NewSelect(cond, x, y Constant) *ExprSelect {
e := &ExprSelect{Cond: cond, 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 *ExprSelect) String() string {
return fmt.Sprintf("%s %s", e.Type(), e.Ident())
}
// Type returns the type of the constant expression.
func (e *ExprSelect) 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 *ExprSelect) Ident() string {
// 'select' '(' Cond=TypeConst ',' X=TypeConst ',' Y=TypeConst ')'
return fmt.Sprintf("select (%s, %s, %s)", e.Cond, e.X, e.Y)
}
// Simplify returns an equivalent (and potentially simplified) constant to the
// constant expression.
func (e *ExprSelect) Simplify() Constant {
//panic("not yet implemented")
// TODO: implement
return e
}