2
2
import cllvm
3
3
#endif
4
4
5
- public struct Constant : IRValue {
5
+ public protocol ConstantRepresentation { }
6
+ public enum Unsigned : ConstantRepresentation { }
7
+ public enum Signed : ConstantRepresentation { }
8
+ public enum Floating : ConstantRepresentation { }
9
+
10
+ public struct Constant < Repr: ConstantRepresentation > : IRValue {
6
11
internal enum Representation {
7
12
case unsigned
8
13
case signed
@@ -17,9 +22,18 @@ public struct Constant: IRValue {
17
22
return llvm
18
23
}
19
24
20
- internal init ( llvm: LLVMValueRef ! , representation : Representation ) {
25
+ public init ( llvm: LLVMValueRef ! ) {
21
26
self . llvm = llvm
22
- self . repr = representation
27
+
28
+ if ObjectIdentifier ( Repr . self) == ObjectIdentifier ( Unsigned . self) {
29
+ self . repr = . unsigned
30
+ } else if ObjectIdentifier ( Repr . self) == ObjectIdentifier ( Signed . self) {
31
+ self . repr = . signed
32
+ } else if ObjectIdentifier ( Repr . self) == ObjectIdentifier ( Floating . self) {
33
+ self . repr = . floating
34
+ } else {
35
+ fatalError ( " Invalid representation \( type ( of: Repr . self) ) " )
36
+ }
23
37
}
24
38
25
39
public static func + ( lhs: Constant , rhs: Constant ) -> Constant {
@@ -28,9 +42,9 @@ public struct Constant: IRValue {
28
42
switch lhs. repr {
29
43
case . signed: fallthrough
30
44
case . unsigned:
31
- return Constant ( llvm: LLVMConstAdd ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
45
+ return Constant ( llvm: LLVMConstAdd ( lhs. llvm, rhs. llvm) )
32
46
case . floating:
33
- return Constant ( llvm: LLVMConstFAdd ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
47
+ return Constant ( llvm: LLVMConstFAdd ( lhs. llvm, rhs. llvm) )
34
48
}
35
49
}
36
50
@@ -40,9 +54,9 @@ public struct Constant: IRValue {
40
54
switch lhs. repr {
41
55
case . signed: fallthrough
42
56
case . unsigned:
43
- return Constant ( llvm: LLVMConstSub ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
57
+ return Constant ( llvm: LLVMConstSub ( lhs. llvm, rhs. llvm) )
44
58
case . floating:
45
- return Constant ( llvm: LLVMConstFSub ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
59
+ return Constant ( llvm: LLVMConstFSub ( lhs. llvm, rhs. llvm) )
46
60
}
47
61
}
48
62
@@ -52,9 +66,9 @@ public struct Constant: IRValue {
52
66
switch lhs. repr {
53
67
case . signed: fallthrough
54
68
case . unsigned:
55
- return Constant ( llvm: LLVMConstMul ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
69
+ return Constant ( llvm: LLVMConstMul ( lhs. llvm, rhs. llvm) )
56
70
case . floating:
57
- return Constant ( llvm: LLVMConstFMul ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
71
+ return Constant ( llvm: LLVMConstFMul ( lhs. llvm, rhs. llvm) )
58
72
}
59
73
}
60
74
@@ -63,30 +77,30 @@ public struct Constant: IRValue {
63
77
64
78
switch lhs. repr {
65
79
case . signed:
66
- return Constant ( llvm: LLVMConstSDiv ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
80
+ return Constant ( llvm: LLVMConstSDiv ( lhs. llvm, rhs. llvm) )
67
81
case . unsigned:
68
- return Constant ( llvm: LLVMConstUDiv ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
82
+ return Constant ( llvm: LLVMConstUDiv ( lhs. llvm, rhs. llvm) )
69
83
case . floating:
70
- return Constant ( llvm: LLVMConstFDiv ( lhs. llvm, rhs. llvm) , representation : lhs . repr )
84
+ return Constant ( llvm: LLVMConstFDiv ( lhs. llvm, rhs. llvm) )
71
85
}
72
86
}
73
87
74
88
public static func == ( lhs: Constant , rhs: Constant ) -> Constant {
75
89
precondition ( lhs. repr == rhs. repr, " Mixed-representation constant operations are disallowed " )
76
90
77
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . eq. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
91
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . eq. llvm, lhs. llvm, rhs. llvm) )
78
92
}
79
93
80
94
public static func < ( lhs: Constant , rhs: Constant ) -> Constant {
81
95
precondition ( lhs. repr == rhs. repr, " Mixed-representation constant operations are disallowed " )
82
96
83
97
switch lhs. repr {
84
98
case . signed:
85
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . slt. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
99
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . slt. llvm, lhs. llvm, rhs. llvm) )
86
100
case . unsigned:
87
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . ult. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
101
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . ult. llvm, lhs. llvm, rhs. llvm) )
88
102
case . floating:
89
- return Constant ( llvm: LLVMConstFCmp ( RealPredicate . olt. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
103
+ return Constant ( llvm: LLVMConstFCmp ( RealPredicate . olt. llvm, lhs. llvm, rhs. llvm) )
90
104
}
91
105
}
92
106
@@ -95,11 +109,11 @@ public struct Constant: IRValue {
95
109
96
110
switch lhs. repr {
97
111
case . signed:
98
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . sgt. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
112
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . sgt. llvm, lhs. llvm, rhs. llvm) )
99
113
case . unsigned:
100
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . ugt. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
114
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . ugt. llvm, lhs. llvm, rhs. llvm) )
101
115
case . floating:
102
- return Constant ( llvm: LLVMConstFCmp ( RealPredicate . ogt. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
116
+ return Constant ( llvm: LLVMConstFCmp ( RealPredicate . ogt. llvm, lhs. llvm, rhs. llvm) )
103
117
}
104
118
}
105
119
@@ -108,11 +122,11 @@ public struct Constant: IRValue {
108
122
109
123
switch lhs. repr {
110
124
case . signed:
111
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . sle. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
125
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . sle. llvm, lhs. llvm, rhs. llvm) )
112
126
case . unsigned:
113
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . ule. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
127
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . ule. llvm, lhs. llvm, rhs. llvm) )
114
128
case . floating:
115
- return Constant ( llvm: LLVMConstFCmp ( RealPredicate . ole. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
129
+ return Constant ( llvm: LLVMConstFCmp ( RealPredicate . ole. llvm, lhs. llvm, rhs. llvm) )
116
130
}
117
131
}
118
132
@@ -121,11 +135,11 @@ public struct Constant: IRValue {
121
135
122
136
switch lhs. repr {
123
137
case . signed:
124
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . sge. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
138
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . sge. llvm, lhs. llvm, rhs. llvm) )
125
139
case . unsigned:
126
- return Constant ( llvm: LLVMConstICmp ( IntPredicate . uge. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
140
+ return Constant ( llvm: LLVMConstICmp ( IntPredicate . uge. llvm, lhs. llvm, rhs. llvm) )
127
141
case . floating:
128
- return Constant ( llvm: LLVMConstFCmp ( RealPredicate . oge. llvm, lhs. llvm, rhs. llvm) , representation : lhs . repr )
142
+ return Constant ( llvm: LLVMConstFCmp ( RealPredicate . oge. llvm, lhs. llvm, rhs. llvm) )
129
143
}
130
144
}
131
145
}
0 commit comments