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/world.h"
6
7namespace mim {
8
9namespace {
10/// The App::callee of @p def - or `nullptr`, if @p def isn't an App at all.
11const Def* callee_of(const Def* def) {
12 auto app = def->isa<App>();
13 return app ? app->callee() : nullptr;
14}
15} // namespace
16
17Select::Select(const Def* def) {
18 if (!def) return;
19 auto extract = def->isa<Extract>();
20 if (!extract || Lit::isa(extract->index())) return;
21 if (auto a = Lit::isa(extract->tuple()->arity()); a && *a == 2) extract_ = extract;
22}
23
25 : Select(callee_of(def)) {
26 if (extract()) app_ = def->as<App>();
27}
28
29const Def* Branch::callee() const { return app()->callee(); }
30const Def* Branch::arg() const { return app()->arg(); }
31
33 auto app = def->isa<App>();
34 if (!app) return;
35 auto extract = app->callee()->isa<Extract>();
36 if (!extract || Lit::isa(extract->index())) return;
37 if (Lit::isa(extract->tuple()->arity())) {
38 app_ = app;
39 extract_ = extract;
40 }
41}
42
43const Def* Dispatch::callee() const { return app()->callee(); }
44const Def* Dispatch::arg() const { return app()->arg(); }
45
46bool is_unit(const Def* def) { return def->type() == def->world().sigma(); }
47
48std::string tuple2str(const Def* def) {
49 if (def == nullptr) return {};
50
51 auto& w = def->world();
52 auto res = std::string();
53 if (auto n = Lit::isa(def->arity())) {
54 res.reserve(*n);
55 for (size_t i = 0; i != *n; ++i) {
56 auto elem = def->proj(*n, i);
57 if (elem->type() == w.type_i8()) {
58 if (auto l = Lit::isa<char>(elem)) {
59 res.push_back(*l);
60 continue;
61 }
62 }
63 return {};
64 }
65 }
66 return res;
67}
68
69/*
70 * cat
71 */
72
74 auto res = DefVec();
75 res.reserve(a.size() + b.size());
76 res.append_range(a);
77 res.append_range(b);
78 return res;
79}
80
81DefVec cat(nat_t n, nat_t m, const Def* a, const Def* b) {
82 auto res = DefVec();
83 res.reserve(n + m);
84 for (nat_t i = 0; i != n; ++i)
85 res.emplace_back(a->proj(n, i));
86 for (nat_t i = 0; i != m; ++i)
87 res.emplace_back(b->proj(m, i));
88
89 return res;
90}
91
92const Def* cat_tuple(nat_t n, nat_t m, const Def* a, const Def* b) { return a->world().tuple(cat(n, m, a, b)); }
93const Def* cat_sigma(nat_t n, nat_t m, const Def* a, const Def* b) { return a->world().sigma(cat(n, m, a, b)); }
94
95const Def* cat_tuple(World& world, Defs a, Defs b) { return world.tuple(cat(a, b)); }
96const Def* cat_sigma(World& world, Defs a, Defs b) { return world.sigma(cat(a, b)); }
97
98const Def* tuple_of_types(const Def* t) {
99 auto& world = t->world();
100 if (auto sigma = t->isa<Sigma>()) return world.tuple(sigma->ops());
101 if (auto arr = t->isa<Arr>()) return world.pack(arr->arity(), arr->body());
102 return t;
103}
104
105} // namespace mim
const Def * callee() const
Definition lam.h:275
const Def * arg() const
Definition lam.h:284
A (possibly paramterized) Array.
Definition tuple.h:110
const App * app() const
Definition tuple.h:236
const Def * callee() const
Definition tuple.cpp:29
Branch(const Def *)
Definition tuple.cpp:24
const Def * arg() const
Definition tuple.cpp:30
Base class for all Defs.
Definition def.h:273
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:623
World & world() const noexcept
Definition def.h:1097
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1111
const Def * arity() const
Number of elements available to Extract / Insert (may be dynamic).
Definition def.cpp:592
const Def * callee() const
Definition tuple.cpp:43
const Def * arg() const
Definition tuple.cpp:44
Dispatch(const Def *)
Definition tuple.cpp:32
const Extract * extract() const
Definition tuple.h:265
const App * app() const
Definition tuple.h:261
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:161
const Def * tuple() const
Definition tuple.h:171
const Def * index() const
Definition tuple.h:172
static std::optional< T > isa(const Def *def)
Definition def.h:937
const Extract * extract() const
Definition tuple.h:218
Select(const Def *)
Definition tuple.cpp:17
A dependent tuple type.
Definition tuple.h:23
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:40
const Def * sigma(Defs ops)
Definition world.cpp:316
const Def * tuple(Defs ops)
Definition world.cpp:326
Definition ast.h:16
u64 nat_t
Definition types.h:37
const Def * cat_tuple(nat_t n, nat_t m, const Def *a, const Def *b)
Definition tuple.cpp:92
bool is_unit(const Def *)
Definition tuple.cpp:46
fe::View< const Def * > Defs
Definition def.h:91
std::string tuple2str(const Def *)
Definition tuple.cpp:48
const Def * cat_sigma(nat_t n, nat_t m, const Def *a, const Def *b)
Definition tuple.cpp:93
const Def * tuple_of_types(const Def *t)
Definition tuple.cpp:98
fe::Vector< const Def * > DefVec
Definition def.h:93
DefVec cat(Defs, Defs)
Definition tuple.cpp:73