MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
def.cpp
Go to the documentation of this file.
1#include "mim/def.h"
2
3#include <algorithm>
4
5#include <absl/container/fixed_array.h>
6#include <fe/assert.h>
7
8#include "mim/world.h"
9
10#include "mim/util/hash.h"
11
12using namespace std::literals;
13
14namespace mim {
15
16template void Sets<const Var>::dot();
17template void Sets<Def>::dot();
18
19/*
20 * constructors
21 */
22
23Def::Def(World* world, Node node, const Def* type, Defs ops, flags_t flags)
24 : world_(world)
25 , flags_(flags)
26 , node_(node)
27 , mut_(false)
28 , external_(false)
29 , dep_(node == Node::Hole ? unsigned(Dep::Hole)
30 : node == Node::Proxy ? unsigned(Dep::Proxy)
31 : node == Node::Var ? (Dep::Var | Dep::Mut)
32 : 0)
33 , num_ops_(ops.size())
34 , type_(type) {
35 if (node == Node::Univ) {
36 gid_ = world->next_gid();
38 } else if (auto var = isa<Var>()) {
39 assert(flags_ == 0); // if we ever need flags here, we need to hash that
40 auto& world = type->world();
41 gid_ = world.next_gid();
42 vars_ = world.vars().insert(type->local_vars(), var);
43 muts_ = type->local_muts();
44 dep_ |= type->dep_;
45 auto op = ops[0];
46 ops_ptr()[0] = op;
47 hash_ = hash_begin(node_t(Node::Var));
48 hash_ = hash_combine(hash_, type->gid());
49 hash_ = hash_combine(hash_, op->gid());
50 } else {
51 hash_ = hash_begin(u8(node));
52 hash_ = hash_combine(hash_, flags_);
53
54 if (type) {
55 world = &type->world();
56 dep_ |= type->dep_;
57 vars_ = type->local_vars();
58 muts_ = type->local_muts();
59 hash_ = hash_combine(hash_, type->gid());
60 } else {
61 world = &ops[0]->world();
62 }
63
64 auto vars = &world->vars();
65 auto muts = &world->muts();
66 auto ptr = ops_ptr();
67 gid_ = world->next_gid();
68
69 for (size_t i = 0, e = ops.size(); i != e; ++i) {
70 auto op = ops[i];
71 ptr[i] = op;
72 dep_ |= op->dep_;
73 vars_ = vars->merge(vars_, op->local_vars());
74 muts_ = muts->merge(muts_, op->local_muts());
75 hash_ = hash_combine(hash_, op->gid());
76 }
77 }
78}
79
80Def::Def(Node n, const Def* type, Defs ops, flags_t flags)
81 : Def(nullptr, n, type, ops, flags) {}
82
83Def::Def(Node node, const Def* type, size_t num_ops, flags_t flags)
84 : flags_(flags)
85 , node_(node)
86 , mut_(true)
87 , external_(false)
88 , dep_(Dep::Mut | (node == Node::Hole ? Dep::Hole : Dep::None))
89 , num_ops_(num_ops)
90 , type_(type) {
91 gid_ = world().next_gid();
92 hash_ = mim::hash(gid());
93 var_ = nullptr;
94 std::fill_n(ops_ptr(), num_ops, nullptr);
95}
96
97Nat::Nat(World& world)
98 : Def(Node, world.type(), Defs{}, 0) {}
99
100UMax::UMax(World& world, Defs ops)
101 : Def(Node, world.univ(), ops, 0) {}
102
103// clang-format off
104
105/*
106 * rebuild
107 */
108
109const Def* Hole ::rebuild_(World&, const Def*, Defs ) const { fe::unreachable(); }
110const Def* Global ::rebuild_(World&, const Def*, Defs ) const { fe::unreachable(); }
111const Def* Idx ::rebuild_(World& w, const Def* , Defs ) const { return w.type_idx(); }
112const Def* Nat ::rebuild_(World& w, const Def* , Defs ) const { return w.type_nat(); }
113const Def* Univ ::rebuild_(World& w, const Def* , Defs ) const { return w.univ(); }
114const Def* App ::rebuild_(World& w, const Def* , Defs o) const { return w.app(o[0], o[1]); }
115const Def* Arr ::rebuild_(World& w, const Def* , Defs o) const { return w.arr(o[0], o[1]); }
116const Def* Extract::rebuild_(World& w, const Def* , Defs o) const { return w.extract(o[0], o[1]); }
117const Def* Inj ::rebuild_(World& w, const Def* t, Defs o) const { return w.inj(t, o[0])->set(dbg()); }
118const Def* Insert ::rebuild_(World& w, const Def* , Defs o) const { return w.insert(o[0], o[1], o[2]); }
119const Def* Lam ::rebuild_(World& w, const Def* t, Defs o) const { return w.lam(t->as<Pi>(), o[0], o[1]); }
120const Def* Lit ::rebuild_(World& w, const Def* t, Defs ) const { return w.lit(t, get()); }
121const Def* Merge ::rebuild_(World& w, const Def* t, Defs o) const { return w.merge(t, o); }
122const Def* Pack ::rebuild_(World& w, const Def* t, Defs o) const { return w.pack(t->arity(), o[0]); }
123const Def* Pi ::rebuild_(World& w, const Def* , Defs o) const { return w.pi(o[0], o[1], is_implicit()); }
124const Def* Proxy ::rebuild_(World& w, const Def* t, Defs o) const { return w.proxy(t, pass(), tag(), o); }
125const Def* Sigma ::rebuild_(World& w, const Def* , Defs o) const { return w.sigma(o); }
126const Def* Split ::rebuild_(World& w, const Def* t, Defs o) const { return w.split(t, o[0]); }
127const Def* Match ::rebuild_(World& w, const Def* , Defs o) const { return w.match(o); }
128const Def* Tuple ::rebuild_(World& w, const Def* t, Defs o) const { return w.tuple(t, o); }
129const Def* Type ::rebuild_(World& w, const Def* , Defs o) const { return w.type(o[0]); }
130const Def* UInc ::rebuild_(World& w, const Def* , Defs o) const { return w.uinc(o[0], offset()); }
131const Def* UMax ::rebuild_(World& w, const Def* , Defs o) const { return w.umax(o); }
132const Def* Uniq ::rebuild_(World& w, const Def* , Defs o) const { return w.uniq(o[0]); }
133const Def* Var ::rebuild_(World& w, const Def* t, Defs o) const { return w.var(t, o[0]->as_mut()); }
134
135const Def* Axm ::rebuild_(World& w, const Def* t, Defs ) const {
136 if (&w != &world()) return w.axm(normalizer(), curry(), trip(), t, plugin(), tag(), sub())->set(dbg());
138 return this;
139}
140
141template<bool up> const Def* TExt <up>::rebuild_(World& w, const Def* t, Defs ) const { return w.ext <up>(t)->set(dbg()); }
142template<bool up> const Def* TBound<up>::rebuild_(World& w, const Def* , Defs o) const { return w.bound<up>(o)->set(dbg()); }
143
144/*
145 * stub
146 */
147
148Arr* Arr ::stub_(World& w, const Def* t) { return w.mut_arr (t); }
149Global* Global::stub_(World& w, const Def* t) { return w.global (t, is_mutable()); }
150Hole* Hole ::stub_(World& w, const Def* t) { return w.mut_hole (t); }
151Lam* Lam ::stub_(World& w, const Def* t) { return w.mut_lam (t->as<Pi>()); }
152Pack* Pack ::stub_(World& w, const Def* t) { return w.mut_pack (t); }
153Pi* Pi ::stub_(World& w, const Def* t) { return w.mut_pi (t, is_implicit()); }
154Sigma* Sigma ::stub_(World& w, const Def* t) { return w.mut_sigma(t, num_ops()); }
155
156/*
157 * instantiate templates
158 */
159
160#ifndef DOXYGEN
161template const Def* TExt<false> ::rebuild_(World&, const Def*, Defs) const;
162template const Def* TExt<true > ::rebuild_(World&, const Def*, Defs) const;
163template const Def* TBound<false>::rebuild_(World&, const Def*, Defs) const;
164template const Def* TBound<true >::rebuild_(World&, const Def*, Defs) const;
165#endif
166
167// clang-format on
168
169/*
170 * immutabilize
171 */
172
174 if (!is_set()) return false;
175
176 if (auto v = has_var()) {
177 for (auto op : deps())
178 if (op->free_vars().contains(v)) return false;
179 }
180 for (auto op : deps()) {
181 for (auto mut : op->local_muts())
182 if (mut == this) return false; // recursion
183 }
184 return true;
185}
186
188 if (is_immutabilizable()) return world().pi(dom(), codom());
189 return nullptr;
190}
191
193 if (is_immutabilizable()) return static_cast<const Sigma*>(world().sigma(ops()));
194 return nullptr;
195}
196
198 auto& w = world();
199 if (is_immutabilizable()) return w.arr(arity(), body());
200
201 if (auto n = Lit::isa(arity()); n && *n < w.flags().scalarize_threshold)
202 return w.sigma(DefVec(*n, [&](size_t i) { return reduce(w.lit_idx(*n, i)); }));
203
204 return nullptr;
205}
206
208 auto& w = world();
209 if (is_immutabilizable()) return w.pack(arity(), body());
210
211 if (auto n = Lit::isa(arity()); n && *n < w.flags().scalarize_threshold)
212 return w.tuple(DefVec(*n, [&](size_t i) { return reduce(w.lit_idx(*n, i)); }));
213
214 return nullptr;
215}
216
217/*
218 * reduce
219 */
220
221Defs Def::reduce_(const Def* arg) const {
222 if (auto var = has_var()) return world().reduce(var, arg);
223 return {ops().begin() + reduction_offset(), num_ops() - reduction_offset()};
226const Def* Def::refine(size_t i, const Def* new_op) const {
227 auto new_ops = absl::FixedArray<const Def*>(num_ops());
228 for (size_t j = 0, e = num_ops(); j != e; ++j)
229 new_ops[j] = i == j ? new_op : op(j);
230 return rebuild(type(), new_ops);
231}
232
233/*
234 * Def - set
235 */
236
238 assert(ops.size() == num_ops());
239 for (size_t i = 0, e = num_ops(); i != e; ++i)
240 set(i, ops[i]);
241 return this;
242}
243
244Def* Def::set(size_t i, const Def* def) {
245#ifdef MIM_ENABLE_CHECKS
246 if (world().watchpoints().contains(gid())) fe::breakpoint();
247#endif
248 invalidate();
249 def = check(i, def);
250 assert(def && !op(i) && curr_op_++ == i);
251 ops_ptr()[i] = def;
252
253 if (i + 1 == num_ops()) { // set last op, so check kind
254 if (auto t = check()->zonk(); t != type()) type_ = t;
255 }
256
257 return this;
258}
259
260Def* Def::set_type(const Def* type) {
261 invalidate();
262 assert(curr_op_ == 0);
263 type_ = type;
264 return this;
265}
266
268 invalidate();
269#ifndef NDEBUG
270 curr_op_ = 0;
271#endif
272 std::ranges::fill(ops_ptr(), ops_ptr() + num_ops(), nullptr);
273 return this;
274}
275
276bool Def::is_set() const {
277 if (num_ops() == 0) return true;
278 bool result = ops().back();
279 assert((!result || std::ranges::all_of(ops().rsubspan(1), [](auto op) { return op; }))
280 && "the last operand is set but others in front of it aren't");
281 return result;
282}
283
284/*
285 * free_vars
286 */
287
288const Def* Def::var() {
289 if (var_) return var_;
290 auto& w = world();
291
292 // clang-format off
293 if (w.is_frozen()) return nullptr;
294 if (auto lam = isa<Lam >()) return w.var(lam ->dom(), lam);
295 if (auto pi = isa<Pi >()) return w.var(pi ->dom(), pi);
296 if (auto sig = isa<Sigma>()) return w.var(sig, sig);
297 if (auto arr = isa<Arr >()) return w.var(w.type_idx(arr ->arity()), arr ); // TODO shapes like (2, 3)
298 if (auto pack = isa<Pack >()) return w.var(w.type_idx(pack->arity()), pack); // TODO shapes like (2, 3)
299 if (isa<Bound >()) return w.var(this, this);
300 if (isa<Hole >()) return nullptr;
301 if (isa<Global>()) return nullptr;
302 // clang-format on
303 fe::unreachable();
304}
305
307 if (auto mut = isa_mut()) return Muts(mut);
308 return muts_;
309}
310
312 if (auto mut = isa_mut()) return mut->free_vars();
313
314 auto& vars = world().vars();
315 auto fvs = local_vars();
316 for (auto mut : local_muts())
317 fvs = vars.merge(fvs, mut->free_vars());
318
319 return fvs;
320}
321
322Vars Def::local_vars() const { return mut_ ? Vars() : vars_; }
323
325 if (mark_ == 0) {
326 // fixed-point iteration to recompute free vars:
327 // (run - 1) identifies the previous iteration; so make sure to offset run by 2 for the first iteration
328 auto& w = world();
329 bool cyclic = false;
330 w.next_run();
331 free_vars<true>(cyclic, w.next_run());
332
333 for (bool todo = cyclic; todo;) {
334 todo = false;
335 free_vars<false>(todo, w.next_run());
336 }
337 }
338
339 return vars_;
340}
341
342template<bool init>
343Vars Def::free_vars(bool& todo, uint32_t run) {
344 // If init == true : todo flag detects cycle.
345 // If init == false: todo flag keeps track whether sth changed.
346 //
347 // Recursively recompute free vars. If
348 // * mark_ == 0: Invalid - need to recompute.
349 // * mark_ == run - 1: Previous iteration - need to recompute.
350 // * mark_ == run: We are running in cycles within the *current* iteration of our fixed-point loop.
351 // * otherwise: Valid!
352 if (mark_ != 0 && mark_ != run - 1) {
353 if constexpr (init) todo |= mark_ == run;
354 return vars_;
355 }
356
357 mark_ = run;
358
359 auto fvs0 = vars_;
360 auto fvs = fvs0;
361 auto& w = world();
362 auto& muts = w.muts();
363 auto& vars = w.vars();
364
365 for (auto op : deps()) {
366 if constexpr (init) fvs = vars.merge(fvs, op->local_vars());
367
368 for (auto mut : op->local_muts()) {
369 if constexpr (init) mut->muts_ = muts.insert(mut->muts_, this); // register "this" as user of local_mut
370 fvs = vars.merge(fvs, mut->free_vars<init>(todo, run));
371 }
372 }
373
374 if (auto var = has_var()) fvs = vars.erase(fvs, var); // FV(λx.e) = FV(e) \ {x}
375
376 if constexpr (!init) todo |= fvs0 != fvs;
377
378 return vars_ = fvs;
379}
380
381void Def::invalidate() {
382 if (mark_ != 0) {
383 mark_ = 0;
384 // TODO optimize if vars empty?
385 for (auto mut : users())
386 mut->invalidate();
387 vars_ = Vars();
388 muts_ = Muts();
389 }
390}
391
392bool Def::is_closed() const {
393 if (local_vars().empty() && local_muts().empty()) return true;
394#ifdef MIM_ENABLE_CHECKS
395 assert(!is_external() || free_vars().empty());
396#endif
397 return free_vars().empty();
398}
399
400bool Def::is_open() const {
401 if (!local_vars().empty()) return true;
402 return !free_vars().empty();
403}
404
405/*
406 * Def - misc
407 */
408
409Sym Def::sym(const char* s) const { return world().sym(s); }
410Sym Def::sym(std::string_view s) const { return world().sym(s); }
411Sym Def::sym(std::string s) const { return world().sym(std::move(s)); }
412
413World& Def::world() const noexcept {
414 for (auto def = this;; def = def->type()) {
415 if (def->isa<Univ>()) return *def->world_;
416 if (auto type = def->isa<Type>()) return *type->level()->type()->as<Univ>()->world_;
417 }
418}
419
420const Def* Def::unfold_type() const {
421 if (!type_) {
422 auto& w = world();
423 if (auto t = isa<Type>()) return w.type(w.uinc(t->level()));
424 assert(isa<Univ>());
425 return nullptr;
426 }
427
428 return type_;
429}
430
431std::string_view Def::node_name() const {
432 switch (node()) {
433#define CODE(name, _) \
434 case Node::name: return #name;
436#undef CODE
437 default: fe::unreachable();
438 }
439}
440
441Defs Def::deps() const noexcept {
442 if (isa<Type>() || isa<Univ>()) return Defs();
443 assert(type());
444 return Defs(ops_ptr() - 1, (is_set() ? num_ops_ : 0) + 1);
445}
446
447u32 Def::judge() const noexcept {
448 switch (node()) {
449#define CODE(n, j) \
450 case Node::n: return u32(j);
452#undef CODE
453 default: fe::unreachable();
454 }
455}
456
457bool Def::is_term() const {
458 if (auto t = type()) {
459 if (auto u = t->type()) {
460 if (auto type = u->isa<Type>()) {
461 if (auto level = Lit::isa(type->level())) return *level == 0;
462 }
463 }
464 }
465 return false;
466}
467
468#ifndef NDEBUG
469const Def* Def::debug_prefix(std::string prefix) const { return dbg_.set(world().sym(prefix + sym().str())), this; }
470const Def* Def::debug_suffix(std::string suffix) const { return dbg_.set(world().sym(sym().str() + suffix)), this; }
471#endif
472
473/*
474 * cmp
475 */
476
477Def::Cmp Def::cmp(const Def* a, const Def* b) {
478 if (a == b) return Cmp::E;
479
480 if (a->isa_imm() && b->isa_mut()) return Cmp::L;
481 if (a->isa_mut() && b->isa_imm()) return Cmp::G;
482
483 // clang-format off
484 if (a->node() != b->node() ) return a->node() < b->node() ? Cmp::L : Cmp::G;
485 if (a->num_ops() != b->num_ops()) return a->num_ops() < b->num_ops() ? Cmp::L : Cmp::G;
486 if (a->flags() != b->flags() ) return a->flags() < b->flags() ? Cmp::L : Cmp::G;
487 // clang-format on
488
489 if (a->isa_mut() && b->isa_mut()) return Cmp::U;
490 assert(a->isa_imm() && b->isa_imm());
491
492 if (auto va = a->isa<Var>()) {
493 auto vb = b->as<Var>();
494 auto ma = va->mut();
495 auto mb = vb->mut();
496 if (ma->is_set() && ma->free_vars().contains(vb)) return Cmp::L;
497 if (mb->is_set() && mb->free_vars().contains(va)) return Cmp::G;
498 return Cmp::U;
499 }
500
501 // heuristic: iterate backwards as index (often a Lit) comes last and will faster find a solution
502 for (size_t i = a->num_ops(); i-- != 0;)
503 if (auto res = cmp(a->op(i), b->op(i)); res == Cmp::L || res == Cmp::G) return res;
504
505 return cmp(a->type(), b->type());
506}
507
508template<Def::Cmp c>
509bool Def::cmp_(const Def* a, const Def* b) {
510 auto res = cmp(a, b);
511 if (res == Cmp::U) {
512 a->world().WLOG("resorting to unstable gid-based compare for commute check");
513 return c == Cmp::L ? a->gid() < b->gid() : a->gid() > b->gid();
514 }
515 return res == c;
516}
517
518// clang-format off
519bool Def::less (const Def* a, const Def* b) { return cmp_<Cmp::L>(a, b); }
520bool Def::greater(const Def* a, const Def* b) { return cmp_<Cmp::G>(a, b); }
521// clang-format on
522
523const Def* Def::arity() const {
524 if (auto t = type(); t && !t->isa<Type>()) return t->arity();
525 return world().lit_nat_1();
526}
527
528bool Def::equal(const Def* other) const {
529 if (isa<Univ>() || this->isa_mut() || other->isa_mut()) return this == other;
530
531 bool result = this->node() == other->node() && this->flags() == other->flags()
532 && this->num_ops() == other->num_ops() && this->type() == other->type();
533
534 for (size_t i = 0, e = num_ops(); result && i != e; ++i)
535 result &= this->op(i) == other->op(i);
536
537 return result;
538}
539
540void Def::make_external() { return world().make_external(this); }
541void Def::make_internal() { return world().make_internal(this); }
542
543std::string Def::unique_name() const { return sym().str() + "_"s + std::to_string(gid()); }
544
545nat_t Def::num_projs() const { return Lit::isa(arity()).value_or(1); }
546
548 if (auto a = Lit::isa(arity()); a && *a < world().flags().scalarize_threshold) return *a;
549 return 1;
550}
551
552const Def* Def::proj(nat_t a, nat_t i) const {
553 World& w = world();
554
555 if (a == 1) {
556 assert(i == 0 && "only inhabitant of Idx 2 is 0_1");
557 if (!type()) return this;
558 if (!isa_mut<Sigma>() && !type()->isa_mut<Sigma>()) return this;
559 }
560
561 if (auto seq = isa<Seq>()) {
562 if (seq->has_var()) return seq->reduce(world().lit_idx(a, i));
563 return seq->body();
564 }
565
566 if (isa<Prod>()) return op(i);
567 if (w.is_frozen()) return nullptr;
568
569 return w.extract(this, a, i);
570}
571
572/*
573 * Idx
574 */
575
576const Def* Idx::isa(const Def* def) {
577 if (auto app = def->isa<App>()) {
578 if (app->callee()->isa<Idx>()) return app->arg();
579 }
580
581 return nullptr;
582}
583
584std::optional<nat_t> Idx::isa_lit(const Def* def) {
585 if (auto size = Idx::isa(def))
586 if (auto l = Lit::isa(size)) return l;
587 return {};
588}
589
590std::optional<nat_t> Idx::size2bitwidth(const Def* size) {
591 if (size->isa<Top>()) return 64;
592 if (auto s = Lit::isa(size)) return size2bitwidth(*s);
593 return {};
594}
595
596/*
597 * Global
598 */
599
600const App* Global::type() const { return Def::type()->as<App>(); }
601const Def* Global::alloced_type() const { return type()->arg(0); }
602
603} // namespace mim
friend class World
Definition lam.h:250
const Def * arg() const
Definition lam.h:233
const Def * arity() const final
Definition tuple.h:130
const Def * reduce(const Def *arg) const final
Definition tuple.h:147
friend class World
Definition tuple.h:164
const Def * immutabilize() final
Tries to make an immutable from a mutable.
Definition def.cpp:197
tag_t tag() const
Definition axm.h:55
u8 trip() const
Definition axm.h:38
friend class World
Definition axm.h:141
NormalizeFn normalizer() const
Definition axm.h:36
sub_t sub() const
Definition axm.h:56
plugin_t plugin() const
Definition axm.h:54
u8 curry() const
Definition axm.h:37
static bool alpha(const Def *d1, const Def *d2)
Definition check.h:96
Base class for all Defs.
Definition def.h:216
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:276
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:552
constexpr Node node() const noexcept
Definition def.h:239
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:244
void make_internal()
Definition def.cpp:541
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:454
Defs deps() const noexcept
Definition def.cpp:441
nat_t num_tprojs() const
As above but yields 1, if Flags::scalarize_threshold is exceeded.
Definition def.cpp:547
const Def * zonk() const
If Holes have been filled, reconstruct the program without them.
Definition check.cpp:60
World & world() const noexcept
Definition def.cpp:413
virtual const Def * check()
After all Def::ops have ben Def::set, this method will be invoked to check the type of this mutable.
Definition def.h:543
const Def * refine(size_t i, const Def *new_op) const
Definition def.cpp:226
Def * set_type(const Def *)
Update type.
Definition def.cpp:260
std::string_view node_name() const
Definition def.cpp:431
constexpr auto ops() const noexcept
Definition def.h:270
Vars local_vars() const
Vars reachable by following immutable deps().
Definition def.cpp:322
void make_external()
Definition def.cpp:540
constexpr flags_t flags() const noexcept
Definition def.h:234
Dbg dbg_
Definition def.h:616
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:445
auto vars() noexcept
Definition def.h:394
bool is_term() const
Definition def.cpp:457
const Def * debug_prefix(std::string) const
Definition def.cpp:469
const Def * op(size_t i) const noexcept
Definition def.h:273
bool is_immutabilizable()
Definition def.cpp:173
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:394
bool is_external() const
Definition def.h:429
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
Definition def.cpp:420
bool is_open() const
Has free_vars()?
Definition def.cpp:400
friend class World
Definition def.h:646
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
Definition def.cpp:306
const Def * debug_suffix(std::string) const
Definition def.cpp:470
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:260
auto vars(F f) noexcept
Definition def.h:394
const Def * rebuild(World &w, const Def *type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
Definition def.h:508
static bool less(const Def *a, const Def *b)
Definition def.cpp:519
static bool greater(const Def *a, const Def *b)
Definition def.cpp:520
const Def * var()
Not necessarily a Var: E.g., if the return type is [], this will yield ().
Definition def.cpp:288
static Cmp cmp(const Def *a, const Def *b)
Definition def.cpp:477
virtual constexpr size_t reduction_offset() const noexcept
First Def::op that needs to be dealt with during reduction; e.g.
Definition def.h:529
Sym sym() const
Definition def.h:467
nat_t num_projs() const
Yields Def::arity(), if it is a Lit, or 1 otherwise.
Definition def.cpp:545
flags_t flags_
Definition def.h:623
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:235
virtual const Def * arity() const
Definition def.cpp:523
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
Definition def.cpp:267
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:543
const T * isa_imm() const
Definition def.h:439
Muts users()
Set of mutables where this mutable is locally referenced.
Definition def.h:422
bool is_closed() const
Has no free_vars()?
Definition def.cpp:392
Vars free_vars() const
Compute a global solution by transitively following mutables as well.
Definition def.cpp:311
Dbg dbg() const
Definition def.h:465
u32 judge() const noexcept
Definition def.cpp:447
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:398
constexpr size_t num_ops() const noexcept
Definition def.h:274
@ E
Equal.
Definition def.h:571
@ U
Unknown.
Definition def.h:572
@ L
Less.
Definition def.h:569
@ G
Greater.
Definition def.h:570
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:116
friend class World
Definition tuple.h:230
const Def * alloced_type() const
Definition def.cpp:601
friend class World
Definition def.h:917
bool is_mutable() const
Definition def.h:902
const App * type() const
Definition def.cpp:600
Global * stub_(World &, const Def *) final
Definition def.cpp:149
This node is a hole in the IR that is inferred by its context later on.
Definition check.h:14
friend class World
Definition check.h:75
static constexpr nat_t size2bitwidth(nat_t n)
Definition def.h:840
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
Definition def.cpp:576
friend class World
Definition def.h:851
static std::optional< nat_t > isa_lit(const Def *def)
Definition def.cpp:584
friend class World
Definition lattice.h:89
friend class World
Definition tuple.h:258
friend class World
Definition lam.h:198
static std::optional< T > isa(const Def *def)
Definition def.h:773
friend class World
Definition def.h:790
friend class World
Definition lattice.h:140
friend class World
Definition lattice.h:65
friend class World
Definition def.h:804
const Def * immutabilize() final
Tries to make an immutable from a mutable.
Definition def.cpp:207
const Def * arity() const final
Definition tuple.cpp:46
friend class World
Definition tuple.h:206
const Def * reduce(const Def *arg) const final
Definition tuple.h:195
A dependent function type.
Definition lam.h:11
Pi(const Def *type, const Def *dom, const Def *codom, bool implicit)
Constructor for an immutable Pi.
Definition lam.h:14
bool is_implicit() const
Definition lam.h:23
const Def * dom() const
Definition lam.h:32
friend class World
Definition lam.h:102
const Def * codom() const
Definition lam.h:33
const Pi * immutabilize() final
Tries to make an immutable from a mutable.
Definition def.cpp:187
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Definition def.cpp:23
friend class World
Definition def.h:874
u32 pass() const
IPass::index within PassMan.
Definition def.h:864
u32 tag() const
Definition def.h:865
const Def * body() const
Definition tuple.h:91
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Definition def.cpp:23
constexpr bool empty() const noexcept
Is empty?
Definition sets.h:240
bool contains(D *d) const noexcept
Is ?.
Definition sets.h:252
void dot()
Definition sets.h:562
const TExt< Up > * set(Loc l) const
Definition def.h:168
friend class World
Definition tuple.h:63
const Def * immutabilize() final
Tries to make an immutable from a mutable.
Definition def.cpp:192
friend class World
Definition lattice.h:112
friend class World
Definition lattice.h:48
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:142
Extremum. Either Top (Up) or Bottom.
Definition lattice.h:152
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:141
friend class World
Definition lattice.h:166
friend class World
Definition tuple.h:80
friend class World
Definition def.h:746
friend class World
Definition def.h:724
level_t offset() const
Definition def.h:715
friend class World
Definition def.h:701
friend class World
Definition lattice.h:199
friend class World
Definition def.h:685
friend class World
Definition def.h:670
Def * mut() const
Definition def.h:661
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:34
void make_external(Def *)
Definition world.cpp:87
const Def * sigma(Defs ops)
Definition world.cpp:277
const Pi * pi(const Def *dom, const Def *codom, bool implicit=false)
Definition world.h:260
Flags & flags()
Retrieve compile Flags.
Definition world.cpp:71
u32 next_gid()
Definition world.h:92
Sym sym(std::string_view)
Definition world.cpp:74
void make_internal(Def *)
Definition world.cpp:94
auto & vars()
Definition world.h:533
Defs reduce(const Var *var, const Def *arg)
Yields the new body of [mut->var() -> arg]mut.
Definition world.cpp:655
const Lit * lit_nat_1()
Definition world.h:410
#define MIM_NODE(m)
Definition def.h:18
Definition ast.h:14
View< const Def * > Defs
Definition def.h:51
u64 nat_t
Definition types.h:43
Vector< const Def * > DefVec
Definition def.h:52
Dep
Tracks a dependency to certain Defs transitively through the Def::deps() up to but excliding mutables...
Definition def.h:98
@ None
Definition def.h:99
@ Mut
Definition def.h:100
u64 flags_t
Definition types.h:45
uint64_t scalarize_threshold
Definition flags.h:13
u8 node_t
Definition types.h:44
constexpr size_t hash_combine(size_t seed, T v) noexcept
Definition hash.h:62
Sets< Def >::Set Muts
Definition def.h:62
TExt< true > Top
Definition lattice.h:172
constexpr decltype(auto) get(Span< T, N > span) noexcept
Definition span.h:115
consteval size_t hash_begin() noexcept
Definition hash.h:75
constexpr size_t hash(size_t h) noexcept
Definition hash.h:32
Sets< const Var >::Set Vars
Definition def.h:72
uint32_t u32
Definition types.h:34
uint8_t u8
Definition types.h:34
Node
Definition def.h:87
@ Univ
Definition def.h:89
CODE(node, _)
Definition def.h:88
@ Var
Definition def.h:89