Skip to content

Commit 48c0eeb

Browse files
committed
Handle BitCast and BitCastFP opcodes the same
1 parent f3a05e4 commit 48c0eeb

File tree

3 files changed

+16
-39
lines changed

3 files changed

+16
-39
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,18 @@ bool ByteCodeExprGen<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
117117

118118
assert(ToT);
119119

120+
const llvm::fltSemantics *TargetSemantics = nullptr;
121+
if (ToT == PT_Float)
122+
TargetSemantics = &Ctx.getFloatSemantics(ToType);
123+
120124
// Conversion to a primitive type. FromType can be another
121125
// primitive type, or a record/array.
122-
//
123-
// Same thing for floats, but we need the target
124-
// semantics here.
125-
if (ToT == PT_Float) {
126-
const auto *TargetSemantics = &Ctx.getFloatSemantics(ToType);
127-
CharUnits FloatSize = Ctx.getASTContext().getTypeSizeInChars(ToType);
128-
return this->emitBitCastFP(TargetSemantics, FloatSize.getQuantity(), E);
129-
}
130-
131126
bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
132127
ToType->isSpecificBuiltinType(BuiltinType::Char_U));
133128
uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
134129

135130
if (!this->emitBitCast(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
136-
ResultBitWidth, E))
131+
ResultBitWidth, TargetSemantics, E))
137132
return false;
138133

139134
if (DiscardResult)

clang/lib/AST/Interp/Interp.h

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,8 @@ template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
15781578

15791579
template <PrimType Name, class ToT = typename PrimConv<Name>::T>
15801580
bool BitCast(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
1581-
uint32_t ResultBitWidth) {
1581+
uint32_t ResultBitWidth, const llvm::fltSemantics *Sem) {
1582+
assert(ResultBitWidth > 0);
15821583
const Pointer &FromPtr = S.Stk.pop<Pointer>();
15831584

15841585
size_t BuffSize = ResultBitWidth / 8;
@@ -1591,27 +1592,13 @@ bool BitCast(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
15911592
if (!CheckBitcast(S, OpPC, HasIndeterminateBits, TargetIsUCharOrByte))
15921593
return false;
15931594

1594-
S.Stk.push<ToT>(ToT::bitcastFromMemory(Buff.data(), ResultBitWidth));
1595-
return true;
1596-
}
1597-
1598-
/// Bitcast TO a float.
1599-
inline bool BitCastFP(InterpState &S, CodePtr OpPC,
1600-
const llvm::fltSemantics *Sem, uint32_t TargetSize) {
1601-
const Pointer &FromPtr = S.Stk.pop<Pointer>();
1602-
1603-
std::vector<std::byte> Buff(TargetSize);
1604-
bool HasIndeterminateBits = false;
1605-
1606-
if (!DoBitCast(S, OpPC, FromPtr, Buff.data(), TargetSize,
1607-
HasIndeterminateBits))
1608-
return false;
1609-
1610-
if (!CheckBitcast(S, OpPC, HasIndeterminateBits,
1611-
/*TargetIsUCharOrByte=*/false))
1612-
return false;
1613-
1614-
S.Stk.push<Floating>(Floating::bitcastFromMemory(Buff.data(), *Sem));
1595+
if constexpr (std::is_same_v<ToT, Floating>) {
1596+
assert(Sem);
1597+
S.Stk.push<Floating>(Floating::bitcastFromMemory(Buff.data(), *Sem));
1598+
} else {
1599+
assert(!Sem);
1600+
S.Stk.push<ToT>(ToT::bitcastFromMemory(Buff.data(), ResultBitWidth));
1601+
}
16151602
return true;
16161603
}
16171604

clang/lib/AST/Interp/Opcodes.td

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,22 +586,17 @@ def Cast: Opcode {
586586
}
587587

588588
def BitCastTypeClass : TypeClass {
589-
let Types = [Uint8, Sint8, Uint16, Sint16, Uint32, Sint32, Uint64, Sint64, IntAP, IntAPS, Bool];
589+
let Types = [Uint8, Sint8, Uint16, Sint16, Uint32, Sint32, Uint64, Sint64, IntAP, IntAPS, Bool, Float];
590590
}
591591

592592
def BitCast : Opcode {
593593
let Types = [BitCastTypeClass];
594594
let HasGroup = 1;
595-
let Args = [ArgBool, ArgUint32];
595+
let Args = [ArgBool, ArgUint32, ArgFltSemantics];
596596
}
597597

598598
def BitCastPtr : Opcode;
599599

600-
def BitCastFP : Opcode {
601-
let Types = [];
602-
let Args = [ArgFltSemantics, ArgUint32];
603-
}
604-
605600
def CastFP : Opcode {
606601
let Types = [];
607602
let Args = [ArgFltSemantics, ArgRoundingMode];

0 commit comments

Comments
 (0)