MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalizers.cpp
Go to the documentation of this file.
1#include "mim/world.h"
2
3#include "mim/plug/ord/ord.h"
4
5#include "fe/assert.h"
6
7namespace mim::plug::ord {
8
9template<init id> const Def* normalize_init(const Def* type, const Def* callee, const Def* arg) {
10 auto& world = type->world();
11 return world.raw_app(type, callee, arg);
12}
13
14template<size> const Def* normalize_size(const Def*, const Def*, const Def* arg) {
15 if (auto init = Axm::isa<ord::init>(arg)) return init->decurry()->arg();
16 return nullptr;
17}
18
19const Def* normalize_get(const Def*, const Def*, const Def* arg) {
20 auto [c, k] = arg->projs<2>();
21
22 if (auto init = Axm::isa<ord::init>(c)) {
23 if (auto tuple = init->arg()->isa<Tuple>()) {
24 for (auto kv : tuple->ops())
25 if (kv->proj(2, 0) == k) return kv->proj(2, 1);
26 }
27 }
28
29 return nullptr;
30}
31
32template<mem id> const Def* normalize_mem(const Def*, const Def*, const Def* arg) {
33 auto& w = arg->world();
34 auto [k, c] = arg->projs<2>();
35
36 if (auto init = Axm::isa<ord::init>(c)) {
37 if (auto tuple = init->arg()->isa<Tuple>()) {
38 for (auto kv : tuple->ops()) {
39 auto key = id == mem::map ? kv->proj(2, 0) : kv;
40 if (key == k) return w.lit_tt();
41 }
42 return tuple->is_closed() ? w.lit_ff() : nullptr;
43 }
44
45 if (auto pack = init->arg()->isa_imm<Pack>()) w.WLOG("packs not yet implemented: {}", pack);
46 }
47
48 return nullptr;
49}
50
51template<insert id> const Def* normalize_insert(const Def*, const Def*, const Def*) {
52 return nullptr;
53#if 0
54 auto& world = type->world();
55 auto [c, kv] = arg->projs<2>();
56
57 if (auto init = Axm::isa<ord::init>(c)) {
58 if (auto tuple = init->arg()->isa<Tuple>()) {
59 auto n = init->decurry()->arg();
60 auto KV = init->decurry()->decurry()->arg();
61 if (auto l = Lit::isa(n)) {
62 DefVec new_ops;
63 bool updated = false;
64 for (size_t i = 0, e = *l; i != e; ++i) {
65 auto key = id == mem::map ? kv->proj(2, 0) : kv;
66 auto kv = tuple->proj(e, i);
67 if (kv->proj(2, 0) == k) {
68 updated = true;
69 new_ops.emplace_back(world.tuple({k, v}));
70 } else {
71 new_ops.emplace_back(tuple->proj(e, i));
72 }
73 }
74 if (!updated) new_ops.emplace_back(world.tuple({k, v}));
75 return world.call(id, KV, new_ops.size(), Defs(new_ops));
76 }
77 }
78
79 if (auto pack = init->arg()->isa_imm<Pack>()) w.WLOG("packs not yet implemented: {}", pack);
80 }
81
82 return world.raw_app(type, callee, arg);
83#endif
84}
85
87
88} // namespace mim::plug::ord
static auto isa(const Def *def)
Definition axm.h:104
Base class for all Defs.
Definition def.h:198
World & world() const noexcept
Definition def.cpp:387
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
static std::optional< T > isa(const Def *def)
Definition def.h:713
A (possibly paramterized) Tuple.
Definition tuple.h:122
Data constructor for a Sigma.
Definition tuple.h:56
The ord Plugin
const Def * normalize_insert(const Def *, const Def *, const Def *)
const Def * normalize_size(const Def *, const Def *, const Def *arg)
const Def * normalize_mem(const Def *, const Def *, const Def *arg)
const Def * normalize_get(const Def *, const Def *, const Def *arg)
const Def * normalize_init(const Def *type, const Def *callee, const Def *arg)
The tuple Plugin
View< const Def * > Defs
Definition def.h:49
Vector< const Def * > DefVec
Definition def.h:50
#define MIM_ord_NORMALIZER_IMPL
Definition autogen.h:112