MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
tuple.cpp
Go to the documentation of this file.
1#include "mim/tuple.h"
2
3#include <cassert>
4
5#include "mim/tuple.h"
6#include "mim/world.h"
7
8namespace mim {
9
10Prod::~Prod() = default;
11Seq::~Seq() = default;
12
13const Def* Sigma::arity() const {
14 auto n = num_ops();
15 if (n != 1 || isa_mut()) return world().lit_nat(n);
16 return op(0)->arity();
17}
18
19const Def* Pack::arity() const {
20 if (auto arr = type()->isa<Arr>()) return arr->arity();
21 if (type() == world().sigma()) return world().lit_nat_0();
22 return world().lit_nat_1();
23}
24
25Select::Select(const Def* def) {
26 if (def) {
27 if (auto extract = def->isa<Extract>(); extract && !Lit::isa(extract->index())) {
28 if (auto a = Lit::isa(extract->tuple()->arity()); a && a == 2) extract_ = extract;
29 }
30 }
31}
32
34 : Select(def->isa<App>() ? def->as<App>()->callee() : nullptr) {
35 if (extract()) app_ = def->as<App>();
36}
37
38const Def* Branch::callee() const { return app()->callee(); }
39const Def* Branch::arg() const { return app()->arg(); }
40
42 if (auto app = def->isa<App>()) {
43 if (auto extract = app->callee()->isa<Extract>(); extract && !Lit::isa(extract->index())) {
44 if (auto a = Lit::isa(extract->tuple()->arity())) {
45 app_ = app;
46 extract_ = extract;
47 }
48 }
49 }
50}
51
52const Def* Dispatch::callee() const { return app()->callee(); }
53const Def* Dispatch::arg() const { return app()->arg(); }
54
55bool is_unit(const Def* def) { return def->type() == def->world().sigma(); }
56
57std::string tuple2str(const Def* def) {
58 if (def == nullptr) return {};
59
60 auto& w = def->world();
61 auto res = std::string();
62 if (auto n = Lit::isa(def->arity())) {
63 for (size_t i = 0; i != *n; ++i) {
64 auto elem = def->proj(*n, i);
65 if (elem->type() == w.type_i8()) {
66 if (auto l = Lit::isa<char>(elem)) {
67 res.push_back(*l);
68 continue;
69 }
70 }
71 return {};
72 }
73 }
74 return res;
75}
76
77/*
78 * cat
79 */
80
82 auto res = DefVec();
83 res.reserve(a.size() + b.size());
84 res.insert(res.end(), a.begin(), a.end());
85 res.insert(res.end(), b.begin(), b.end());
86 return res;
87}
88
89DefVec cat(nat_t n, nat_t m, const Def* a, const Def* b) {
90 auto defs = DefVec();
91 defs.reserve(n + m);
92 for (size_t i = 0, e = n; i != e; ++i)
93 defs.emplace_back(a->proj(e, i));
94 for (size_t i = 0, e = m; i != e; ++i)
95 defs.emplace_back(b->proj(e, i));
96
97 return defs;
98}
99
100const Def* cat_tuple(nat_t n, nat_t m, const Def* a, const Def* b) { return a->world().tuple(cat(n, m, a, b)); }
101const Def* cat_sigma(nat_t n, nat_t m, const Def* a, const Def* b) { return a->world().sigma(cat(n, m, a, b)); }
102
103const Def* cat_tuple(World& world, Defs a, Defs b) { return world.tuple(cat(a, b)); }
104const Def* cat_sigma(World& world, Defs a, Defs b) { return world.sigma(cat(a, b)); }
105
106const Def* tuple_of_types(const Def* t) {
107 auto& world = t->world();
108 if (auto sigma = t->isa<Sigma>()) return world.tuple(sigma->ops());
109 if (auto arr = t->isa<Arr>()) return world.pack(arr->arity(), arr->body());
110 return t;
111}
112
113} // namespace mim
const Def * callee() const
Definition lam.h:276
const Def * arg() const
Definition lam.h:285
A (possibly paramterized) Array.
Definition tuple.h:121
const App * app() const
Definition tuple.h:289
const Def * callee() const
Definition tuple.cpp:38
Branch(const Def *)
Definition tuple.cpp:33
const Def * arg() const
Definition tuple.cpp:39
Base class for all Defs.
Definition def.h:261
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:635
World & world() const noexcept
Definition def.cpp:483
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:527
const Def * op(size_t i) const noexcept
Definition def.h:320
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:491
virtual const Def * arity() const
Number of elements available to Extract / Insert (may be dynamic).
Definition def.cpp:597
constexpr size_t num_ops() const noexcept
Definition def.h:321
const Def * callee() const
Definition tuple.cpp:52
const Def * arg() const
Definition tuple.cpp:53
Dispatch(const Def *)
Definition tuple.cpp:41
const Extract * extract() const
Definition tuple.h:318
const App * app() const
Definition tuple.h:314
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:210
static std::optional< T > isa(const Def *def)
Definition def.h:878
const Def * arity() const final
Number of elements available to Extract / Insert (may be dynamic).
Definition tuple.cpp:19
~Prod() override
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Definition def.cpp:24
const Extract * extract() const
Definition tuple.h:271
Select(const Def *)
Definition tuple.cpp:25
~Seq() override
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Definition def.cpp:24
A dependent tuple type.
Definition tuple.h:22
const Def * arity() const final
Number of elements available to Extract / Insert (may be dynamic).
Definition tuple.cpp:13
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
const Def * sigma(Defs ops)
Definition world.cpp:298
const Def * tuple(Defs ops)
Definition world.cpp:308
const Lit * lit_nat_0()
Definition world.h:523
const Lit * lit_nat(nat_t a)
Definition world.h:522
const Lit * lit_nat_1()
Definition world.h:524
Definition ast.h:14
View< const Def * > Defs
Definition def.h:78
u64 nat_t
Definition types.h:37
Vector< const Def * > DefVec
Definition def.h:79
const Def * cat_tuple(nat_t n, nat_t m, const Def *a, const Def *b)
Definition tuple.cpp:100
bool is_unit(const Def *)
Definition tuple.cpp:55
std::string tuple2str(const Def *)
Definition tuple.cpp:57
const Def * cat_sigma(nat_t n, nat_t m, const Def *a, const Def *b)
Definition tuple.cpp:101
const Def * tuple_of_types(const Def *t)
Definition tuple.cpp:106
DefVec cat(Defs, Defs)
Definition tuple.cpp:81