1 //===--- Integral.h - Wrapper for numeric types for the VM ------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Defines the VM types and helpers operating on types.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_INTERP_INTEGRAL_H
14 #define LLVM_CLANG_AST_INTERP_INTEGRAL_H
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"
24 #include "Primitives.h"
29 using APInt
= llvm::APInt
;
30 using APSInt
= llvm::APSInt
;
32 template <bool Signed
> class IntegralAP
;
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; };
45 /// Wrapper around numeric types.
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
{
52 template <unsigned OtherBits
, bool OtherSigned
> friend class Integral
;
54 // The primitive representing the integral.
55 using ReprT
= typename Repr
<Bits
, Signed
>::Type
;
58 /// Primitive representing limits.
59 static const auto Min
= std::numeric_limits
<ReprT
>::min();
60 static const auto Max
= std::numeric_limits
<ReprT
>::max();
62 /// Construct an integral from anything that is convertible to storage.
63 template <typename T
> explicit Integral(T V
) : V(V
) {}
66 using AsUnsigned
= Integral
<Bits
, false>;
68 /// Zero-initializes an integral.
71 /// Constructs an integral from another integral.
72 template <unsigned SrcBits
, bool SrcSign
>
73 explicit Integral(Integral
<SrcBits
, SrcSign
> V
) : V(V
.V
) {}
75 /// Construct an integral from a value based on signedness.
76 explicit Integral(const APSInt
&V
)
77 : V(V
.isSigned() ? V
.getSExtValue() : V
.getZExtValue()) {}
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
; }
86 bool operator>(unsigned RHS
) const {
87 return V
>= 0 && static_cast<unsigned>(V
) > RHS
;
90 Integral
operator-() const { return Integral(-V
); }
91 Integral
operator-(const Integral
&Other
) const {
92 return Integral(V
- Other
.V
);
94 Integral
operator~() const { return Integral(~V
); }
96 template <unsigned DstBits
, bool DstSign
>
97 explicit operator Integral
<DstBits
, DstSign
>() const {
98 return Integral
<DstBits
, DstSign
>(V
);
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
; }
106 APSInt
toAPSInt() const {
107 return APSInt(APInt(Bits
, static_cast<uint64_t>(V
), Signed
), !Signed
);
109 APSInt
toAPSInt(unsigned NumBits
) const {
110 if constexpr (Signed
)
111 return APSInt(toAPSInt().sextOrTrunc(NumBits
), !Signed
);
113 return APSInt(toAPSInt().zextOrTrunc(NumBits
), !Signed
);
115 APValue
toAPValue() const { return APValue(toAPSInt()); }
117 Integral
<Bits
, false> toUnsigned() const {
118 return Integral
<Bits
, false>(*this);
121 constexpr static unsigned bitWidth() { return Bits
; }
123 bool isZero() const { return !V
; }
125 bool isMin() const { return *this == min(bitWidth()); }
127 bool isMinusOne() const { return Signed
&& V
== ReprT(-1); }
129 constexpr static bool isSigned() { return Signed
; }
131 bool isNegative() const { return V
< ReprT(0); }
132 bool isPositive() const { return !isNegative(); }
134 ComparisonCategoryResult
compare(const Integral
&RHS
) const {
135 return Compare(V
, RHS
.V
);
138 std::string
toDiagnosticString(const ASTContext
&Ctx
) const {
140 llvm::raw_string_ostream
OS(NameStr
);
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.");
151 Integral
truncate(unsigned TruncBits
) const {
152 if (TruncBits
>= Bits
)
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));
160 void print(llvm::raw_ostream
&OS
) const { OS
<< V
; }
162 static Integral
min(unsigned NumBits
) {
163 return Integral(Min
);
165 static Integral
max(unsigned NumBits
) {
166 return Integral(Max
);
169 template <typename ValT
> static Integral
from(ValT Value
) {
170 if constexpr (std::is_integral
<ValT
>::value
)
171 return Integral(Value
);
173 return Integral::from(static_cast<Integral::ReprT
>(Value
));
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
);
182 static Integral
zero() { return from(0); }
184 template <typename T
> static Integral
from(T Value
, unsigned NumBits
) {
185 return Integral(Value
);
188 static bool inRange(int64_t Value
, unsigned NumBits
) {
189 return CheckRange
<ReprT
, Min
, Max
>(Value
);
192 static bool increment(Integral A
, Integral
*R
) {
193 return add(A
, Integral(ReprT(1)), A
.bitWidth(), R
);
196 static bool decrement(Integral A
, Integral
*R
) {
197 return sub(A
, Integral(ReprT(1)), A
.bitWidth(), R
);
200 static bool add(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
201 return CheckAddUB(A
.V
, B
.V
, R
->V
);
204 static bool sub(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
205 return CheckSubUB(A
.V
, B
.V
, R
->V
);
208 static bool mul(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
209 return CheckMulUB(A
.V
, B
.V
, R
->V
);
212 static bool rem(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
213 *R
= Integral(A
.V
% B
.V
);
217 static bool div(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
218 *R
= Integral(A
.V
/ B
.V
);
222 static bool bitAnd(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
223 *R
= Integral(A
.V
& B
.V
);
227 static bool bitOr(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
228 *R
= Integral(A
.V
| B
.V
);
232 static bool bitXor(Integral A
, Integral B
, unsigned OpBits
, Integral
*R
) {
233 *R
= Integral(A
.V
^ B
.V
);
237 static bool neg(Integral A
, Integral
*R
) {
238 if (Signed
&& A
.isMin())
245 static bool comp(Integral A
, Integral
*R
) {
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
);
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
);
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
);
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
);
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
);
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
;
293 return V
>= 0 && static_cast<uint64_t>(V
) <= Max
;
298 template <unsigned Bits
, bool Signed
>
299 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, Integral
<Bits
, Signed
> I
) {
304 } // namespace interp