Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/Integral.h
35292 views
//===--- Integral.h - Wrapper for numeric types for the VM ------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Defines the VM types and helpers operating on types.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_CLANG_AST_INTERP_INTEGRAL_H13#define LLVM_CLANG_AST_INTERP_INTEGRAL_H1415#include "clang/AST/ComparisonCategories.h"16#include "clang/AST/APValue.h"17#include "llvm/ADT/APSInt.h"18#include "llvm/Support/MathExtras.h"19#include "llvm/Support/raw_ostream.h"20#include <cstddef>21#include <cstdint>2223#include "Primitives.h"2425namespace clang {26namespace interp {2728using APInt = llvm::APInt;29using APSInt = llvm::APSInt;3031template <bool Signed> class IntegralAP;3233// Helper structure to select the representation.34template <unsigned Bits, bool Signed> struct Repr;35template <> struct Repr<8, false> { using Type = uint8_t; };36template <> struct Repr<16, false> { using Type = uint16_t; };37template <> struct Repr<32, false> { using Type = uint32_t; };38template <> struct Repr<64, false> { using Type = uint64_t; };39template <> struct Repr<8, true> { using Type = int8_t; };40template <> struct Repr<16, true> { using Type = int16_t; };41template <> struct Repr<32, true> { using Type = int32_t; };42template <> struct Repr<64, true> { using Type = int64_t; };4344/// Wrapper around numeric types.45///46/// These wrappers are required to shared an interface between APSint and47/// builtin primitive numeral types, while optimising for storage and48/// allowing methods operating on primitive type to compile to fast code.49template <unsigned Bits, bool Signed> class Integral final {50private:51template <unsigned OtherBits, bool OtherSigned> friend class Integral;5253// The primitive representing the integral.54using ReprT = typename Repr<Bits, Signed>::Type;55ReprT V;5657/// Primitive representing limits.58static const auto Min = std::numeric_limits<ReprT>::min();59static const auto Max = std::numeric_limits<ReprT>::max();6061/// Construct an integral from anything that is convertible to storage.62template <typename T> explicit Integral(T V) : V(V) {}6364public:65using AsUnsigned = Integral<Bits, false>;6667/// Zero-initializes an integral.68Integral() : V(0) {}6970/// Constructs an integral from another integral.71template <unsigned SrcBits, bool SrcSign>72explicit Integral(Integral<SrcBits, SrcSign> V) : V(V.V) {}7374/// Construct an integral from a value based on signedness.75explicit Integral(const APSInt &V)76: V(V.isSigned() ? V.getSExtValue() : V.getZExtValue()) {}7778bool operator<(Integral RHS) const { return V < RHS.V; }79bool operator>(Integral RHS) const { return V > RHS.V; }80bool operator<=(Integral RHS) const { return V <= RHS.V; }81bool operator>=(Integral RHS) const { return V >= RHS.V; }82bool operator==(Integral RHS) const { return V == RHS.V; }83bool operator!=(Integral RHS) const { return V != RHS.V; }8485bool operator>(unsigned RHS) const {86return V >= 0 && static_cast<unsigned>(V) > RHS;87}8889Integral operator-() const { return Integral(-V); }90Integral operator-(const Integral &Other) const {91return Integral(V - Other.V);92}93Integral operator~() const { return Integral(~V); }9495template <unsigned DstBits, bool DstSign>96explicit operator Integral<DstBits, DstSign>() const {97return Integral<DstBits, DstSign>(V);98}99100template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>101explicit operator Ty() const {102return V;103}104105APSInt toAPSInt() const {106return APSInt(APInt(Bits, static_cast<uint64_t>(V), Signed), !Signed);107}108APSInt toAPSInt(unsigned NumBits) const {109if constexpr (Signed)110return APSInt(toAPSInt().sextOrTrunc(NumBits), !Signed);111else112return APSInt(toAPSInt().zextOrTrunc(NumBits), !Signed);113}114APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }115116Integral<Bits, false> toUnsigned() const {117return Integral<Bits, false>(*this);118}119120constexpr static unsigned bitWidth() { return Bits; }121122bool isZero() const { return !V; }123124bool isMin() const { return *this == min(bitWidth()); }125126bool isMinusOne() const { return Signed && V == ReprT(-1); }127128constexpr static bool isSigned() { return Signed; }129130bool isNegative() const { return V < ReprT(0); }131bool isPositive() const { return !isNegative(); }132133ComparisonCategoryResult compare(const Integral &RHS) const {134return Compare(V, RHS.V);135}136137std::string toDiagnosticString(const ASTContext &Ctx) const {138std::string NameStr;139llvm::raw_string_ostream OS(NameStr);140OS << V;141return NameStr;142}143144unsigned countLeadingZeros() const {145if constexpr (!Signed)146return llvm::countl_zero<ReprT>(V);147llvm_unreachable("Don't call countLeadingZeros() on signed types.");148}149150Integral truncate(unsigned TruncBits) const {151if (TruncBits >= Bits)152return *this;153const ReprT BitMask = (ReprT(1) << ReprT(TruncBits)) - 1;154const ReprT SignBit = ReprT(1) << (TruncBits - 1);155const ReprT ExtMask = ~BitMask;156return Integral((V & BitMask) | (Signed && (V & SignBit) ? ExtMask : 0));157}158159void print(llvm::raw_ostream &OS) const { OS << V; }160161static Integral min(unsigned NumBits) {162return Integral(Min);163}164static Integral max(unsigned NumBits) {165return Integral(Max);166}167168template <typename ValT> static Integral from(ValT Value) {169if constexpr (std::is_integral<ValT>::value)170return Integral(Value);171else172return Integral::from(static_cast<Integral::ReprT>(Value));173}174175template <unsigned SrcBits, bool SrcSign>176static std::enable_if_t<SrcBits != 0, Integral>177from(Integral<SrcBits, SrcSign> Value) {178return Integral(Value.V);179}180181static Integral zero() { return from(0); }182183template <typename T> static Integral from(T Value, unsigned NumBits) {184return Integral(Value);185}186187static bool inRange(int64_t Value, unsigned NumBits) {188return CheckRange<ReprT, Min, Max>(Value);189}190191static bool increment(Integral A, Integral *R) {192return add(A, Integral(ReprT(1)), A.bitWidth(), R);193}194195static bool decrement(Integral A, Integral *R) {196return sub(A, Integral(ReprT(1)), A.bitWidth(), R);197}198199static bool add(Integral A, Integral B, unsigned OpBits, Integral *R) {200return CheckAddUB(A.V, B.V, R->V);201}202203static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R) {204return CheckSubUB(A.V, B.V, R->V);205}206207static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R) {208return CheckMulUB(A.V, B.V, R->V);209}210211static bool rem(Integral A, Integral B, unsigned OpBits, Integral *R) {212*R = Integral(A.V % B.V);213return false;214}215216static bool div(Integral A, Integral B, unsigned OpBits, Integral *R) {217*R = Integral(A.V / B.V);218return false;219}220221static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R) {222*R = Integral(A.V & B.V);223return false;224}225226static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R) {227*R = Integral(A.V | B.V);228return false;229}230231static bool bitXor(Integral A, Integral B, unsigned OpBits, Integral *R) {232*R = Integral(A.V ^ B.V);233return false;234}235236static bool neg(Integral A, Integral *R) {237if (Signed && A.isMin())238return true;239240*R = -A;241return false;242}243244static bool comp(Integral A, Integral *R) {245*R = Integral(~A.V);246return false;247}248249template <unsigned RHSBits, bool RHSSign>250static void shiftLeft(const Integral A, const Integral<RHSBits, RHSSign> B,251unsigned OpBits, Integral *R) {252*R = Integral::from(A.V << B.V, OpBits);253}254255template <unsigned RHSBits, bool RHSSign>256static void shiftRight(const Integral A, const Integral<RHSBits, RHSSign> B,257unsigned OpBits, Integral *R) {258*R = Integral::from(A.V >> B.V, OpBits);259}260261private:262template <typename T> static bool CheckAddUB(T A, T B, T &R) {263if constexpr (std::is_signed_v<T>) {264return llvm::AddOverflow<T>(A, B, R);265} else {266R = A + B;267return false;268}269}270271template <typename T> static bool CheckSubUB(T A, T B, T &R) {272if constexpr (std::is_signed_v<T>) {273return llvm::SubOverflow<T>(A, B, R);274} else {275R = A - B;276return false;277}278}279280template <typename T> static bool CheckMulUB(T A, T B, T &R) {281if constexpr (std::is_signed_v<T>) {282return llvm::MulOverflow<T>(A, B, R);283} else {284R = A * B;285return false;286}287}288template <typename T, T Min, T Max> static bool CheckRange(int64_t V) {289if constexpr (std::is_signed_v<T>) {290return Min <= V && V <= Max;291} else {292return V >= 0 && static_cast<uint64_t>(V) <= Max;293}294}295};296297template <unsigned Bits, bool Signed>298llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Integral<Bits, Signed> I) {299I.print(OS);300return OS;301}302303} // namespace interp304} // namespace clang305306#endif307308309