MimIR
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/axm.h>
4#include <mim/world.h>
5
7
8namespace mim::plug::core {
9
10constexpr 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?
15enum 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.
20};
21
22/// Give Mode as mim::plug::math::Mode, mim::nat_t or const Def*.
23using VMode = std::variant<Mode, nat_t, const Def*>;
24
25/// mim::plug::core::VMode -> const Def*.
26inline 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///@{
35inline 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///@{
43inline 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]`
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 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}
66inline 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///@{
74inline 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}
78inline 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///@{
87template<bool up>
88const Sigma* convert(const TBound<up>* b);
89inline 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
94namespace mim {
95
96/// @name is_commutative/is_associative
97///@{
98// clang-format off
99constexpr bool is_commutative(plug::core::nat id) { return id == plug::core::nat ::add || id == plug::core::nat ::mul; }
100constexpr bool is_commutative(plug::core::ncmp id) { return id == plug::core::ncmp:: e || id == plug::core::ncmp:: ne; }
102constexpr bool is_commutative(plug::core::icmp id) { return id == plug::core::icmp:: e || id == plug::core::icmp:: ne; }
103// clang-format off
104
106 auto tab = make_truth_table(id);
107 return tab[0][1] == tab[1][0];
108}
109
111 switch (id) {
119 case plug::core::bit2::f: return true;
120 default: return false;
121 }
122}
123
124// clang-format off
125constexpr bool is_associative(plug::core::nat id) { return is_commutative(id); }
126constexpr bool is_associative(plug::core::ncmp id) { return is_commutative(id); }
127constexpr bool is_associative(plug::core::icmp id) { return is_commutative(id); }
128constexpr bool is_associative(plug::core::wrap id) { return is_commutative(id); }
129// clang-format on
130///@}
131
132} // namespace mim
133
134#ifndef DOXYGEN
135template<>
136struct fe::is_bit_enum<mim::plug::core::Mode> : std::true_type {};
137#endif
Common base for TBound.
Definition lattice.h:13
Base class for all Defs.
Definition def.h:246
World & world() const noexcept
Definition def.cpp:444
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:452
A dependent tuple type.
Definition tuple.h:20
Specific Bound depending on Up.
Definition lattice.h:34
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
The core Plugin
Definition core.h:8
const Def * extract_unsafe(const Def *d, const Def *i)
Definition core.h:62
const Sigma * convert(const TBound< up > *b)
Definition core.cpp:14
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
const Def * op(trait o, const Def *type)
Definition core.h:35
constexpr std::array< std::array< u64, 2 >, 2 > make_truth_table(bit2 id)
Definition core.h:52
const Def * insert_unsafe(const Def *d, const Def *i, const Def *val)
Definition core.h:74
constexpr flags_t icmp_mask
Definition core.h:10
@ nuw
No Unsigned Wrap around.
Definition core.h:18
@ none
Wrap around.
Definition core.h:16
@ nsw
No Signed Wrap around.
Definition core.h:17
Definition ast.h:14
u64 nat_t
Definition types.h:37
u8 sub_t
Definition types.h:42
u64 flags_t
Definition types.h:39
TBound< true > Join
AKA union.
Definition lattice.h:174
constexpr bool is_commutative(Id)
Definition axm.h:153
constexpr bool is_associative(Id id)
Definition axm.h:159
uint64_t u64
Definition types.h:27
TBound< false > Meet
AKA intersection.
Definition lattice.h:173