MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalizers.cpp
Go to the documentation of this file.
1#include <fe/term.h>
2
3#include <mim/tuple.h>
4#include <mim/world.h>
5
7
8using namespace std::string_literals;
9
11
12static_assert(sizeof(void*) <= sizeof(u64), "pointer doesn't fit into Lit");
13
14namespace {
15
16// The trick is that we simply "box" the pointer of @p def inside a Lit of type `refly.Code`.
17const Def* do_reify(const Def* def) {
18 auto& world = def->world();
19 return world.lit(world.call<Code>(def->type()), reinterpret_cast<u64>(def));
20}
21
22// And here we are doing the reverse to retrieve the original pointer again.
23const Def* do_reflect(const Def* def) { return reinterpret_cast<const Def*>(def->as<Lit>()->get()); }
24
25void debug_print(const Def* lvl, const Def* def) {
26 auto& world = def->world();
27 auto level = fe::Log::Level::Debug;
28 if (auto l = Lit::isa(lvl)) {
29 level = std::to_underlying(fe::Log::Level::Error) <= int(*l)
30 && int(*l) <= std::to_underlying(fe::Log::Level::Debug)
31 ? static_cast<fe::Log::Level>(*l)
32 : fe::Log::Level::Debug;
33 }
34 world.log().log(level, "{}debug_print: {}{}", fe::term::FG::Yellow, def, fe::term::FG::Reset);
35 world.log().log(level, def->loc(), "def : {}", def);
36 world.log().log(level, def->loc(), "id : {}", def->unique_name());
37 world.log().log(level, def->type()->loc(), "type: {}", def->type());
38 world.log().log(level, def->loc(), "node: {}", def->node_name());
39 world.log().log(level, def->loc(), "ops : {}", def->num_ops());
40 world.log().log(level, def->loc(), "proj: {}", def->num_projs());
41 world.log().log(level, def->loc(), "eops: {}", def->num_deps());
42}
43
44} // namespace
45
46template<dbg id>
47const Def* normalize_dbg(const Def*, const Def*, const Def* arg) {
48 auto [lvl, x] = arg->projs<2>();
49 debug_print(lvl, x);
50 return id == dbg::perm ? nullptr : x;
51}
52
53const Def* normalize_reify(const Def*, const Def*, const Def* arg) { return do_reify(arg); }
54
55const Def* normalize_reflect(const Def*, const Def*, const Def* arg) { return do_reflect(arg); }
56
57const Def* normalize_type(const Def*, const Def*, const Def* arg) { return arg->type(); }
58const Def* normalize_gid(const Def*, const Def*, const Def* arg) { return arg->world().lit_nat(arg->gid()); }
59
60template<equiv id>
61const Def* normalize_equiv(const Def*, const Def*, const Def* arg) {
62 auto [a, b] = arg->projs<2>();
63 constexpr bool eq = id == equiv::aE || id == equiv::AE;
64
65 if constexpr (id == equiv::Ae || id == equiv::AE) {
66 auto res = Checker::alpha<Checker::Test>(a, b);
67 if (res ^ eq) arg->blame("'{}' and '{}' {}alpha-equivalent", a, b, !res ? "not " : "").bail();
68 } else {
69 auto res = a == b;
70 if (res ^ eq) arg->blame("'{}' and '{}' {}structural-equivalent", a, b, !res ? "not " : "").bail();
71 }
72 return a;
73}
74
75const Def* normalize_check_bot(const Def*, const Def* c, const Def* arg) {
76 if (!arg->isa<Bot>()) c->blame("'{}' is not bottom", arg).bail();
77 return arg;
78}
79
80const Def* normalize_check(const Def* type, const Def*, const Def* arg) {
81 auto& w = type->world();
82 auto [cond, val, msg] = arg->projs<3>();
83
84 if (cond == w.lit_tt()) return val;
85 if (cond == w.lit_ff()) {
86 auto s = tuple2str(msg);
87 if (s.empty()) s = "unknown error"s;
88 w.log().e("{}", s);
89 }
90
91 return nullptr;
92}
93
95
96} // namespace mim::plug::refly
static bool alpha(const Def *d1, const Def *d2)
Definition check.h:93
Base class for all Defs.
Definition def.h:273
size_t num_deps() const noexcept
Definition def.h:394
World & world() const noexcept
Definition def.h:1097
std::string_view node_name() const
Definition def.cpp:459
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:440
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1111
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
Definition def.h:310
Loc loc() const
Definition def.h:611
nat_t num_projs() const
Yields Def::arity(), if it is a Lit, or 1 otherwise.
Definition def.h:1132
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:294
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:616
constexpr size_t num_ops() const noexcept
Definition def.h:352
static std::optional< T > isa(const Def *def)
Definition def.h:937
T get() const
Definition def.h:924
const Lit * lit(const Def *type, u64 val)
Definition world.cpp:564
const fe::Log & log() const
Log via log().e("...", args) etc.; owned by the Driver.
Definition world.cpp:129
const Lit * lit_nat(nat_t a)
Definition world.h:540
The refly Plugin
const Def * normalize_reify(const Def *, const Def *, const Def *arg)
const Def * normalize_dbg(const Def *, const Def *, const Def *arg)
const Def * normalize_check(const Def *type, const Def *, const Def *arg)
const Def * normalize_check_bot(const Def *, const Def *c, const Def *arg)
const Def * normalize_equiv(const Def *, const Def *, const Def *arg)
const Def * normalize_gid(const Def *, const Def *, const Def *arg)
const Def * normalize_type(const Def *, const Def *, const Def *arg)
const Def * normalize_reflect(const Def *, const Def *, const Def *arg)
std::string tuple2str(const Def *)
Definition tuple.cpp:48
TExt< false > Bot
Definition lattice.h:164
uint64_t u64
Definition types.h:27
#define MIM_refly_NORMALIZER_IMPL
Definition autogen.h:141