MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
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
7
namespace
mim
{
8
9
namespace
{
10
/// The App::callee of @p def - or `nullptr`, if @p def isn't an App at all.
11
const
Def
* callee_of(
const
Def
* def) {
12
auto
app = def->isa<
App
>();
13
return
app ? app->
callee
() :
nullptr
;
14
}
15
}
// namespace
16
17
Select::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
24
Branch::Branch
(
const
Def
* def)
25
:
Select
(callee_of(def)) {
26
if
(
extract
()) app_ = def->as<
App
>();
27
}
28
29
const
Def
*
Branch::callee
()
const
{
return
app
()->
callee
(); }
30
const
Def
*
Branch::arg
()
const
{
return
app
()->
arg
(); }
31
32
Dispatch::Dispatch
(
const
Def
* def) {
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
43
const
Def
*
Dispatch::callee
()
const
{
return
app
()->
callee
(); }
44
const
Def
*
Dispatch::arg
()
const
{
return
app
()->
arg
(); }
45
46
bool
is_unit
(
const
Def
* def) {
return
def->
type
() == def->
world
().
sigma
(); }
47
48
std::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
73
DefVec
cat
(
Defs
a,
Defs
b) {
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
81
DefVec
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
92
const
Def
*
cat_tuple
(
nat_t
n,
nat_t
m,
const
Def
* a,
const
Def
* b) {
return
a->world().tuple(
cat
(n, m, a, b)); }
93
const
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
95
const
Def
*
cat_tuple
(
World
& world,
Defs
a,
Defs
b) {
return
world.
tuple
(
cat
(a, b)); }
96
const
Def
*
cat_sigma
(
World
& world,
Defs
a,
Defs
b) {
return
world.
sigma
(
cat
(a, b)); }
97
98
const
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
mim::App
Definition
lam.h:224
mim::App::callee
const Def * callee() const
Definition
lam.h:275
mim::App::arg
const Def * arg() const
Definition
lam.h:284
mim::Arr
A (possibly paramterized) Array.
Definition
tuple.h:110
mim::Branch::app
const App * app() const
Definition
tuple.h:236
mim::Branch::callee
const Def * callee() const
Definition
tuple.cpp:29
mim::Branch::Branch
Branch(const Def *)
Definition
tuple.cpp:24
mim::Branch::arg
const Def * arg() const
Definition
tuple.cpp:30
mim::Def
Base class for all Defs.
Definition
def.h:273
mim::Def::proj
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
mim::Def::world
World & world() const noexcept
Definition
def.h:1097
mim::Def::type
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition
def.h:1111
mim::Def::arity
const Def * arity() const
Number of elements available to Extract / Insert (may be dynamic).
Definition
def.cpp:592
mim::Dispatch::callee
const Def * callee() const
Definition
tuple.cpp:43
mim::Dispatch::arg
const Def * arg() const
Definition
tuple.cpp:44
mim::Dispatch::Dispatch
Dispatch(const Def *)
Definition
tuple.cpp:32
mim::Dispatch::extract
const Extract * extract() const
Definition
tuple.h:265
mim::Dispatch::app
const App * app() const
Definition
tuple.h:261
mim::Extract
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition
tuple.h:161
mim::Extract::tuple
const Def * tuple() const
Definition
tuple.h:171
mim::Extract::index
const Def * index() const
Definition
tuple.h:172
mim::Lit::isa
static std::optional< T > isa(const Def *def)
Definition
def.h:937
mim::Select::extract
const Extract * extract() const
Definition
tuple.h:218
mim::Select::Select
Select(const Def *)
Definition
tuple.cpp:17
mim::Sigma
A dependent tuple type.
Definition
tuple.h:23
mim::World
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition
world.h:40
mim::World::sigma
const Def * sigma(Defs ops)
Definition
world.cpp:316
mim::World::tuple
const Def * tuple(Defs ops)
Definition
world.cpp:326
mim
Definition
ast.h:16
mim::nat_t
u64 nat_t
Definition
types.h:37
mim::cat_tuple
const Def * cat_tuple(nat_t n, nat_t m, const Def *a, const Def *b)
Definition
tuple.cpp:92
mim::is_unit
bool is_unit(const Def *)
Definition
tuple.cpp:46
mim::Defs
fe::View< const Def * > Defs
Definition
def.h:91
mim::tuple2str
std::string tuple2str(const Def *)
Definition
tuple.cpp:48
mim::cat_sigma
const Def * cat_sigma(nat_t n, nat_t m, const Def *a, const Def *b)
Definition
tuple.cpp:93
mim::tuple_of_types
const Def * tuple_of_types(const Def *t)
Definition
tuple.cpp:98
mim::DefVec
fe::Vector< const Def * > DefVec
Definition
def.h:93
mim::cat
DefVec cat(Defs, Defs)
Definition
tuple.cpp:73
tuple.h
world.h
src
mim
tuple.cpp
Generated by
1.18.0