MimIR 0.1
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().annexes())
7 visit(def, Lattice::Known);
8 for (auto def : old_world().externals())
9 visit(def, Lattice::Known);
10
11 return false; // no fixed-point neccessary
12}
13
14void EtaExpPhase::analyze(const Def* def) {
15 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
16 if (def->isa<Var>()) return; // ignore Var's mut
17
18 if (auto app = def->isa<App>()) {
19 visit(app->type());
20 visit(app->callee(), Lattice::Known);
21 visit(app->arg());
22 } else {
23 for (auto d : def->deps())
24 visit(d);
25 }
26}
27
28void EtaExpPhase::visit(const Def* def, Lattice l) {
29 if (auto lam = def->isa_mut<Lam>()) join(lam, l);
30 analyze(def);
31}
32
33void EtaExpPhase::rewrite_annex(flags_t f, const Def* def) { new_world().register_annex(f, rewrite_no_eta(def)); }
34
36 auto new_mut = rewrite_no_eta(old_mut)->as_mut();
37 if (old_mut->is_external()) new_mut->make_external();
38}
39
40const Def* EtaExpPhase::rewrite(const Def* old_def) {
41 if (auto lam = old_def->isa<Lam>(); lam && lattice(lam) == Both) {
42 auto [i, ins] = lam2eta_.emplace(lam, nullptr);
43 if (ins) i->second = Lam::eta_expand(rewrite_no_eta(lam));
44 DLOG("eta-expand: `{}` → `{}`", lam, i->second);
45 return i->second;
46 }
47
48 return Rewriter::rewrite(old_def);
49}
50
52 auto callee = rewrite_no_eta(app->callee());
53 return new_world().app(callee, rewrite(app->arg()));
54}
55
57 return new_world().var(rewrite_no_eta(var->mut())->as_mut());
58}
59
60} // namespace mim
const Def * callee() const
Definition lam.h:277
const Def * arg() const
Definition lam.h:286
Base class for all Defs.
Definition def.h:251
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:494
Defs deps() const noexcept
Definition def.cpp:467
bool is_external() const noexcept
Definition def.h:467
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, const Def *) final
void rewrite_external(Def *) final
bool analyze() final
You can do an optional fixed-point loop on the RWPhase::old_world before rewriting.
A function.
Definition lam.h:111
static Lam * eta_expand(Filter, const Def *f)
Definition lam.cpp:51
World & new_world()
Create new Defs into this.
Definition phase.h:100
World & old_world()
Get old Defs from here.
Definition phase.h:99
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:27
A variable introduced by a binder (mutable).
Definition def.h:700
Def * mut() const
Definition def.h:712
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:199
const Def * var(Def *mut)
Definition world.cpp:179
const Def * register_annex(flags_t f, const Def *)
Definition world.cpp:79
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:95
Definition ast.h:14
bool visit(IndexSet< Indexer, Key > &set, const Key &key)
Definition indexset.h:100
u64 flags_t
Definition types.h:45
@ Lam
Definition def.h:114