1 | //===--- Integral.h - Wrapper for numeric types for the VM ------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Defines the VM types and helpers operating on types. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_AST_INTERP_INTEGRAL_H |
14 | #define LLVM_CLANG_AST_INTERP_INTEGRAL_H |
15 | |
16 | #include "clang/AST/ComparisonCategories.h" |
17 | #include "clang/AST/APValue.h" |
18 | #include "llvm/ADT/APSInt.h" |
19 | #include "llvm/Support/MathExtras.h" |
20 | #include "llvm/Support/raw_ostream.h" |
21 | #include <cstddef> |
22 | #include <cstdint> |
23 | |
24 | #include "Primitives.h" |
25 | |
26 | namespace clang { |
27 | namespace interp { |
28 | |
29 | using APInt = llvm::APInt; |
30 | using APSInt = llvm::APSInt; |
31 | |
32 | template <bool Signed> class IntegralAP; |
33 | |
34 | // Helper structure to select the representation. |
35 | template <unsigned Bits, bool Signed> struct Repr; |
36 | template <> struct Repr<8, false> { using Type = uint8_t; }; |
37 | template <> struct Repr<16, false> { using Type = uint16_t; }; |
38 | template <> struct Repr<32, false> { using Type = uint32_t; }; |
39 | template <> struct Repr<64, false> { using Type = uint64_t; }; |
40 | template <> struct Repr<8, true> { using Type = int8_t; }; |
41 | template <> struct Repr<16, true> { using Type = int16_t; }; |
42 | template <> struct Repr<32, true> { using Type = int32_t; }; |
43 | template <> struct Repr<64, true> { using Type = int64_t; }; |
44 | |
45 | /// Wrapper around numeric types. |
46 | /// |
47 | /// These wrappers are required to shared an interface between APSint and |
48 | /// builtin primitive numeral types, while optimising for storage and |
49 | /// allowing methods operating on primitive type to compile to fast code. |
50 | template <unsigned Bits, bool Signed> class Integral final { |
51 | private: |
52 | template <unsigned OtherBits, bool OtherSigned> friend class Integral; |
53 | |
54 | // The primitive representing the integral. |
55 | using ReprT = typename Repr<Bits, Signed>::Type; |
56 | ReprT V; |
57 | |
58 | /// Primitive representing limits. |
59 | static const auto Min = std::numeric_limits<ReprT>::min(); |
60 | static const auto Max = std::numeric_limits<ReprT>::max(); |
61 | |
62 | /// Construct an integral from anything that is convertible to storage. |
63 | template <typename T> explicit Integral(T V) : V(V) {} |
64 | |
65 | public: |
66 | using AsUnsigned = Integral<Bits, false>; |
67 | |
68 | /// Zero-initializes an integral. |
69 | Integral() : V(0) {} |
70 | |
71 | /// Constructs an integral from another integral. |
72 | template <unsigned SrcBits, bool SrcSign> |
73 | explicit Integral(Integral<SrcBits, SrcSign> V) : V(V.V) {} |
74 | |
75 | /// Construct an integral from a value based on signedness. |
76 | explicit Integral(const APSInt &V) |
77 | : V(V.isSigned() ? V.getSExtValue() : V.getZExtValue()) {} |
78 | |
79 | bool operator<(Integral RHS) const { return V < RHS.V; } |
80 | bool operator>(Integral RHS) const { return V > RHS.V; } |
81 | bool operator<=(Integral RHS) const { return V <= RHS.V; } |
82 | bool operator>=(Integral RHS) const { return V >= RHS.V; } |
83 | bool operator==(Integral RHS) const { return V == RHS.V; } |
84 | bool operator!=(Integral RHS) const { return V != RHS.V; } |
85 | |
86 | bool operator>(unsigned RHS) const { |
87 | return V >= 0 && static_cast<unsigned>(V) > RHS; |
88 | } |
89 | |
90 | Integral operator-() const { return Integral(-V); } |
91 | Integral operator-(const Integral &Other) const { |
92 | return Integral(V - Other.V); |
93 | } |
94 | Integral operator~() const { return Integral(~V); } |
95 | |
96 | template <unsigned DstBits, bool DstSign> |
97 | explicit operator Integral<DstBits, DstSign>() const { |
98 | return Integral<DstBits, DstSign>(V); |
99 | } |
100 | |
101 | explicit operator unsigned() const { return V; } |
102 | explicit operator int64_t() const { return V; } |
103 | explicit operator uint64_t() const { return V; } |
104 | explicit operator int32_t() const { return V; } |
105 | |
106 | APSInt toAPSInt() const { |
107 | return APSInt(APInt(Bits, static_cast<uint64_t>(V), Signed), !Signed); |
108 | } |
109 | APSInt toAPSInt(unsigned NumBits) const { |
110 | if constexpr (Signed) |
111 | return APSInt(toAPSInt().sextOrTrunc(NumBits), !Signed); |
112 | else |
113 | return APSInt(toAPSInt().zextOrTrunc(NumBits), !Signed); |
114 | } |
115 | APValue toAPValue() const { return APValue(toAPSInt()); } |
116 | |
117 | Integral<Bits, false> toUnsigned() const { |
118 | return Integral<Bits, false>(*this); |
119 | } |
120 | |
121 | constexpr static unsigned bitWidth() { return Bits; } |
122 | |
123 | bool isZero() const { return !V; } |
124 | |
125 | bool isMin() const { return *this == min(NumBits: bitWidth()); } |
126 | |
127 | bool isMinusOne() const { return Signed && V == ReprT(-1); } |
128 | |
129 | constexpr static bool isSigned() { return Signed; } |
130 | |
131 | bool isNegative() const { return V < ReprT(0); } |
132 | bool isPositive() const { return !isNegative(); } |
133 | |
134 | ComparisonCategoryResult compare(const Integral &RHS) const { |
135 | return Compare(V, RHS.V); |
136 | } |
137 | |
138 | std::string toDiagnosticString(const ASTContext &Ctx) const { |
139 | std::string NameStr; |
140 | llvm::raw_string_ostream OS(NameStr); |
141 | OS << V; |
142 | return NameStr; |
143 | } |
144 | |
145 | unsigned countLeadingZeros() const { |
146 | if constexpr (!Signed) |
147 | return llvm::countl_zero<ReprT>(V); |
148 | llvm_unreachable("Don't call countLeadingZeros() on signed types." ); |
149 | } |
150 | |
151 | Integral truncate(unsigned TruncBits) const { |
152 | if (TruncBits >= Bits) |
153 | return *this; |
154 | const ReprT BitMask = (ReprT(1) << ReprT(TruncBits)) - 1; |
155 | const ReprT SignBit = ReprT(1) << (TruncBits - 1); |
156 | const ReprT ExtMask = ~BitMask; |
157 | return Integral((V & BitMask) | (Signed && (V & SignBit) ? ExtMask : 0)); |
158 | } |
159 | |
160 | void print(llvm::raw_ostream &OS) const { OS << V; } |
161 | |
162 | static Integral min(unsigned NumBits) { |
163 | return Integral(Min); |
164 | } |
165 | static Integral max(unsigned NumBits) { |
166 | return Integral(Max); |
167 | } |
168 | |
169 | template <typename ValT> static Integral from(ValT Value) { |
170 | if constexpr (std::is_integral<ValT>::value) |
171 | return Integral(Value); |
172 | else |
173 | return Integral::from(static_cast<Integral::ReprT>(Value)); |
174 | } |
175 | |
176 | template <unsigned SrcBits, bool SrcSign> |
177 | static std::enable_if_t<SrcBits != 0, Integral> |
178 | from(Integral<SrcBits, SrcSign> Value) { |
179 | return Integral(Value.V); |
180 | } |
181 | |
182 | static Integral zero() { return from(0); } |
183 | |
184 | template <typename T> static Integral from(T Value, unsigned NumBits) { |
185 | return Integral(Value); |
186 | } |
187 | |
188 | static bool inRange(int64_t Value, unsigned NumBits) { |
189 | return CheckRange<ReprT, Min, Max>(Value); |
190 | } |
191 | |
192 | static bool increment(Integral A, Integral *R) { |
193 | return add(A, B: Integral(ReprT(1)), OpBits: A.bitWidth(), R); |
194 | } |
195 | |
196 | static bool decrement(Integral A, Integral *R) { |
197 | return sub(A, B: Integral(ReprT(1)), OpBits: A.bitWidth(), R); |
198 | } |
199 | |
200 | static bool add(Integral A, Integral B, unsigned OpBits, Integral *R) { |
201 | return CheckAddUB(A.V, B.V, R->V); |
202 | } |
203 | |
204 | static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R) { |
205 | return CheckSubUB(A.V, B.V, R->V); |
206 | } |
207 | |
208 | static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R) { |
209 | return CheckMulUB(A.V, B.V, R->V); |
210 | } |
211 | |
212 | static bool rem(Integral A, Integral B, unsigned OpBits, Integral *R) { |
213 | *R = Integral(A.V % B.V); |
214 | return false; |
215 | } |
216 | |
217 | static bool div(Integral A, Integral B, unsigned OpBits, Integral *R) { |
218 | *R = Integral(A.V / B.V); |
219 | return false; |
220 | } |
221 | |
222 | static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R) { |
223 | *R = Integral(A.V & B.V); |
224 | return false; |
225 | } |
226 | |
227 | static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R) { |
228 | *R = Integral(A.V | B.V); |
229 | return false; |
230 | } |
231 | |
232 | static bool bitXor(Integral A, Integral B, unsigned OpBits, Integral *R) { |
233 | *R = Integral(A.V ^ B.V); |
234 | return false; |
235 | } |
236 | |
237 | static bool neg(Integral A, Integral *R) { |
238 | if (Signed && A.isMin()) |
239 | return true; |
240 | |
241 | *R = -A; |
242 | return false; |
243 | } |
244 | |
245 | static bool comp(Integral A, Integral *R) { |
246 | *R = Integral(~A.V); |
247 | return false; |
248 | } |
249 | |
250 | template <unsigned RHSBits, bool RHSSign> |
251 | static void shiftLeft(const Integral A, const Integral<RHSBits, RHSSign> B, |
252 | unsigned OpBits, Integral *R) { |
253 | *R = Integral::from(A.V << B.V, OpBits); |
254 | } |
255 | |
256 | template <unsigned RHSBits, bool RHSSign> |
257 | static void shiftRight(const Integral A, const Integral<RHSBits, RHSSign> B, |
258 | unsigned OpBits, Integral *R) { |
259 | *R = Integral::from(A.V >> B.V, OpBits); |
260 | } |
261 | |
262 | private: |
263 | template <typename T> static bool CheckAddUB(T A, T B, T &R) { |
264 | if constexpr (std::is_signed_v<T>) { |
265 | return llvm::AddOverflow<T>(A, B, R); |
266 | } else { |
267 | R = A + B; |
268 | return false; |
269 | } |
270 | } |
271 | |
272 | template <typename T> static bool CheckSubUB(T A, T B, T &R) { |
273 | if constexpr (std::is_signed_v<T>) { |
274 | return llvm::SubOverflow<T>(A, B, R); |
275 | } else { |
276 | R = A - B; |
277 | return false; |
278 | } |
279 | } |
280 | |
281 | template <typename T> static bool CheckMulUB(T A, T B, T &R) { |
282 | if constexpr (std::is_signed_v<T>) { |
283 | return llvm::MulOverflow<T>(A, B, R); |
284 | } else { |
285 | R = A * B; |
286 | return false; |
287 | } |
288 | } |
289 | template <typename T, T Min, T Max> static bool CheckRange(int64_t V) { |
290 | if constexpr (std::is_signed_v<T>) { |
291 | return Min <= V && V <= Max; |
292 | } else { |
293 | return V >= 0 && static_cast<uint64_t>(V) <= Max; |
294 | } |
295 | } |
296 | }; |
297 | |
298 | template <unsigned Bits, bool Signed> |
299 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Integral<Bits, Signed> I) { |
300 | I.print(OS); |
301 | return OS; |
302 | } |
303 | |
304 | } // namespace interp |
305 | } // namespace clang |
306 | |
307 | #endif |
308 | |