https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/110237
None >From 232b8a42f922b1f325734cc7feadd4108c58a5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com> Date: Fri, 27 Sep 2024 12:21:32 +0200 Subject: [PATCH] [clang][bytecode] Implement fixed point negation --- clang/lib/AST/ByteCode/Compiler.cpp | 4 ++-- clang/lib/AST/ByteCode/FixedPoint.h | 16 +++++++++++++--- clang/lib/AST/ByteCode/Opcodes.td | 2 +- clang/lib/AST/ByteCode/PrimType.h | 12 ++++++------ clang/test/AST/ByteCode/fixed-point.cpp | 2 ++ 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index aac3fd384130d7..78ba1a7eec6620 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -724,9 +724,9 @@ bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) { assert(E->getType()->isFixedPointType()); assert(classifyPrim(E) == PT_FixedPoint); - // FIXME: Semantics. + auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType()); APInt Value = E->getValue(); - return this->emitConstFixedPoint(Value, E); + return this->emitConstFixedPoint(FixedPoint(Value, Sem), E); } template <class Emitter> diff --git a/clang/lib/AST/ByteCode/FixedPoint.h b/clang/lib/AST/ByteCode/FixedPoint.h index 5c4043f060ec56..fba793cd59e7e1 100644 --- a/clang/lib/AST/ByteCode/FixedPoint.h +++ b/clang/lib/AST/ByteCode/FixedPoint.h @@ -17,16 +17,16 @@ namespace clang { namespace interp { using APInt = llvm::APInt; +using APSInt = llvm::APSInt; /// Wrapper around fixed point types. class FixedPoint final { private: llvm::APFixedPoint V; + FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {} public: - FixedPoint(APInt V) - : V(V, - llvm::FixedPointSemantics(V.getBitWidth(), 0, false, false, false)) {} + FixedPoint(APInt V, llvm::FixedPointSemantics Sem) : V(V, Sem) {} // This needs to be default-constructible so llvm::endian::read works. FixedPoint() : V(APInt(0, 0ULL, false), @@ -42,12 +42,22 @@ class FixedPoint final { void print(llvm::raw_ostream &OS) const { OS << V; } APValue toAPValue(const ASTContext &) const { return APValue(V); } + APSInt toAPSInt(unsigned BitWidth) const { return V.getValue(); } + + unsigned bitWidth() const { return V.getWidth(); } + bool isSigned() const { return V.isSigned(); } ComparisonCategoryResult compare(const FixedPoint &Other) const { if (Other.V == V) return ComparisonCategoryResult::Equal; return ComparisonCategoryResult::Unordered; } + + static bool neg(const FixedPoint &A, FixedPoint *R) { + bool Overflow = false; + *R = FixedPoint(A.V.negate(&Overflow)); + return Overflow; + } }; inline FixedPoint getSwappedBytes(FixedPoint F) { return F; } diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td index 84c5a1d1ab4c0d..5fdafd1bf81984 100644 --- a/clang/lib/AST/ByteCode/Opcodes.td +++ b/clang/lib/AST/ByteCode/Opcodes.td @@ -106,7 +106,7 @@ def PtrTypeClass : TypeClass { } def NonPtrTypeClass : TypeClass { - let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float]); + let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float], [FixedPoint]); } def AllTypeClass : TypeClass { diff --git a/clang/lib/AST/ByteCode/PrimType.h b/clang/lib/AST/ByteCode/PrimType.h index 23ca8027599cd5..59c04c4673d936 100644 --- a/clang/lib/AST/ByteCode/PrimType.h +++ b/clang/lib/AST/ByteCode/PrimType.h @@ -43,11 +43,11 @@ enum PrimType : unsigned { PT_IntAP = 8, PT_IntAPS = 9, PT_Bool = 10, - PT_Float = 11, - PT_Ptr = 12, - PT_FnPtr = 13, - PT_MemberPtr = 14, - PT_FixedPoint = 15, + PT_FixedPoint = 11, + PT_Float = 12, + PT_Ptr = 13, + PT_FnPtr = 14, + PT_MemberPtr = 15, }; inline constexpr bool isPtrType(PrimType T) { @@ -71,7 +71,7 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, return OS; } -constexpr bool isIntegralType(PrimType T) { return T <= PT_Bool; } +constexpr bool isIntegralType(PrimType T) { return T <= PT_FixedPoint; } /// Mapping from primitive types to their representation. template <PrimType T> struct PrimConv; diff --git a/clang/test/AST/ByteCode/fixed-point.cpp b/clang/test/AST/ByteCode/fixed-point.cpp index 24595ed96c166d..42ebdf64e1a9fe 100644 --- a/clang/test/AST/ByteCode/fixed-point.cpp +++ b/clang/test/AST/ByteCode/fixed-point.cpp @@ -7,3 +7,5 @@ static_assert((bool)0.0k); // both-error {{static assertion failed}} static_assert(1.0k == 1.0k); static_assert(1.0k != 1.0k); // both-error {{failed due to requirement '1.0k != 1.0k'}} +static_assert(-12.0k == -(-(-12.0k))); + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits