MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalizers.cpp
Go to the documentation of this file.
1#include <rang.hpp>
2
3#include "mim/world.h"
4
6
7using namespace std::string_literals;
8
9namespace mim::plug::refly {
10
11static_assert(sizeof(void*) <= sizeof(u64), "pointer doesn't fit into Lit");
12
13namespace {
14
15// The trick is that we simply "box" the pointer of @p def inside a Lit of type `%refly.Code`.
16const Def* do_reify(const Def* def) {
17 auto& world = def->world();
18 return world.lit(world.call<Code>(def->type()), reinterpret_cast<u64>(def));
19}
20
21// And here we are doing the reverse to retrieve the original pointer again.
22const Def* do_reflect(const Def* def) { return reinterpret_cast<const Def*>(def->as<Lit>()->get()); }
23
24void debug_print(const Def* lvl, const Def* def) {
25 auto& world = def->world();
26 auto level = Log::Level::Debug;
27 if (auto l = Lit::isa(lvl))
28 level = (nat_t)Log::Level::Error <= *l && *l <= (nat_t)Log::Level::Debug ? (Log::Level)*l : Log::Level::Debug;
29 world.log().log(level, __FILE__, __LINE__, "{}debug_print: {}{}", rang::fg::yellow, def, rang::fg::reset);
30 world.log().log(level, def->loc(), "def : {}", def);
31 world.log().log(level, def->loc(), "id : {}", def->unique_name());
32 world.log().log(level, def->type()->loc(), "type: {}", def->type());
33 world.log().log(level, def->loc(), "node: {}", def->node_name());
34 world.log().log(level, def->loc(), "ops : {}", def->num_ops());
35 world.log().log(level, def->loc(), "proj: {}", def->num_projs());
36 world.log().log(level, def->loc(), "eops: {}", def->num_deps());
37}
38
39} // namespace
40
41template<dbg id> const Def* normalize_dbg(const Def*, const Def*, const Def* arg) {
42 auto [lvl, x] = arg->projs<2>();
43 debug_print(lvl, x);
44 return id == dbg::perm ? nullptr : x;
45}
46
47const Def* normalize_reify(const Def*, const Def*, const Def* arg) { return do_reify(arg); }
48
49const Def* normalize_reflect(const Def*, const Def*, const Def* arg) { return do_reflect(arg); }
50
51const Def* normalize_refine(const Def*, const Def*, const Def* arg) {
52 auto [code, i, x] = arg->projs<3>();
53 if (auto l = Lit::isa(i)) {
54 auto def = do_reflect(code);
55 return do_reify(def->refine(*l, do_reflect(x)));
56 }
57
58 return {};
59}
60
61const Def* normalize_type(const Def*, const Def*, const Def* arg) { return arg->type(); }
62const Def* normalize_gid(const Def*, const Def*, const Def* arg) { return arg->world().lit_nat(arg->gid()); }
63
64template<equiv id> const Def* normalize_equiv(const Def*, const Def*, const Def* arg) {
65 auto [a, b] = arg->projs<2>();
66 bool eq = id & (equiv::aE & 0xff);
67
68 if (id & (equiv::Ae & 0xff)) {
69 auto res = Checker::alpha<Checker::Test>(a, b);
70 if (res ^ eq) mim::error(arg->loc(), "'{}' and '{}' {}alpha-equivalent", a, b, !res ? "not " : "");
71 } else {
72 auto res = a == b;
73 if (res ^ eq) mim::error(arg->loc(), "'{}' and '{}' {}structural-equivalent", a, b, !res ? "not " : "");
74 }
75 return a;
76}
77
78const Def* normalize_check(const Def* type, const Def*, const Def* arg) {
79 auto& w = type->world();
80 auto [cond, val, msg] = arg->projs<3>();
81
82 if (cond == w.lit_tt()) return val;
83 if (cond == w.lit_ff()) {
84 auto s = tuple2str(msg);
85 if (s.empty()) s = "unknown error"s;
86 w.ELOG(s.c_str());
87 }
88
89 return nullptr;
90}
91
93
94} // namespace mim::plug::refly
static bool alpha(const Def *d1, const Def *d2)
Definition check.h:63
Base class for all Defs.
Definition def.h:198
World & world() const noexcept
Definition def.cpp:371
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:340
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:242
Loc loc() const
Definition def.h:445
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:217
static std::optional< T > isa(const Def *def)
Definition def.h:719
Level
Definition log.h:22
const Lit * lit_nat(nat_t a)
Definition world.h:384
The refly Plugin
Definition remove_perm.h:7
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_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)
u64 nat_t
Definition types.h:43
std::string tuple2str(const Def *)
Definition tuple.cpp:48
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:122
uint64_t u64
Definition types.h:34
@ Level
Definition def.h:94
@ Lit
Definition def.h:85
#define MIM_refly_NORMALIZER_IMPL
Definition autogen.h:141