Skip to content

Commit d6b0b30

Browse files
committed
[clang][Interp] IntegralAP zero-init
1 parent 66c9915 commit d6b0b30

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,9 +1665,9 @@ bool ByteCodeExprGen<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
16651665
case PT_Uint64:
16661666
return this->emitZeroUint64(E);
16671667
case PT_IntAP:
1668+
return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
16681669
case PT_IntAPS:
1669-
assert(false);
1670-
return false;
1670+
return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
16711671
case PT_Ptr:
16721672
return this->emitNullPtr(E);
16731673
case PT_FnPtr:

clang/lib/AST/Interp/IntegralAP.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ template <bool Signed> class IntegralAP final {
7878

7979
template <typename T> static IntegralAP from(T Value, unsigned NumBits = 0) {
8080
assert(NumBits > 0);
81-
APSInt Copy = APSInt(APInt(NumBits, static_cast<int64_t>(Value), Signed), !Signed);
81+
APSInt Copy =
82+
APSInt(APInt(NumBits, static_cast<uint64_t>(Value), Signed), !Signed);
8283

8384
return IntegralAP<Signed>(Copy);
8485
}
@@ -97,16 +98,16 @@ template <bool Signed> class IntegralAP final {
9798
template <unsigned Bits, bool InputSigned>
9899
static IntegralAP from(Integral<Bits, InputSigned> I, unsigned BitWidth) {
99100
APSInt Copy =
100-
APSInt(APInt(BitWidth, static_cast<int64_t>(I), InputSigned), !Signed);
101+
APSInt(APInt(BitWidth, static_cast<uint64_t>(I), InputSigned), !Signed);
101102
Copy.setIsSigned(Signed);
102103

103104
assert(Copy.isSigned() == Signed);
104105
return IntegralAP<Signed>(Copy);
105106
}
106107

107-
static IntegralAP zero() {
108-
assert(false);
109-
return IntegralAP(0);
108+
static IntegralAP zero(int32_t BitWidth) {
109+
APSInt V = APSInt(APInt(BitWidth, 0LL, Signed), !Signed);
110+
return IntegralAP(V);
110111
}
111112

112113
constexpr unsigned bitWidth() const { return V.getBitWidth(); }

clang/lib/AST/Interp/Interp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,16 @@ bool Zero(InterpState &S, CodePtr OpPC) {
16881688
return true;
16891689
}
16901690

1691+
static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
1692+
S.Stk.push<IntegralAP<false>>(IntegralAP<false>::zero(BitWidth));
1693+
return true;
1694+
}
1695+
1696+
static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
1697+
S.Stk.push<IntegralAP<true>>(IntegralAP<true>::zero(BitWidth));
1698+
return true;
1699+
}
1700+
16911701
template <PrimType Name, class T = typename PrimConv<Name>::T>
16921702
inline bool Null(InterpState &S, CodePtr OpPC) {
16931703
S.Stk.push<T>();

clang/lib/AST/Interp/Opcodes.td

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ def IntegerTypeClass : TypeClass {
7272
Uint32, Sint64, Uint64, IntAP, IntAPS];
7373
}
7474

75+
def FixedSizeIntegralTypeClass : TypeClass {
76+
let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
77+
Uint32, Sint64, Uint64, Bool];
78+
}
79+
7580
def NumberTypeClass : TypeClass {
7681
let Types = !listconcat(IntegerTypeClass.Types, [Float]);
7782
}
@@ -243,10 +248,18 @@ def ConstBool : ConstOpcode<Bool, ArgBool>;
243248

244249
// [] -> [Integer]
245250
def Zero : Opcode {
246-
let Types = [AluTypeClass];
251+
let Types = [FixedSizeIntegralTypeClass];
247252
let HasGroup = 1;
248253
}
249254

255+
def ZeroIntAP : Opcode {
256+
let Args = [ArgUint32];
257+
}
258+
259+
def ZeroIntAPS : Opcode {
260+
let Args = [ArgUint32];
261+
}
262+
250263
// [] -> [Pointer]
251264
def Null : Opcode {
252265
let Types = [PtrTypeClass];

clang/test/AST/Interp/intap.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ constexpr MaxBitInt A_ = 0;
1717
constexpr MaxBitInt B_ = A_ + 1;
1818
static_assert(B_ == 1, "");
1919

20+
constexpr MaxBitInt BitIntZero{};
21+
static_assert(BitIntZero == 0, "");
22+
constexpr unsigned _BitInt(128) UBitIntZero{};
23+
static_assert(UBitIntZero == 0, "");
24+
25+
constexpr _BitInt(2) BitIntZero2{};
26+
static_assert(BitIntZero2 == 0, "");
27+
constexpr unsigned _BitInt(1) UBitIntZero1{};
28+
static_assert(UBitIntZero1 == 0, "");
29+
2030

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

62+
constexpr uint128_t i128Zero{};
63+
static_assert(i128Zero == 0, "");
64+
constexpr uint128_t ui128Zero{};
65+
static_assert(ui128Zero == 0, "");
66+
5267
#if __cplusplus >= 201402L
5368
template <typename T>
5469
constexpr T CastFrom(__int128_t A) {

0 commit comments

Comments
 (0)