MimIR
0.5-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::core::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
(0b0010) ?
u64
(-1) : 0},
55
{
sub_t
(
id
) &
sub_t
(0b0100) ?
u64
(-1) : 0,
sub_t
(
id
) &
sub_t
(0b1000) ?
u64
(-1) : 0}}
56
};
57
}
58
///@}
59
60
}
// namespace mim::plug::core
61
62
namespace
mim
{
63
64
/// @name is_commutative/is_associative
65
///@{
66
// clang-format off
67
constexpr
bool
is_commutative
(
plug::core::nat
id
) {
return
id
== plug::core::nat ::add ||
id
== plug::core::nat ::mul; }
68
constexpr
bool
is_commutative
(
plug::core::ncmp
id
) {
return
id
== plug::core::ncmp:: e ||
id
==
plug::core::ncmp:: ne
; }
69
constexpr
bool
is_commutative
(
plug::core::wrap
id
) {
return
id
==
plug::core::wrap::add
||
id
==
plug::core::wrap::mul
; }
70
constexpr
bool
is_commutative
(
plug::core::icmp
id
) {
return
id
== plug::core::icmp:: e ||
id
==
plug::core::icmp:: ne
; }
71
// clang-format on
72
73
constexpr
bool
is_commutative
(
plug::core::bit2
id
) {
74
auto
tab = make_truth_table(
id
);
75
return
tab[0][1] == tab[1][0];
76
}
77
78
constexpr
bool
is_associative
(
plug::core::bit2
id
) {
79
switch
(
id
) {
80
case
plug::core::bit2::t
:
81
case
plug::core::bit2::xor_
:
82
case
plug::core::bit2::and_
:
83
case
plug::core::bit2::nxor
:
84
case
plug::core::bit2::fst
:
85
case
plug::core::bit2::snd
:
86
case
plug::core::bit2::or_
:
87
case
plug::core::bit2::f
:
return
true
;
88
default
:
return
false
;
89
}
90
}
91
92
// clang-format off
93
constexpr
bool
is_associative
(
plug::core::nat
id
) {
return
is_commutative
(
id
); }
94
constexpr
bool
is_associative
(
plug::core::ncmp
id
) {
return
is_commutative
(
id
); }
95
constexpr
bool
is_associative
(
plug::core::icmp
id
) {
return
is_commutative
(
id
); }
96
constexpr
bool
is_associative
(
plug::core::wrap
id
) {
return
is_commutative
(
id
); }
97
// clang-format on
98
///@}
99
100
}
// namespace mim
101
102
#ifndef DOXYGEN
103
template
<>
104
struct
fe::is_bit_enum<
mim
::plug::core::Mode> : std::true_type {};
105
#endif
axm.h
mim::Def
Base class for all Defs.
Definition
def.h:289
mim::Def::world
World & world() const noexcept
Definition
def.h:1105
mim::Def::type
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition
def.h:1119
mim::World
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition
world.h:43
autogen.h
mim::plug::core
The core Plugin
Definition
core.h:8
mim::plug::core::wrap
wrap
Definition
autogen.h:143
mim::plug::core::wrap::add
@ add
Definition
autogen.h:144
mim::plug::core::wrap::mul
@ mul
Definition
autogen.h:146
mim::plug::core::mode
mode
Definition
autogen.h:69
mim::plug::core::nat
nat
Definition
autogen.h:28
mim::plug::core::VMode
std::variant< Mode, nat_t, const Def * > VMode
Give Mode as mim::plug::core::Mode, mim::nat_t or const Def*.
Definition
core.h:23
mim::plug::core::ncmp
ncmp
Definition
autogen.h:43
mim::plug::core::ncmp::ne
@ ne
Definition
autogen.h:57
mim::plug::core::bit2
bit2
Definition
autogen.h:105
mim::plug::core::bit2::fst
@ fst
Definition
autogen.h:118
mim::plug::core::bit2::nxor
@ nxor
Definition
autogen.h:115
mim::plug::core::bit2::xor_
@ xor_
Definition
autogen.h:112
mim::plug::core::bit2::snd
@ snd
Definition
autogen.h:116
mim::plug::core::bit2::f
@ f
Definition
autogen.h:106
mim::plug::core::bit2::and_
@ and_
Definition
autogen.h:114
mim::plug::core::bit2::or_
@ or_
Definition
autogen.h:120
mim::plug::core::bit2::t
@ t
Definition
autogen.h:121
mim::plug::core::trait
trait
Definition
autogen.h:286
mim::plug::core::icmp
icmp
Definition
autogen.h:178
mim::plug::core::icmp::ne
@ ne
Definition
autogen.h:220
mim::plug::core::op
const Def * op(trait o, const Def *type)
Definition
core.h:35
mim::plug::core::pe
pe
Definition
autogen.h:298
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::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::none
@ none
Wrap around.
Definition
core.h:16
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::is_commutative
constexpr bool is_commutative(Id)
Definition
axm.h:171
mim::is_associative
constexpr bool is_associative(Id id)
Definition
axm.h:177
mim::u64
uint64_t u64
Definition
types.h:27
world.h
include
mim
plug
core
core.h
Generated by
1.18.0