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
7namespace mim::plug::refly {
8
9static_assert(sizeof(void*) <= sizeof(u64), "pointer doesn't fit into Lit");
10
11namespace {
12
13// The trick is that we simply "box" the pointer of @p def inside a Lit of type `%refly.Code`.
14const Def* do_reify(const Def* def) {
15 auto& world = def->world();
16 return world.lit(world.call<Code>(def->type()), reinterpret_cast<u64>(def));
17}
18
19// And here we are doing the reverse to retrieve the original pointer again.
20const Def* do_reflect(const Def* def) { return reinterpret_cast<const Def*>(def->as<Lit>()->get()); }
21
22void debug_print(const Def* lvl, const Def* def) {
23 auto& world = def->world();
24 auto level = Log::Level::Debug;
25 if (auto l = Lit::isa(lvl))
26 level = (nat_t)Log::Level::Error <= *l && *l <= (nat_t)Log::Level::Debug ? (Log::Level)*l : Log::Level::Debug;
27 world.log().log(level, __FILE__, __LINE__, "{}debug_print: {}{}", rang::fg::yellow, def, rang::fg::reset);
28 world.log().log(level, def->loc(), "def : {}", def);
29 world.log().log(level, def->loc(), "id : {}", def->unique_name());
30 world.log().log(level, def->type()->loc(), "type: {}", def->type());
31 world.log().log(level, def->loc(), "node: {}", def->node_name());
32 world.log().log(level, def->loc(), "ops : {}", def->num_ops());
33 world.log().log(level, def->loc(), "proj: {}", def->num_projs());
34 world.log().log(level, def->loc(), "eops: {}", def->num_deps());
35}
36
37} // namespace
38
39template<dbg id> const Def* normalize_dbg(const Def*, const Def*, const Def* arg) {
40 auto [lvl, x] = arg->projs<2>();
41 debug_print(lvl, x);
42 return id == dbg::perm ? nullptr : x;
43}
44
45const Def* normalize_reify(const Def*, const Def*, const Def* arg) { return do_reify(arg); }
46
47const Def* normalize_reflect(const Def*, const Def*, const Def* arg) { return do_reflect(arg); }
48
49const Def* normalize_refine(const Def*, const Def*, const Def* arg) {
50 auto [code, i, x] = arg->projs<3>();
51 if (auto l = Lit::isa(i)) {
52 auto def = do_reflect(code);
53 return do_reify(def->refine(*l, do_reflect(x)));
54 }
55
56 return {};
57}
58
59const Def* normalize_gid(const Def*, const Def*, const Def* arg) { return arg->world().lit_nat(arg->gid()); }
60
62
63} // namespace mim::plug::refly
Base class for all Defs.
Definition def.h:198
World & world() const noexcept
Definition def.cpp:413
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:345
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:730
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_refine(const Def *, const Def *, const Def *arg)
const Def * normalize_gid(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
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:107