MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
ds2cps.cpp
Go to the documentation of this file.
1#include "mim/phase/ds2cps.h"
2
3namespace mim {
4
5std::array<const Def*, 2> DS2CPS::rewrite_filter_body(Lam* old_lam, Lam* new_lam) {
6 auto scope = Scope(*this, Lam::isa_cn(new_lam));
7 auto [filter, body] = RWPhase::rewrite_filter_body(old_lam, new_lam);
8 return {filter, wire(scope.base(), body)};
9}
10
11const Def* DS2CPS::lift(const Def* k, Defs new_args, const Def* new_type, bool filter, Sym name) {
12 auto& w = new_world();
13 auto cont = w.mut_con(new_type)->set_filter(filter);
14 auto res = cont->var();
15 cont->set(name ? name : w.append_suffix(k->sym(), "_cont"));
16 bind_new(cont->has_var());
17 pending_.emplace_back(k, DefVec(new_args.begin(), new_args.end()), cont);
18 log().d("lift {} ({}, {})", k, fe::Join(new_args, ", "), cont);
19 return res;
20}
21
22const Def* DS2CPS::wire(size_t base, const Def* body) {
23 auto& w = new_world();
24 while (pending_.size() > base) {
25 auto [callee, args, cont] = std::move(pending_.back());
26 pending_.pop_back();
27 cont->set_body(body);
28 args.emplace_back(cont);
29 body = w.app(callee, args);
30 }
31 return body;
32}
33
34} // namespace mim
Scope(DS2CPS &phase, bool liftable)
Definition ds2cps.h:37
const Def * lift(const Def *k, Defs new_args, const Def *new_type, bool filter=false, Sym name={})
Records the call k (new_args..., cont) as pending and returns the Var of cont: Cn new_type as its res...
Definition ds2cps.cpp:11
const Def * wire(size_t base, const Def *body)
Wires all pending calls above base around body and returns the new body.
Definition ds2cps.cpp:22
std::array< const Def *, 2 > rewrite_filter_body(Lam *, Lam *) override
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
Definition ds2cps.cpp:5
Base class for all Defs.
Definition def.h:285
Sym sym() const
Definition def.h:629
A function.
Definition lam.h:91
static const Lam * isa_cn(const Def *d)
Definition lam.h:125
const fe::Log & log() const
Definition phase.h:62
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
std::string_view name() const
Definition phase.h:63
World & new_world()
Create new Defs into this.
Definition phase.h:418
virtual std::array< const Def *, 2 > rewrite_filter_body(Lam *old_lam, Lam *)
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
Definition phase.h:434
void bind_new(const Var *var)
Adds the new var - if any - to the innermost scope.
Definition rewrite.cpp:44
Definition ast.h:16
fe::View< const Def * > Defs
Definition def.h:94
fe::Vector< const Def * > DefVec
Definition def.h:96