[clang] 581c015 - [clang][bytecode] Implement fixed point negation (#110237)

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 27 04:29:17 PDT 2024


Author: Timm Baeder
Date: 2024-09-27T13:29:14+02:00
New Revision: 581c015ed0cfe05d8dd3450375cd3db316e334f1

URL: https://github.com/llvm/llvm-project/commit/581c015ed0cfe05d8dd3450375cd3db316e334f1
DIFF: https://github.com/llvm/llvm-project/commit/581c015ed0cfe05d8dd3450375cd3db316e334f1.diff

LOG: [clang][bytecode] Implement fixed point negation (#110237)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/lib/AST/ByteCode/FixedPoint.h
    clang/lib/AST/ByteCode/Opcodes.td
    clang/lib/AST/ByteCode/PrimType.h
    clang/test/AST/ByteCode/fixed-point.cpp

Removed: 
    


################################################################################
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)));
+


        


More information about the cfe-commits mailing list