MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
eta_exp_phase.cpp
Go to the documentation of this file.
2
3namespace mim {
4
6 for (auto def : old_world().roots())
7 visit(def, Lattice::Known);
8 return false; // no fixed-point neccessary
9}
10
11void EtaExpPhase::analyze(const Def* def) {
12 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
13 if (def->isa<Var>()) return; // ignore Var's mut
14
15 if (auto app = def->isa<App>()) {
16 visit(app->type(), Lattice::Unknown);
17 visit(app->callee(), Lattice::Known);
18 visit(app->arg(), Lattice::Unknown);
19 } else {
20 for (auto d : def->deps())
21 visit(d, Lattice::Unknown);
22 }
23}
24
25void EtaExpPhase::visit(const Def* def, Lattice l) {
26 if (auto lam = def->isa_mut<Lam>()) join(lam, l);
27 analyze(def);
28}
29
30void EtaExpPhase::rewrite_annex(flags_t flags, Sym sym, const Def* def) {
31 new_world().annexes().attach(flags, sym, rewrite_no_eta(def));
32}
33
35 auto new_mut = rewrite_no_eta(old_mut)->as_mut();
36 if (old_mut->is_external()) new_mut->externalize();
37}
38
39const Def* EtaExpPhase::rewrite(const Def* old_def) {
40 if (auto lam = old_def->isa<Lam>(); lam && lattice(lam) == Both) {
41 auto [i, ins] = lam2eta_.emplace(lam, nullptr);
42 if (ins) i->second = Lam::eta_expand(rewrite_no_eta(lam));
43 DLOG("eta-expand: `{}` → `{}`", lam, i->second);
44 return i->second;
45 }
46
47 return RWPhase::rewrite(old_def);
48}
49
51 auto callee = rewrite_no_eta(app->callee());
52 return new_world().app(callee, rewrite(app->arg()));
53}
54
56 return new_world().var(rewrite_no_eta(var->mut())->as_mut());
57}
58
59} // namespace mim
const Def * callee() const
Definition lam.h:276
const Def * arg() const
Definition lam.h:285
Base class for all Defs.
Definition def.h:246
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:502
Defs deps() const noexcept
Definition def.cpp:475
bool is_external() const noexcept
Definition def.h:474
const Def * rewrite(const Def *) final
const Def * rewrite_imm_Var(const Var *) final
const Def * rewrite_imm_App(const App *) final
void rewrite_annex(flags_t, Sym, const Def *) final
void rewrite_external(Def *) final
bool analyze() final
Runs the optional pre-analysis on RWPhase::old_world(), typically to a fixed point,...
A function.
Definition lam.h:110
static Lam * eta_expand(Filter, const Def *f)
Definition lam.cpp:51
World & new_world()
Create new Defs into this.
Definition phase.h:243
World & old_world()
Get old Defs from here.
Definition phase.h:242
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:56
A variable introduced by a binder (mutable).
Definition def.h:714
Def * mut() const
Definition def.h:726
const Def * attach(flags_t, Sym, const Def *)
Definition world.cpp:43
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:205
Annexes & annexes()
Definition world.h:285
const Def * var(Def *mut)
Definition world.cpp:184
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:94
Definition ast.h:14
u64 flags_t
Definition types.h:39
@ Lam
Definition def.h:109