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`.
14Ref 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(Ref lvl, Ref 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_extended_ops());
35}
36
37} // namespace
38
39template<dbg id> Ref normalize_dbg(Ref type, Ref callee, Ref arg) {
40 auto& world = arg->world();
41 auto [lvl, x] = arg->projs<2>();
42 debug_print(lvl, x);
43 return id == dbg::perm ? world.raw_app(type, callee, arg) : Ref(x);
44}
45
46Ref normalize_reify(Ref, Ref, Ref arg) { return do_reify(arg); }
47
48Ref normalize_reflect(Ref, Ref, Ref arg) { return do_reflect(arg); }
49
50Ref normalize_refine(Ref type, Ref callee, Ref arg) {
51 auto& world = arg->world();
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 world.raw_app(type, callee, arg);
59}
60
61Ref normalize_gid(Ref, Ref, Ref arg) { return arg->world().lit_nat(arg->gid()); }
62
64
65} // namespace mim::plug::refly
World & world() const
Definition def.cpp:415
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == -1_n) or std::array (otherwise).
Definition def.h:367
u32 gid() const
Definition def.h:239
static std::optional< T > isa(Ref def)
Definition def.h:763
Level
Definition log.h:22
Helper class to retrieve Infer::arg if present.
Definition def.h:86
const Lit * lit_nat(nat_t a)
Definition world.h:388
@ Lit
Definition def.h:40
The refly Plugin
Definition remove_perm.h:6
Ref normalize_gid(Ref, Ref, Ref arg)
Ref normalize_refine(Ref type, Ref callee, Ref arg)
Ref normalize_dbg(Ref type, Ref callee, Ref arg)
Ref normalize_reflect(Ref, Ref, Ref arg)
Ref normalize_reify(Ref, Ref, Ref arg)
u64 nat_t
Definition types.h:43
uint64_t u64
Definition types.h:34
#define MIM_refly_NORMALIZER_IMPL
Definition autogen.h:107