From de3a3cb987f11075a62680c001d3cadd0a1d1397 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Timm=20B=C3=A4der?= Date: Sun, 22 Jan 2023 21:09:35 +0100 Subject: [PATCH] [clang][Interp] Fix compound assign operator types Just like we do (or will do) for floating types, we need to take into acocunt that the LHSComputationType, ResultType and type of the expression (what we ultimately store) might be different. Do this by emitting cast ops before and after doing the computation. This fixes the test failures introduced by 490e8214fca48824beda8b508d6d6bbbf3d8d9a7 on big endian machines. Differential Revision: https://reviews.llvm.org/D142328 --- clang/lib/AST/Interp/ByteCodeExprGen.cpp | 34 +++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index 615dbde..a777768 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -498,10 +498,13 @@ bool ByteCodeExprGen::VisitCompoundAssignOperator( const CompoundAssignOperator *E) { const Expr *LHS = E->getLHS(); const Expr *RHS = E->getRHS(); - std::optional LT = classify(E->getLHS()->getType()); - std::optional RT = classify(E->getRHS()->getType()); + std::optional LHSComputationT = + classify(E->getComputationLHSType()); + std::optional LT = classify(LHS->getType()); + std::optional RT = classify(E->getComputationResultType()); + std::optional ResultT = classify(E->getType()); - if (!LT || !RT) + if (!LT || !RT || !ResultT || !LHSComputationT) return false; assert(!E->getType()->isPointerType() && @@ -512,29 +515,36 @@ bool ByteCodeExprGen::VisitCompoundAssignOperator( return false; if (!this->emitLoad(*LT, E)) return false; + // If necessary, cast LHS to its computation type. + if (*LT != *LHSComputationT) { + if (!this->emitCast(*LT, *LHSComputationT, E)) + return false; + } + if (!visit(RHS)) return false; // Perform operation. switch (E->getOpcode()) { case BO_AddAssign: - if (!this->emitAdd(*LT, E)) + if (!this->emitAdd(*LHSComputationT, E)) return false; break; case BO_SubAssign: - if (!this->emitSub(*LT, E)) + if (!this->emitSub(*LHSComputationT, E)) return false; break; case BO_MulAssign: case BO_DivAssign: case BO_RemAssign: + case BO_ShlAssign: - if (!this->emitShl(*LT, *RT, E)) + if (!this->emitShl(*LHSComputationT, *RT, E)) return false; break; case BO_ShrAssign: - if (!this->emitShr(*LT, *RT, E)) + if (!this->emitShr(*LHSComputationT, *RT, E)) return false; break; case BO_AndAssign: @@ -544,10 +554,16 @@ bool ByteCodeExprGen::VisitCompoundAssignOperator( llvm_unreachable("Unimplemented compound assign operator"); } + // And now cast from LHSComputationT to ResultT. + if (*ResultT != *LHSComputationT) { + if (!this->emitCast(*LHSComputationT, *ResultT, E)) + return false; + } + // And store the result in LHS. if (DiscardResult) - return this->emitStorePop(*LT, E); - return this->emitStore(*LT, E); + return this->emitStorePop(*ResultT, E); + return this->emitStore(*ResultT, E); } template bool ByteCodeExprGen::discard(const Expr *E) { -- 2.7.4