Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clang/lib/AST/ByteCode/BitcastBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ struct BitcastBuffer {
Data = std::make_unique<std::byte[]>(ByteSize);
}

/// Returns the byte at the given offset.
std::byte *atByte(unsigned Offset) {
assert(Offset < FinalBitSize.roundToBytes());
return Data.get() + Offset;
}

/// Returns the buffer size in bits.
Bits size() const { return FinalBitSize; }
Bytes byteSize() const { return FinalBitSize.toBytes(); }
Expand All @@ -113,6 +119,13 @@ struct BitcastBuffer {
std::unique_ptr<std::byte[]> copyBits(Bits BitOffset, Bits BitWidth,
Bits FullBitWidth,
Endian TargetEndianness) const;

/// Dereferences the value at the given offset.
template <typename T> T deref(Bytes Offset) const {
assert(Offset.getQuantity() < FinalBitSize.roundToBytes());
assert((Offset.getQuantity() + sizeof(T)) <= FinalBitSize.roundToBytes());
return *reinterpret_cast<T *>(Data.get() + Offset.getQuantity());
}
};

} // namespace interp
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1997,8 +1997,8 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
for (size_t I = 0; I != CmpSize; I += ElemSize) {
if (IsWide) {
INT_TYPE_SWITCH(*S.getContext().classify(ASTCtx.getWCharType()), {
T A = *reinterpret_cast<T *>(BufferA.Data.get() + I);
T B = *reinterpret_cast<T *>(BufferB.Data.get() + I);
T A = *reinterpret_cast<T *>(BufferA.atByte(I));
T B = *reinterpret_cast<T *>(BufferB.atByte(I));
if (A < B) {
pushInteger(S, -1, Call->getType());
return true;
Expand All @@ -2009,8 +2009,8 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
}
});
} else {
std::byte A = BufferA.Data[I];
std::byte B = BufferB.Data[I];
std::byte A = BufferA.deref<std::byte>(Bytes(I));
std::byte B = BufferB.deref<std::byte>(Bytes(I));

if (A < B) {
pushInteger(S, -1, Call->getType());
Expand Down