00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef LLVM_APSINT_H
00016 #define LLVM_APSINT_H
00017
00018 #include "llvm/ADT/APInt.h"
00019
00020 namespace llvm {
00021
00022 class APSInt : public APInt {
00023 bool IsUnsigned;
00024 public:
00026 explicit APSInt() {}
00027
00030 explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
00031 : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
00032
00033 explicit APSInt(const APInt &I, bool isUnsigned = true)
00034 : APInt(I), IsUnsigned(isUnsigned) {}
00035
00036 APSInt &operator=(const APSInt &RHS) {
00037 APInt::operator=(RHS);
00038 IsUnsigned = RHS.IsUnsigned;
00039 return *this;
00040 }
00041
00042 APSInt &operator=(const APInt &RHS) {
00043
00044 APInt::operator=(RHS);
00045 return *this;
00046 }
00047
00048 APSInt &operator=(uint64_t RHS) {
00049
00050 APInt::operator=(RHS);
00051 return *this;
00052 }
00053
00054
00055 bool isSigned() const { return !IsUnsigned; }
00056 bool isUnsigned() const { return IsUnsigned; }
00057 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
00058 void setIsSigned(bool Val) { IsUnsigned = !Val; }
00059
00061 void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
00062 APInt::toString(Str, Radix, isSigned());
00063 }
00066 std::string toString(unsigned Radix) const {
00067 return APInt::toString(Radix, isSigned());
00068 }
00069 using APInt::toString;
00070
00071 APSInt trunc(uint32_t width) const {
00072 return APSInt(APInt::trunc(width), IsUnsigned);
00073 }
00074
00075 APSInt extend(uint32_t width) const {
00076 if (IsUnsigned)
00077 return APSInt(zext(width), IsUnsigned);
00078 else
00079 return APSInt(sext(width), IsUnsigned);
00080 }
00081
00082 APSInt extOrTrunc(uint32_t width) const {
00083 if (IsUnsigned)
00084 return APSInt(zextOrTrunc(width), IsUnsigned);
00085 else
00086 return APSInt(sextOrTrunc(width), IsUnsigned);
00087 }
00088
00089 const APSInt &operator%=(const APSInt &RHS) {
00090 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00091 if (IsUnsigned)
00092 *this = urem(RHS);
00093 else
00094 *this = srem(RHS);
00095 return *this;
00096 }
00097 const APSInt &operator/=(const APSInt &RHS) {
00098 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00099 if (IsUnsigned)
00100 *this = udiv(RHS);
00101 else
00102 *this = sdiv(RHS);
00103 return *this;
00104 }
00105 APSInt operator%(const APSInt &RHS) const {
00106 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00107 return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
00108 }
00109 APSInt operator/(const APSInt &RHS) const {
00110 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00111 return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
00112 }
00113
00114 APSInt operator>>(unsigned Amt) const {
00115 return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
00116 }
00117 APSInt& operator>>=(unsigned Amt) {
00118 *this = *this >> Amt;
00119 return *this;
00120 }
00121
00122 inline bool operator<(const APSInt& RHS) const {
00123 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00124 return IsUnsigned ? ult(RHS) : slt(RHS);
00125 }
00126 inline bool operator>(const APSInt& RHS) const {
00127 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00128 return IsUnsigned ? ugt(RHS) : sgt(RHS);
00129 }
00130 inline bool operator<=(const APSInt& RHS) const {
00131 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00132 return IsUnsigned ? ule(RHS) : sle(RHS);
00133 }
00134 inline bool operator>=(const APSInt& RHS) const {
00135 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00136 return IsUnsigned ? uge(RHS) : sge(RHS);
00137 }
00138
00139
00140
00141
00142 APSInt operator<<(unsigned Bits) const {
00143 return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
00144 }
00145 APSInt& operator<<=(unsigned Amt) {
00146 *this = *this << Amt;
00147 return *this;
00148 }
00149
00150 APSInt& operator++() {
00151 static_cast<APInt&>(*this)++;
00152 return *this;
00153 }
00154 APSInt& operator--() {
00155 static_cast<APInt&>(*this)--;
00156 return *this;
00157 }
00158 APSInt operator++(int) {
00159 return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
00160 }
00161 APSInt operator--(int) {
00162 return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
00163 }
00164 APSInt operator-() const {
00165 return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
00166 }
00167 APSInt& operator+=(const APSInt& RHS) {
00168 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00169 static_cast<APInt&>(*this) += RHS;
00170 return *this;
00171 }
00172 APSInt& operator-=(const APSInt& RHS) {
00173 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00174 static_cast<APInt&>(*this) -= RHS;
00175 return *this;
00176 }
00177 APSInt& operator*=(const APSInt& RHS) {
00178 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00179 static_cast<APInt&>(*this) *= RHS;
00180 return *this;
00181 }
00182 APSInt& operator&=(const APSInt& RHS) {
00183 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00184 static_cast<APInt&>(*this) &= RHS;
00185 return *this;
00186 }
00187 APSInt& operator|=(const APSInt& RHS) {
00188 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00189 static_cast<APInt&>(*this) |= RHS;
00190 return *this;
00191 }
00192 APSInt& operator^=(const APSInt& RHS) {
00193 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00194 static_cast<APInt&>(*this) ^= RHS;
00195 return *this;
00196 }
00197
00198 APSInt operator&(const APSInt& RHS) const {
00199 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00200 return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
00201 }
00202 APSInt And(const APSInt& RHS) const {
00203 return this->operator&(RHS);
00204 }
00205
00206 APSInt operator|(const APSInt& RHS) const {
00207 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00208 return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
00209 }
00210 APSInt Or(const APSInt& RHS) const {
00211 return this->operator|(RHS);
00212 }
00213
00214
00215 APSInt operator^(const APSInt& RHS) const {
00216 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00217 return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
00218 }
00219 APSInt Xor(const APSInt& RHS) const {
00220 return this->operator^(RHS);
00221 }
00222
00223 APSInt operator*(const APSInt& RHS) const {
00224 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00225 return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
00226 }
00227 APSInt operator+(const APSInt& RHS) const {
00228 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00229 return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
00230 }
00231 APSInt operator-(const APSInt& RHS) const {
00232 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
00233 return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
00234 }
00235 APSInt operator~() const {
00236 return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
00237 }
00238
00241 static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
00242 return APSInt(Unsigned ? APInt::getMaxValue(numBits)
00243 : APInt::getSignedMaxValue(numBits), Unsigned);
00244 }
00245
00248 static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
00249 return APSInt(Unsigned ? APInt::getMinValue(numBits)
00250 : APInt::getSignedMinValue(numBits), Unsigned);
00251 }
00252
00255 void Profile(FoldingSetNodeID& ID) const;
00256 };
00257
00258 inline std::ostream &operator<<(std::ostream &OS, const APSInt &I) {
00259 I.print(OS, I.isSigned());
00260 return OS;
00261 }
00262
00263
00264 }
00265
00266 #endif