https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/110405
None >From 58de93b574d2b8e47d830b79a136521c10eca1c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com> Date: Sun, 29 Sep 2024 06:12:01 +0200 Subject: [PATCH] [clang][bytecode] Implement fixed-point add --- clang/lib/AST/ByteCode/Compiler.cpp | 3 +++ clang/lib/AST/ByteCode/FixedPoint.h | 36 ++++++++++++++++++++++++- clang/lib/AST/ByteCode/Opcodes.td | 4 +-- clang/test/AST/ByteCode/fixed-point.cpp | 12 +++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 77bfb3d8cd7f1a..ef058e8da44b34 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -1528,6 +1528,9 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) { case BO_GE: return this->emitGEFixedPoint(E); #endif + case BO_Add: + return this->emitAddFixedPoint(E); + default: return this->emitInvalid(E); } diff --git a/clang/lib/AST/ByteCode/FixedPoint.h b/clang/lib/AST/ByteCode/FixedPoint.h index fb8457a08e93cc..0a808e13eda4eb 100644 --- a/clang/lib/AST/ByteCode/FixedPoint.h +++ b/clang/lib/AST/ByteCode/FixedPoint.h @@ -47,15 +47,28 @@ 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(); } + APSInt toAPSInt(unsigned BitWidth = 0) const { return V.getValue(); } unsigned bitWidth() const { return V.getWidth(); } bool isSigned() const { return V.isSigned(); } + bool isZero() const { return V.getValue().isZero(); } + bool isNegative() const { return V.getValue().isNegative(); } + bool isPositive() const { return V.getValue().isNonNegative(); } + bool isMin() const { + return V.getValue() == APSInt::getMinValue(V.getSemantics().getWidth(), + !V.getSemantics().isSigned()); + } + + FixedPoint truncate(unsigned BitWidth) const { return *this; } llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const { return V.convertToFloat(*Sem); } + std::string toDiagnosticString(const ASTContext &Ctx) const { + return V.toString(); + } + ComparisonCategoryResult compare(const FixedPoint &Other) const { if (Other.V == V) return ComparisonCategoryResult::Equal; @@ -67,6 +80,27 @@ class FixedPoint final { *R = FixedPoint(A.V.negate(&Overflow)); return Overflow; } + + static bool add(const FixedPoint A, const FixedPoint B, unsigned Bits, + FixedPoint *R) { + bool Overflow = false; + *R = FixedPoint(A.V.add(B.V, &Overflow)); + return Overflow; + } + static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits, + FixedPoint *R) { + return true; + } + static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits, + FixedPoint *R) { + return true; + } + static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits, + FixedPoint *R) { + return true; + } + static bool increment(const FixedPoint &A, FixedPoint *R) { return true; } + static bool decrement(const FixedPoint &A, FixedPoint *R) { return true; } }; inline FixedPoint getSwappedBytes(FixedPoint F) { return F; } diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td index 4f11b927989004..ceea2accc22eff 100644 --- a/clang/lib/AST/ByteCode/Opcodes.td +++ b/clang/lib/AST/ByteCode/Opcodes.td @@ -98,7 +98,7 @@ def FloatTypeClass : TypeClass { } def AluTypeClass : TypeClass { - let Types = !listconcat(IntegerTypeClass.Types, [Bool]); + let Types = !listconcat(IntegerTypeClass.Types, [Bool], [FixedPoint]); } def PtrTypeClass : TypeClass { @@ -110,7 +110,7 @@ def NonPtrTypeClass : TypeClass { } def AllTypeClass : TypeClass { - let Types = !listconcat(AluTypeClass.Types, PtrTypeClass.Types, FloatTypeClass.Types, [FixedPoint]); + let Types = !listconcat(AluTypeClass.Types, PtrTypeClass.Types, FloatTypeClass.Types); } def ComparableTypeClass : TypeClass { diff --git a/clang/test/AST/ByteCode/fixed-point.cpp b/clang/test/AST/ByteCode/fixed-point.cpp index 68137618b5db60..03324c79fc9cae 100644 --- a/clang/test/AST/ByteCode/fixed-point.cpp +++ b/clang/test/AST/ByteCode/fixed-point.cpp @@ -35,3 +35,15 @@ namespace FloatToFixedPointCast { constexpr float sf2f = sf2; static_assert(sf2f == 0.5); } + +namespace BinOps { + constexpr _Accum A = 13; + static_assert(A + 1 == 14.0k); + static_assert(1 + A == 14.0k); + static_assert((A + A) == 26); + + /// FIXME: Conversion between fixed point semantics. + static_assert(A + 100000 == 14.0k); // expected-error {{static assertion failed}} \ + // ref-error {{is not an integral constant expression}} \ + // ref-note {{is outside the range of representable values}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits