Skip to content

Commit 046b064

Browse files
authored
[clang][bytecode][NFC] Use FixedPoint opaque int API (#123522)
Now that we have it, use it.
1 parent 71d6287 commit 046b064

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -689,20 +689,18 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
689689
if (!this->visit(SubExpr))
690690
return false;
691691

692-
auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
693-
uint32_t I;
694-
std::memcpy(&I, &Sem, sizeof(Sem));
695-
return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), I,
696-
CE);
692+
auto Sem =
693+
Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
694+
return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()),
695+
Sem, CE);
697696
}
698697
case CK_FloatingToFixedPoint: {
699698
if (!this->visit(SubExpr))
700699
return false;
701700

702-
auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
703-
uint32_t I;
704-
std::memcpy(&I, &Sem, sizeof(Sem));
705-
return this->emitCastFloatingFixedPoint(I, CE);
701+
auto Sem =
702+
Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
703+
return this->emitCastFloatingFixedPoint(Sem, CE);
706704
}
707705
case CK_FixedPointToFloating: {
708706
if (!this->visit(SubExpr))
@@ -718,10 +716,9 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
718716
case CK_FixedPointCast: {
719717
if (!this->visit(SubExpr))
720718
return false;
721-
auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
722-
uint32_t I;
723-
std::memcpy(&I, &Sem, sizeof(Sem));
724-
return this->emitCastFixedPoint(I, CE);
719+
auto Sem =
720+
Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
721+
return this->emitCastFixedPoint(Sem, CE);
725722
}
726723

727724
case CK_ToVoid:
@@ -1522,42 +1519,40 @@ template <class Emitter>
15221519
bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15231520
const Expr *LHS = E->getLHS();
15241521
const Expr *RHS = E->getRHS();
1522+
const ASTContext &ASTCtx = Ctx.getASTContext();
15251523

15261524
assert(LHS->getType()->isFixedPointType() ||
15271525
RHS->getType()->isFixedPointType());
15281526

1529-
auto LHSSema = Ctx.getASTContext().getFixedPointSemantics(LHS->getType());
1530-
auto RHSSema = Ctx.getASTContext().getFixedPointSemantics(RHS->getType());
1527+
auto LHSSema = ASTCtx.getFixedPointSemantics(LHS->getType());
1528+
auto LHSSemaInt = LHSSema.toOpaqueInt();
1529+
auto RHSSema = ASTCtx.getFixedPointSemantics(RHS->getType());
1530+
auto RHSSemaInt = RHSSema.toOpaqueInt();
15311531

15321532
if (!this->visit(LHS))
15331533
return false;
15341534
if (!LHS->getType()->isFixedPointType()) {
1535-
uint32_t I;
1536-
std::memcpy(&I, &LHSSema, sizeof(llvm::FixedPointSemantics));
1537-
if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()), I, E))
1535+
if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()),
1536+
LHSSemaInt, E))
15381537
return false;
15391538
}
15401539

15411540
if (!this->visit(RHS))
15421541
return false;
15431542
if (!RHS->getType()->isFixedPointType()) {
1544-
uint32_t I;
1545-
std::memcpy(&I, &RHSSema, sizeof(llvm::FixedPointSemantics));
1546-
if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()), I, E))
1543+
if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()),
1544+
RHSSemaInt, E))
15471545
return false;
15481546
}
15491547

15501548
// Convert the result to the target semantics.
15511549
auto ConvertResult = [&](bool R) -> bool {
15521550
if (!R)
15531551
return false;
1554-
auto ResultSema = Ctx.getASTContext().getFixedPointSemantics(E->getType());
1555-
auto CommonSema = LHSSema.getCommonSemantics(RHSSema);
1556-
if (ResultSema != CommonSema) {
1557-
uint32_t I;
1558-
std::memcpy(&I, &ResultSema, sizeof(ResultSema));
1559-
return this->emitCastFixedPoint(I, E);
1560-
}
1552+
auto ResultSema = ASTCtx.getFixedPointSemantics(E->getType()).toOpaqueInt();
1553+
auto CommonSema = LHSSema.getCommonSemantics(RHSSema).toOpaqueInt();
1554+
if (ResultSema != CommonSema)
1555+
return this->emitCastFixedPoint(ResultSema, E);
15611556
return true;
15621557
};
15631558

clang/lib/AST/ByteCode/Interp.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,9 +2141,8 @@ inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
21412141
}
21422142

21432143
inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
2144-
FixedPointSemantics TargetSemantics(0, 0, false, false, false);
2145-
std::memcpy(&TargetSemantics, &FPS, sizeof(TargetSemantics));
2146-
2144+
FixedPointSemantics TargetSemantics =
2145+
FixedPointSemantics::getFromOpaqueInt(FPS);
21472146
const auto &Source = S.Stk.pop<FixedPoint>();
21482147

21492148
bool Overflow;
@@ -2271,8 +2270,7 @@ static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
22712270
uint32_t FPS) {
22722271
const T &Int = S.Stk.pop<T>();
22732272

2274-
FixedPointSemantics Sem(0, 0, false, false, false);
2275-
std::memcpy(&Sem, &FPS, sizeof(Sem));
2273+
FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
22762274

22772275
bool Overflow;
22782276
FixedPoint Result = FixedPoint::from(Int.toAPSInt(), Sem, &Overflow);
@@ -2288,8 +2286,7 @@ static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
22882286
uint32_t FPS) {
22892287
const auto &Float = S.Stk.pop<Floating>();
22902288

2291-
FixedPointSemantics Sem(0, 0, false, false, false);
2292-
std::memcpy(&Sem, &FPS, sizeof(Sem));
2289+
FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
22932290

22942291
bool Overflow;
22952292
FixedPoint Result = FixedPoint::from(Float.getAPFloat(), Sem, &Overflow);

0 commit comments

Comments
 (0)