#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; }