MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <
mim/axm.h
>
4
#include <
mim/world.h
>
5
6
#include "
mim/plug/core/autogen.h
"
7
8
namespace
mim::plug::core
{
9
10
constexpr
flags_t
icmp_mask
= 0b00011111;
// low 5 bits encode X Y G L E as bits 4..0
11
12
/// @name Mode
13
///@{
14
/// What should happen if Idx arithmetic overflows?
15
enum class
Mode
:
nat_t
{
16
none = 0,
///< Wrap around.
17
nsw
= 1 << 0,
///< No Signed Wrap around.
18
nuw
= 1 << 1,
///< No Unsigned Wrap around.
19
nsuw
=
nsw
|
nuw
,
20
};
21
22
/// Give Mode as mim::plug::math::Mode, mim::nat_t or const Def*.
23
using
VMode
= std::variant<Mode, nat_t, const Def*>;
24
25
/// mim::plug::core::VMode -> const Def*.
26
inline
const
Def
*
mode
(
World
& w,
VMode
m) {
27
if
(
auto
def = std::get_if<const Def*>(&m))
return
*def;
28
if
(
auto
nat
= std::get_if<nat_t>(&m))
return
w.lit_nat(*
nat
);
29
return
w.lit_nat(std::to_underlying(std::get<Mode>(m)));
30
}
31
///@}
32
33
/// @name core.trait
34
///@{
35
inline
const
Def
*
op
(
trait
o,
const
Def
* type) {
36
World
& w = type->world();
37
return
w.app(w.annex(o), type);
38
}
39
///@}
40
41
/// @name core.pe
42
///@{
43
inline
const
Def
*
op
(
pe
o,
const
Def
* def) {
44
World
& w = def->
world
();
45
return
w.app(w.app(w.annex(o), def->
type
()), def);
46
}
47
///@}
48
49
/// @name core.bit2
50
///@{
51
/// Use like this: `a op b = tab[a][b]`
52
constexpr
std::array<std::array<u64, 2>, 2>
make_truth_table
(
bit2
id
) {
53
return
{
54
{{
sub_t
(
id
) &
sub_t
(0b0001) ?
u64
(-1) : 0,
sub_t
(
id
) &
sub_t
(0b0100) ?
u64
(-1) : 0},
55
{
sub_t
(
id
) &
sub_t
(0b0010) ?
u64
(-1) : 0,
sub_t
(
id
) &
sub_t
(0b1000) ?
u64
(-1) : 0}}
56
};
57
}
58
///@}
59
60
/// @name extract_unsafe
61
///@{
62
inline
const
Def
*
extract_unsafe
(
const
Def
* d,
const
Def
* i) {
63
World
& w = d->world();
64
return
w.extract(d, w.call(
conv::u
, d->unfold_type()->arity(), i));
65
}
66
inline
const
Def
*
extract_unsafe
(
const
Def
* d,
u64
i) {
67
World
& w = d->world();
68
return
extract_unsafe
(d, w.lit_idx(0_u64, i));
69
}
70
///@}
71
72
/// @name insert_unsafe
73
///@{
74
inline
const
Def
*
insert_unsafe
(
const
Def
* d,
const
Def
* i,
const
Def
* val) {
75
World
& w = d->world();
76
return
w.insert(d, w.call(
conv::u
, d->unfold_type()->arity(), i), val);
77
}
78
inline
const
Def
*
insert_unsafe
(
const
Def
* d,
u64
i,
const
Def
* val) {
79
World
& w = d->world();
80
return
insert_unsafe
(d, w.lit_idx(0_u64, i), val);
81
}
82
///@}
83
84
/// @name Convert TBound to Sigma
85
/// This is WIP.
86
///@{
87
template
<
bool
up>
88
const
Sigma
*
convert
(
const
TBound<up>
* b);
89
inline
const
Sigma
*
convert
(
const
Bound
* b) {
return
b->isa<
Join
>() ?
convert
(b->as<
Join
>()) :
convert
(b->as<
Meet
>()); }
90
///@}
91
92
}
// namespace mim::plug::core
93
94
namespace
mim
{
95
96
/// @name is_commutative/is_associative
97
///@{
98
// clang-format off
99
constexpr
bool
is_commutative
(
plug::core::nat
id
) {
return
id
== plug::core::nat ::add ||
id
== plug::core::nat ::mul; }
100
constexpr
bool
is_commutative
(
plug::core::ncmp
id
) {
return
id
== plug::core::ncmp:: e ||
id
==
plug::core::ncmp:: ne
; }
101
constexpr
bool
is_commutative
(
plug::core::wrap
id
) {
return
id
==
plug::core::wrap::add
||
id
==
plug::core::wrap::mul
; }
102
constexpr
bool
is_commutative
(
plug::core::icmp
id
) {
return
id
== plug::core::icmp:: e ||
id
==
plug::core::icmp:: ne
; }
103
// clang-format off
104
105
constexpr
bool
is_commutative
(
plug::core::bit2
id
) {
106
auto
tab = make_truth_table(
id
);
107
return
tab[0][1] == tab[1][0];
108
}
109
110
constexpr
bool
is_associative
(
plug::core::bit2
id
) {
111
switch
(
id
) {
112
case
plug::core::bit2::t
:
113
case
plug::core::bit2::xor_
:
114
case
plug::core::bit2::and_
:
115
case
plug::core::bit2::nxor
:
116
case
plug::core::bit2::fst
:
117
case
plug::core::bit2::snd
:
118
case
plug::core::bit2::or_
:
119
case
plug::core::bit2::f
:
return
true
;
120
default
:
return
false
;
121
}
122
}
123
124
// clang-format off
125
constexpr
bool
is_associative
(
plug::core::nat
id
) {
return
is_commutative
(
id
); }
126
constexpr
bool
is_associative
(
plug::core::ncmp
id
) {
return
is_commutative
(
id
); }
127
constexpr
bool
is_associative
(
plug::core::icmp
id
) {
return
is_commutative
(
id
); }
128
constexpr
bool
is_associative
(
plug::core::wrap
id
) {
return
is_commutative
(
id
); }
129
// clang-format on
130
///@}
131
132
}
// namespace mim
133
134
#ifndef DOXYGEN
135
template
<>
136
struct
fe::is_bit_enum<
mim
::plug::core::Mode> : std::true_type {};
137
#endif
axm.h
mim::Bound
Common base for TBound.
Definition
lattice.h:13
mim::Def
Base class for all Defs.
Definition
def.h:273
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::Sigma
A dependent tuple type.
Definition
tuple.h:23
mim::TBound
Specific Bound depending on Up.
Definition
lattice.h:35
mim::World
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition
world.h:40
autogen.h
mim::plug::core
The core Plugin
Definition
core.h:8
mim::plug::core::extract_unsafe
const Def * extract_unsafe(const Def *d, const Def *i)
Definition
core.h:62
mim::plug::core::wrap
wrap
Definition
autogen.h:137
mim::plug::core::wrap::add
@ add
Definition
autogen.h:138
mim::plug::core::wrap::mul
@ mul
Definition
autogen.h:140
mim::plug::core::convert
const Sigma * convert(const TBound< up > *b)
Definition
core.cpp:16
mim::plug::core::mode
mode
Definition
autogen.h:55
mim::plug::core::nat
nat
Definition
autogen.h:14
mim::plug::core::VMode
std::variant< Mode, nat_t, const Def * > VMode
Give Mode as mim::plug::math::Mode, mim::nat_t or const Def*.
Definition
core.h:23
mim::plug::core::conv::u
@ u
Definition
autogen.h:254
mim::plug::core::ncmp
ncmp
Definition
autogen.h:29
mim::plug::core::ncmp::ne
@ ne
Definition
autogen.h:43
mim::plug::core::bit2
bit2
Definition
autogen.h:99
mim::plug::core::bit2::fst
@ fst
Definition
autogen.h:112
mim::plug::core::bit2::nxor
@ nxor
Definition
autogen.h:109
mim::plug::core::bit2::xor_
@ xor_
Definition
autogen.h:106
mim::plug::core::bit2::snd
@ snd
Definition
autogen.h:110
mim::plug::core::bit2::f
@ f
Definition
autogen.h:100
mim::plug::core::bit2::and_
@ and_
Definition
autogen.h:108
mim::plug::core::bit2::or_
@ or_
Definition
autogen.h:114
mim::plug::core::bit2::t
@ t
Definition
autogen.h:115
mim::plug::core::trait
trait
Definition
autogen.h:272
mim::plug::core::icmp
icmp
Definition
autogen.h:172
mim::plug::core::icmp::ne
@ ne
Definition
autogen.h:214
mim::plug::core::op
const Def * op(trait o, const Def *type)
Definition
core.h:35
mim::plug::core::pe
pe
Definition
autogen.h:284
mim::plug::core::make_truth_table
constexpr std::array< std::array< u64, 2 >, 2 > make_truth_table(bit2 id)
Definition
core.h:52
mim::plug::core::insert_unsafe
const Def * insert_unsafe(const Def *d, const Def *i, const Def *val)
Definition
core.h:74
mim::plug::core::icmp_mask
constexpr flags_t icmp_mask
Definition
core.h:10
mim::plug::core::Mode
Mode
Definition
core.h:15
mim::plug::core::Mode::nuw
@ nuw
No Unsigned Wrap around.
Definition
core.h:18
mim::plug::core::Mode::nsuw
@ nsuw
Definition
core.h:19
mim::plug::core::Mode::nsw
@ nsw
No Signed Wrap around.
Definition
core.h:17
mim
Definition
ast.h:16
mim::nat_t
u64 nat_t
Definition
types.h:37
mim::sub_t
u8 sub_t
Definition
types.h:42
mim::flags_t
u64 flags_t
Definition
types.h:39
mim::Join
TBound< true > Join
AKA union.
Definition
lattice.h:167
mim::is_commutative
constexpr bool is_commutative(Id)
Definition
axm.h:164
mim::is_associative
constexpr bool is_associative(Id id)
Definition
axm.h:170
mim::u64
uint64_t u64
Definition
types.h:27
mim::Meet
TBound< false > Meet
AKA intersection.
Definition
lattice.h:166
world.h
include
mim
plug
core
core.h
Generated by
1.18.0