From af3d0ad1926eb825f64152cf217fc9a4777f0be3 Mon Sep 17 00:00:00 2001 From: Stefan Weigl-Bosker Date: Thu, 19 Feb 2026 18:45:44 -0500 Subject: [willow]: more cleanup, tests --- willow/lib/IR/Constant.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 willow/lib/IR/Constant.cpp (limited to 'willow/lib/IR/Constant.cpp') diff --git a/willow/lib/IR/Constant.cpp b/willow/lib/IR/Constant.cpp new file mode 100644 index 0000000..b9c15c7 --- /dev/null +++ b/willow/lib/IR/Constant.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +namespace willow { + +ConstantInt::ConstantInt(Type ty, uint64_t val) + : Constant(ty, ConstantKind::Int) { + const size_t w = ty.getNumBits(); + assert(w <= 64 && "Come back when im less lazy"); + // assert(!(val >> w) && "Truncated constant"); + + if (w == 64) + bits = val; + else { + const uint64_t m = (uint64_t{1} << w) - 1; + bits = val & m; + } +} + +int64_t ConstantInt::getSExtVal() const { + const size_t w = getType().getNumBits(); + + if (w == 64) + return static_cast(bits); + + unsigned sh = 64 - w; + return (static_cast(bits << sh)) >> sh; +} + +size_t ConstantInt::Hash::operator()(const ConstantInt::Key &k) const noexcept { + auto h1 = std::hash{}(k.ty); + auto h2 = std::hash{}(k.bits); + + return h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 6) + (h1 >> 2)); +} + +} // namespace willow + +// TODO +std::ostream &operator<<(std::ostream &os, const willow::Constant &c) { + if (c.isPoison()) + return os << "poison"; + + if (c.isInt()) { + return os << static_cast(&c)->getSExtVal(); + } + + std::unreachable(); + + return os; +} -- cgit v1.2.3