MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1#pragma once
2
3#include <mim/axiom.h>
4#include <mim/world.h>
5
7
8namespace mim::plug::core {
9
10/// @name Mode
11///@{
12/// What should happen if Idx arithmetic overflows?
13enum class Mode : nat_t {
14 none = 0, ///< Wrap around.
15 nsw = 1 << 0, ///< No Signed Wrap around.
16 nuw = 1 << 1, ///< No Unsigned Wrap around.
17 nusw = nuw | nsw,
18};
19
21
22/// Give Mode as mim::plug::math::Mode, mim::nat_t or Ref.
23using VMode = std::variant<Mode, nat_t, Ref>;
24
25/// mim::plug::core::VMode -> Ref.
26inline Ref mode(World& w, VMode m) {
27 if (auto def = std::get_if<Ref>(&m)) return *def;
28 if (auto nat = std::get_if<nat_t>(&m)) return w.lit_nat(*nat);
29 return w.lit_nat((nat_t)std::get<Mode>(m));
30}
31///@}
32
33/// @name %%core.trait
34///@{
35inline Ref op(trait o, Ref type) {
36 World& w = type->world();
37 return w.app(w.annex(o), type);
38}
39///@}
40
41/// @name %%core.pe
42///@{
43inline Ref op(pe o, Ref 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]`
52constexpr 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///@{
62inline Ref extract_unsafe(Ref d, Ref i) {
63 World& w = d->world();
64 return w.extract(d, w.call(conv::u, d->unfold_type()->arity(), i));
65}
66inline Ref extract_unsafe(Ref 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///@{
74inline Ref insert_unsafe(Ref d, Ref i, Ref val) {
75 World& w = d->world();
76 return w.insert(d, w.call(conv::u, d->unfold_type()->arity(), i), val);
77}
78inline Ref insert_unsafe(Ref d, u64 i, Ref 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///@{
87template<bool up> const Sigma* convert(const TBound<up>* b);
88inline const Sigma* convert(const Bound* b) { return b->isa<Join>() ? convert(b->as<Join>()) : convert(b->as<Meet>()); }
89///@}
90
91} // namespace mim::plug::core
92
93namespace mim {
94
95/// @name is_commutative/is_associative
96///@{
97// clang-format off
98constexpr bool is_commutative(plug::core::nat id) { return id == plug::core::nat ::add || id == plug::core::nat ::mul; }
99constexpr bool is_commutative(plug::core::ncmp id) { return id == plug::core::ncmp:: e || id == plug::core::ncmp:: ne; }
101constexpr bool is_commutative(plug::core::icmp id) { return id == plug::core::icmp:: e || id == plug::core::icmp:: ne; }
102// clang-format off
103
105 auto tab = make_truth_table(id);
106 return tab[0][1] == tab[1][0];
107}
108
110 switch (id) {
111 case plug::core::bit2::t:
118 case plug::core::bit2::f: return true;
119 default: return false;
120 }
121}
122
123// clang-format off
124constexpr bool is_associative(plug::core::nat id) { return is_commutative(id); }
125constexpr bool is_associative(plug::core::ncmp id) { return is_commutative(id); }
126constexpr bool is_associative(plug::core::icmp id) { return is_commutative(id); }
127constexpr bool is_associative(plug::core::wrap id) { return is_commutative(id); }
128// clang-format on
129///@}
130
131} // namespace mim
Common base for TBound.
Definition lattice.h:11
World & world() const
Definition def.cpp:417
const Def * type() const
Definition def.h:245
Helper class to retrieve Infer::arg if present.
Definition def.h:85
A dependent tuple type.
Definition tuple.h:9
Specific Bound depending on Up.
Definition lattice.h:31
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
The core Plugin
Definition core.h:8
const Sigma * convert(const TBound< up > *b)
Definition core.cpp:19
Ref op(trait o, Ref type)
Definition core.h:35
constexpr std::array< std::array< u64, 2 >, 2 > make_truth_table(bit2 id)
Definition core.h:52
Ref insert_unsafe(Ref d, Ref i, Ref val)
Definition core.h:74
Ref extract_unsafe(Ref d, Ref i)
Definition core.h:62
std::variant< Mode, nat_t, Ref > VMode
Give Mode as mim::plug::math::Mode, mim::nat_t or Ref.
Definition core.h:23
@ none
Wrap around.
Definition cfg.h:11
u64 nat_t
Definition types.h:43
u8 sub_t
Definition types.h:48
constexpr bool is_commutative(Id)
Definition axiom.h:132
constexpr bool is_associative(Id id)
Definition axiom.h:135
uint64_t u64
Definition types.h:34
Definition span.h:104
constexpr decltype(auto) get(mim::Span< T, N > span)
Definition span.h:113
#define MIM_ENUM_OPERATORS(E)
Use this to declare all kind of bit and comparison operators for an enum E.
Definition util.h:189