|
| 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_AP_H |
| 14 | +#define LLVM_CLANG_AST_INTERP_INTEGRAL_AP_H |
| 15 | + |
| 16 | +#include "clang/AST/APValue.h" |
| 17 | +#include "clang/AST/ComparisonCategories.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 | +template <unsigned Bits, bool Signed> class Integral; |
| 32 | +class Boolean; |
| 33 | + |
| 34 | +template <bool Signed> class IntegralAP final { |
| 35 | +public: |
| 36 | + APSInt V; |
| 37 | + |
| 38 | +public: |
| 39 | + using AsUnsigned = IntegralAP<false>; |
| 40 | + |
| 41 | + template <typename T> |
| 42 | + IntegralAP(T Value) : V(APInt(sizeof(T) * 8, Value, std::is_signed_v<T>)) {} |
| 43 | + |
| 44 | + IntegralAP(APInt V) : V(V) {} |
| 45 | + IntegralAP(APSInt V) : V(V) {} |
| 46 | + /// Arbitrary value for initialized variables. |
| 47 | + IntegralAP() : V(APSInt::getMaxValue(1024, Signed)) {} |
| 48 | + |
| 49 | + IntegralAP operator-() const { return IntegralAP(-V); } |
| 50 | + bool operator>(IntegralAP RHS) const { return V > RHS.V; } |
| 51 | + bool operator>=(IntegralAP RHS) const { return V >= RHS.V; } |
| 52 | + bool operator<(IntegralAP RHS) const { return V < RHS.V; } |
| 53 | + bool operator<=(IntegralAP RHS) const { return V <= RHS.V; } |
| 54 | + |
| 55 | + explicit operator bool() const { return !V.isZero(); } |
| 56 | + explicit operator int8_t() const { return V.getSExtValue(); } |
| 57 | + explicit operator uint8_t() const { return V.getZExtValue(); } |
| 58 | + explicit operator int16_t() const { return V.getSExtValue(); } |
| 59 | + explicit operator uint16_t() const { return V.getZExtValue(); } |
| 60 | + explicit operator int32_t() const { return V.getSExtValue(); } |
| 61 | + explicit operator uint32_t() const { return V.getZExtValue(); } |
| 62 | + explicit operator int64_t() const { return V.getSExtValue(); } |
| 63 | + explicit operator uint64_t() const { return V.getZExtValue(); } |
| 64 | + |
| 65 | + template <typename T> static IntegralAP from(T Value, unsigned NumBits = 0) { |
| 66 | + assert(NumBits > 0); |
| 67 | + APSInt Copy = APSInt(APInt(NumBits, Value, Signed), !Signed); |
| 68 | + |
| 69 | + return IntegralAP<Signed>(Copy); |
| 70 | + } |
| 71 | + |
| 72 | + template <bool InputSigned> |
| 73 | + static IntegralAP from(IntegralAP<InputSigned> V, unsigned NumBits = 0) { |
| 74 | + if constexpr (Signed == InputSigned) |
| 75 | + return V; |
| 76 | + |
| 77 | + APSInt Copy = V.V; |
| 78 | + Copy.setIsSigned(Signed); |
| 79 | + |
| 80 | + return IntegralAP<Signed>(Copy); |
| 81 | + } |
| 82 | + |
| 83 | + template <unsigned Bits, bool InputSigned> |
| 84 | + static IntegralAP from(Integral<Bits, InputSigned> I) { |
| 85 | + // FIXME: Take bits parameter. |
| 86 | + APSInt Copy = |
| 87 | + APSInt(APInt(128, static_cast<int64_t>(I), InputSigned), !Signed); |
| 88 | + Copy.setIsSigned(Signed); |
| 89 | + |
| 90 | + assert(Copy.isSigned() == Signed); |
| 91 | + return IntegralAP<Signed>(Copy); |
| 92 | + } |
| 93 | + static IntegralAP from(const Boolean &B) { |
| 94 | + assert(false); |
| 95 | + return IntegralAP::zero(); |
| 96 | + } |
| 97 | + |
| 98 | + static IntegralAP zero() { |
| 99 | + assert(false); |
| 100 | + return IntegralAP(0); |
| 101 | + } |
| 102 | + |
| 103 | + // FIXME: This can't be static if the bitwidth depends on V. |
| 104 | + static constexpr unsigned bitWidth() { return 128; } |
| 105 | + |
| 106 | + APSInt toAPSInt(unsigned Bits = 0) const { return V; } |
| 107 | + APValue toAPValue() const { return APValue(V); } |
| 108 | + |
| 109 | + bool isZero() const { return V.isZero(); } |
| 110 | + bool isPositive() const { return V.isNonNegative(); } |
| 111 | + bool isNegative() const { return !V.isNonNegative(); } |
| 112 | + bool isMin() const { return V.isMinValue(); } |
| 113 | + bool isMax() const { return V.isMaxValue(); } |
| 114 | + static bool isSigned() { return Signed; } |
| 115 | + bool isMinusOne() const { return Signed && V == -1; } |
| 116 | + |
| 117 | + unsigned countLeadingZeros() const { return V.countl_zero(); } |
| 118 | + |
| 119 | + void print(llvm::raw_ostream &OS) const { OS << V; } |
| 120 | + |
| 121 | + IntegralAP truncate(unsigned bitWidth) const { |
| 122 | + assert(false); |
| 123 | + return V; |
| 124 | + } |
| 125 | + |
| 126 | + IntegralAP<false> toUnsigned() const { |
| 127 | + APSInt Copy = V; |
| 128 | + Copy.setIsSigned(false); |
| 129 | + return IntegralAP<false>(Copy); |
| 130 | + } |
| 131 | + |
| 132 | + ComparisonCategoryResult compare(const IntegralAP &RHS) const { |
| 133 | + return Compare(V, RHS.V); |
| 134 | + } |
| 135 | + |
| 136 | + static bool increment(IntegralAP A, IntegralAP *R) { |
| 137 | + assert(false); |
| 138 | + *R = IntegralAP(A.V + 1); |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + static bool decrement(IntegralAP A, IntegralAP *R) { |
| 143 | + assert(false); |
| 144 | + *R = IntegralAP(A.V - 1); |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + static bool add(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { |
| 149 | + return CheckAddUB(A, B, OpBits, R); |
| 150 | + } |
| 151 | + |
| 152 | + static bool sub(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { |
| 153 | + /// FIXME: Gotta check if the result fits into OpBits bits. |
| 154 | + return CheckSubUB(A, B, R); |
| 155 | + } |
| 156 | + |
| 157 | + static bool mul(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { |
| 158 | + assert(false); |
| 159 | + // return CheckMulUB(A.V, B.V, R->V); |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + static bool rem(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { |
| 164 | + assert(false); |
| 165 | + *R = IntegralAP(A.V % B.V); |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + static bool div(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { |
| 170 | + assert(false); |
| 171 | + *R = IntegralAP(A.V / B.V); |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + static bool bitAnd(IntegralAP A, IntegralAP B, unsigned OpBits, |
| 176 | + IntegralAP *R) { |
| 177 | + assert(false); |
| 178 | + *R = IntegralAP(A.V & B.V); |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + static bool bitOr(IntegralAP A, IntegralAP B, unsigned OpBits, |
| 183 | + IntegralAP *R) { |
| 184 | + assert(false); |
| 185 | + *R = IntegralAP(A.V | B.V); |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + static bool bitXor(IntegralAP A, IntegralAP B, unsigned OpBits, |
| 190 | + IntegralAP *R) { |
| 191 | + assert(false); |
| 192 | + *R = IntegralAP(A.V ^ B.V); |
| 193 | + return false; |
| 194 | + } |
| 195 | + |
| 196 | + static bool neg(const IntegralAP &A, IntegralAP *R) { |
| 197 | + APSInt AI = A.V; |
| 198 | + |
| 199 | + AI.setIsSigned(Signed); |
| 200 | + *R = IntegralAP(AI); |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + static bool comp(IntegralAP A, IntegralAP *R) { |
| 205 | + assert(false); |
| 206 | + *R = IntegralAP(~A.V); |
| 207 | + return false; |
| 208 | + } |
| 209 | + |
| 210 | + static void shiftLeft(const IntegralAP A, const IntegralAP B, unsigned OpBits, |
| 211 | + IntegralAP *R) { |
| 212 | + *R = IntegralAP(A.V << B.V.getZExtValue()); |
| 213 | + } |
| 214 | + |
| 215 | + static void shiftRight(const IntegralAP A, const IntegralAP B, |
| 216 | + unsigned OpBits, IntegralAP *R) { |
| 217 | + *R = IntegralAP(A.V >> B.V.getZExtValue()); |
| 218 | + } |
| 219 | + |
| 220 | +private: |
| 221 | + static bool CheckAddUB(const IntegralAP &A, const IntegralAP &B, |
| 222 | + unsigned BitWidth, IntegralAP *R) { |
| 223 | + if (!A.isSigned()) { |
| 224 | + R->V = A.V + B.V; |
| 225 | + return false; |
| 226 | + } |
| 227 | + |
| 228 | + const APSInt &LHS = A.V; |
| 229 | + const APSInt &RHS = B.V; |
| 230 | + |
| 231 | + APSInt Value(LHS.extend(BitWidth) + RHS.extend(BitWidth), false); |
| 232 | + APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 233 | + if (Result.extend(BitWidth) != Value) |
| 234 | + return true; |
| 235 | + |
| 236 | + R->V = Result; |
| 237 | + return false; |
| 238 | + } |
| 239 | + static bool CheckSubUB(const IntegralAP &A, const IntegralAP &B, |
| 240 | + IntegralAP *R) { |
| 241 | + R->V = A.V - B.V; |
| 242 | + return false; // Success! |
| 243 | + } |
| 244 | +}; |
| 245 | + |
| 246 | +template <bool Signed> |
| 247 | +inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 248 | + IntegralAP<Signed> I) { |
| 249 | + I.print(OS); |
| 250 | + return OS; |
| 251 | +} |
| 252 | + |
| 253 | +} // namespace interp |
| 254 | +} // namespace clang |
| 255 | + |
| 256 | +#endif |
0 commit comments