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 = Log::Level::Debug;
28 if (auto l = Lit::isa(lvl)) {
29 level = std::to_underlying(Log::Level::Error) <= int(*l) && int(*l) <= std::to_underlying(Log::Level::Debug)
30 ? static_cast<Log::Level>(*l)
32 }
33 world.log().log(level, __FILE__, __LINE__, "{}debug_print: {}{}", fe::term::FG::Yellow, def, fe::term::FG::Reset);
34 world.log().log(level, def->loc(), "def : {}", def);
35 world.log().log(level, def->loc(), "id : {}", def->unique_name());
36 world.log().log(level, def->type()->loc(), "type: {}", def->type());
37 world.log().log(level, def->loc(), "node: {}", def->node_name());
38 world.log().log(level, def->loc(), "ops : {}", def->num_ops());
39 world.log().log(level, def->loc(), "proj: {}", def->num_projs());
40 world.log().log(level, def->loc(), "eops: {}", def->num_deps());
41}
42
43} // namespace
44
45template<dbg id>
46const Def* normalize_dbg(const Def*, const Def*, const Def* arg) {
47 auto [lvl, x] = arg->projs<2>();
48 debug_print(lvl, x);
49 return id == dbg::perm ? nullptr : x;
50}
51
52const Def* normalize_reify(const Def*, const Def*, const Def* arg) { return do_reify(arg); }
53
54const Def* normalize_reflect(const Def*, const Def*, const Def* arg) { return do_reflect(arg); }
55
56const Def* normalize_refine(const Def*, const Def*, const Def* arg) {
57 auto [code, i, x] = arg->projs<3>();
58 if (auto l = Lit::isa(i)) {
59 auto def = do_reflect(code);
60 return do_reify(def->refine(*l, do_reflect(x)));
61 }
62
63 return {};
64}
65
66const Def* normalize_type(const Def*, const Def*, const Def* arg) { return arg->type(); }
67const Def* normalize_gid(const Def*, const Def*, const Def* arg) { return arg->world().lit_nat(arg->gid()); }
68
69template<equiv id>
70const Def* normalize_equiv(const Def*, const Def*, const Def* arg) {
71 auto [a, b] = arg->projs<2>();
72 constexpr bool eq = id == equiv::aE || id == equiv::AE;
73
74 if constexpr (id == equiv::Ae || id == equiv::AE) {
75 auto res = Checker::alpha<Checker::Test>(a, b);
76 if (res ^ eq) mim::error(arg->loc(), "'{}' and '{}' {}alpha-equivalent", a, b, !res ? "not " : "");
77 } else {
78 auto res = a == b;
79 if (res ^ eq) mim::error(arg->loc(), "'{}' and '{}' {}structural-equivalent", a, b, !res ? "not " : "");
80 }
81 return a;
82}
83
84const Def* normalize_check_bot(const Def*, const Def* c, const Def* arg) {
85 if (!arg->isa<Bot>()) mim::error(c->loc(), "'{}' is not bottom", arg);
86 return arg;
87}
88
89const Def* normalize_check(const Def* type, const Def*, const Def* arg) {
90 auto& w = type->world();
91 auto [cond, val, msg] = arg->projs<3>();
92
93 if (cond == w.lit_tt()) return val;
94 if (cond == w.lit_ff()) {
95 auto s = tuple2str(msg);
96 if (s.empty()) s = "unknown error"s;
97 w.ELOG("{}", s);
98 }
99
100 return nullptr;
101}
102
104
105} // namespace mim::plug::refly
static bool alpha(const Def *d1, const Def *d2)
Definition check.h:96
Base class for all Defs.
Definition def.h:246
size_t num_deps() const noexcept
Definition def.h:340
World & world() const noexcept
Definition def.cpp:444
std::string_view node_name() const
Definition def.cpp:465
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:386
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:452
Loc loc() const
Definition def.h:514
nat_t num_projs() const
Yields Def::arity(), if it is a Lit, or 1 otherwise.
Definition def.cpp:586
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:266
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:584
constexpr size_t num_ops() const noexcept
Definition def.h:305
static std::optional< T > isa(const Def *def)
Definition def.h:838
T get() const
Definition def.h:825
Level
Definition log.h:23
const Lit * lit(const Def *type, u64 val)
Definition world.cpp:534
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_refine(const Def *, 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:83
void error(std::format_string< Args... > fmt, Args &&... args)
Wraps std::format to throw T with a formatted message.
Definition dbg.h:17
TExt< false > Bot
Definition lattice.h:171
uint64_t u64
Definition types.h:27
#define MIM_refly_NORMALIZER_IMPL
Definition autogen.h:149