diff options
| author | Stefan Weigl-Bosker <stefan@s00.xyz> | 2026-02-19 18:45:44 -0500 |
|---|---|---|
| committer | Stefan Weigl-Bosker <stefan@s00.xyz> | 2026-02-19 18:45:44 -0500 |
| commit | af3d0ad1926eb825f64152cf217fc9a4777f0be3 (patch) | |
| tree | 8d7f73ce6c3c89863418382d8d553a06c668bbb3 /willow/lib/IR/Constant.cpp | |
| parent | d11fbc8268f5775ad783f8570478daad4a9e81cf (diff) | |
| download | compiler-more-tests.tar.gz | |
[willow]: more cleanup, testsmore-tests
Diffstat (limited to 'willow/lib/IR/Constant.cpp')
| -rw-r--r-- | willow/lib/IR/Constant.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
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 <willow/IR/Constant.h> +#include <utility> +#include <cassert> +#include <ostream> + +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<int64_t>(bits); + + unsigned sh = 64 - w; + return (static_cast<int64_t>(bits << sh)) >> sh; +} + +size_t ConstantInt::Hash::operator()(const ConstantInt::Key &k) const noexcept { + auto h1 = std::hash<TypeImpl *>{}(k.ty); + auto h2 = std::hash<uint64_t>{}(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<const willow::ConstantInt *>(&c)->getSExtVal(); + } + + std::unreachable(); + + return os; +} |