Unverified Commit 59d2dc23 authored by Timm Baeder's avatar Timm Baeder Committed by GitHub
Browse files

[clang][Interp] IntegralAP zero-initializers (#68081)

parent cffb9df1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -1665,9 +1665,9 @@ bool ByteCodeExprGen<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
  case PT_Uint64:
    return this->emitZeroUint64(E);
  case PT_IntAP:
    return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
  case PT_IntAPS:
    assert(false);
    return false;
    return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
  case PT_Ptr:
    return this->emitNullPtr(E);
  case PT_FnPtr:
+6 −5
Original line number Diff line number Diff line
@@ -78,7 +78,8 @@ public:

  template <typename T> static IntegralAP from(T Value, unsigned NumBits = 0) {
    assert(NumBits > 0);
    APSInt Copy = APSInt(APInt(NumBits, static_cast<int64_t>(Value), Signed), !Signed);
    APSInt Copy =
        APSInt(APInt(NumBits, static_cast<uint64_t>(Value), Signed), !Signed);

    return IntegralAP<Signed>(Copy);
  }
@@ -97,16 +98,16 @@ public:
  template <unsigned Bits, bool InputSigned>
  static IntegralAP from(Integral<Bits, InputSigned> I, unsigned BitWidth) {
    APSInt Copy =
        APSInt(APInt(BitWidth, static_cast<int64_t>(I), InputSigned), !Signed);
        APSInt(APInt(BitWidth, static_cast<uint64_t>(I), InputSigned), !Signed);
    Copy.setIsSigned(Signed);

    assert(Copy.isSigned() == Signed);
    return IntegralAP<Signed>(Copy);
  }

  static IntegralAP zero() {
    assert(false);
    return IntegralAP(0);
  static IntegralAP zero(int32_t BitWidth) {
    APSInt V = APSInt(APInt(BitWidth, 0LL, Signed), !Signed);
    return IntegralAP(V);
  }

  constexpr unsigned bitWidth() const { return V.getBitWidth(); }
+10 −0
Original line number Diff line number Diff line
@@ -1688,6 +1688,16 @@ bool Zero(InterpState &S, CodePtr OpPC) {
  return true;
}

static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
  S.Stk.push<IntegralAP<false>>(IntegralAP<false>::zero(BitWidth));
  return true;
}

static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
  S.Stk.push<IntegralAP<true>>(IntegralAP<true>::zero(BitWidth));
  return true;
}

template <PrimType Name, class T = typename PrimConv<Name>::T>
inline bool Null(InterpState &S, CodePtr OpPC) {
  S.Stk.push<T>();
+14 −1
Original line number Diff line number Diff line
@@ -72,6 +72,11 @@ def IntegerTypeClass : TypeClass {
               Uint32, Sint64, Uint64, IntAP, IntAPS];
}

def FixedSizeIntegralTypeClass : TypeClass {
  let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
               Uint32, Sint64, Uint64, Bool];
}

def NumberTypeClass : TypeClass {
  let Types = !listconcat(IntegerTypeClass.Types, [Float]);
}
@@ -243,10 +248,18 @@ def ConstBool : ConstOpcode<Bool, ArgBool>;

// [] -> [Integer]
def Zero : Opcode {
  let Types = [AluTypeClass];
  let Types = [FixedSizeIntegralTypeClass];
  let HasGroup = 1;
}

def ZeroIntAP : Opcode {
  let Args = [ArgUint32];
}

def ZeroIntAPS : Opcode {
  let Args = [ArgUint32];
}

// [] -> [Pointer]
def Null : Opcode {
  let Types = [PtrTypeClass];
+15 −0
Original line number Diff line number Diff line
@@ -17,6 +17,16 @@ constexpr MaxBitInt A_ = 0;
constexpr MaxBitInt B_ = A_ + 1;
static_assert(B_ == 1, "");

constexpr MaxBitInt BitIntZero{};
static_assert(BitIntZero == 0, "");
constexpr unsigned _BitInt(128) UBitIntZero{};
static_assert(UBitIntZero == 0, "");

constexpr _BitInt(2) BitIntZero2{};
static_assert(BitIntZero2 == 0, "");
constexpr unsigned _BitInt(1) UBitIntZero1{};
static_assert(UBitIntZero1 == 0, "");


#ifdef __SIZEOF_INT128__
namespace i128 {
@@ -49,6 +59,11 @@ namespace i128 {
  constexpr uint128_t AllOnes = ~static_cast<uint128_t>(0);
  static_assert(AllOnes == UINT128_MAX, "");

  constexpr uint128_t i128Zero{};
  static_assert(i128Zero == 0, "");
  constexpr uint128_t ui128Zero{};
  static_assert(ui128Zero == 0, "");

#if __cplusplus >= 201402L
  template <typename T>
  constexpr T CastFrom(__int128_t A) {