Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1#pragma once
2
3#include <thorin/axiom.h>
4#include <thorin/world.h>
5
7
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 thorin::plug::math::Mode, thorin::nat_t or Ref.
23using VMode = std::variant<Mode, nat_t, Ref>;
24
25/// thorin::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
52/// Use like this: `a op b = tab[a][b]`
53constexpr std::array<std::array<u64, 2>, 2> make_truth_table(bit2 id) {
54 return {
55 {{sub_t(id) & sub_t(0b0001) ? u64(-1) : 0, sub_t(id) & sub_t(0b0100) ? u64(-1) : 0},
56 {sub_t(id) & sub_t(0b0010) ? u64(-1) : 0, sub_t(id) & sub_t(0b1000) ? u64(-1) : 0}}
57 };
58}
59///@}
60
61/// @name extract_unsafe
62///@{
63inline Ref extract_unsafe(Ref d, Ref i) {
64 World& w = d->world();
65 return w.extract(d, w.call(conv::u, d->unfold_type()->arity(), i));
66}
67inline Ref extract_unsafe(Ref d, u64 i) {
68 World& w = d->world();
69 return extract_unsafe(d, w.lit_idx(0_u64, i));
70}
71///@}
72
73/// @name insert_unsafe
74///@{
75inline Ref insert_unsafe(Ref d, Ref i, Ref val) {
76 World& w = d->world();
77 return w.insert(d, w.call(conv::u, d->unfold_type()->arity(), i), val);
78}
79inline Ref insert_unsafe(Ref d, u64 i, Ref val) {
80 World& w = d->world();
81 return insert_unsafe(d, w.lit_idx(0_u64, i), val);
82}
83///@}
84
85/// @name Convert TBound to Sigma
86///@{
87/// This is WIP.
88template<bool up> const Sigma* convert(const TBound<up>* b);
89
90inline const Sigma* convert(const Bound* b) { return b->isa<Join>() ? convert(b->as<Join>()) : convert(b->as<Meet>()); }
91///@}
92
93} // namespace thorin::plug::core
94
95namespace thorin {
96
97/// @name is_commutative/is_associative
98///@{
99// clang-format off
100constexpr bool is_commutative(plug::core::nat id) { return id == plug::core::nat ::add || id == plug::core::nat ::mul; }
101constexpr bool is_commutative(plug::core::ncmp id) { return id == plug::core::ncmp:: e || id == plug::core::ncmp:: ne; }
103constexpr bool is_commutative(plug::core::icmp id) { return id == plug::core::icmp:: e || id == plug::core::icmp:: ne; }
104// clang-format off
105
107 auto tab = make_truth_table(id);
108 return tab[0][1] == tab[1][0];
109}
110
112 switch (id) {
113 case plug::core::bit2::t:
120 case plug::core::bit2::f: return true;
121 default: return false;
122 }
123}
124
125// clang-format off
126constexpr bool is_associative(plug::core::nat id) { return is_commutative(id); }
127constexpr bool is_associative(plug::core::ncmp id) { return is_commutative(id); }
128constexpr bool is_associative(plug::core::icmp id) { return is_commutative(id); }
129constexpr bool is_associative(plug::core::wrap id) { return is_commutative(id); }
130// clang-format on
131///@}
132
133} // namespace thorin
Common base for TBound.
Definition lattice.h:11
const Def * type() const
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:248
World & world() const
Definition def.cpp:421
Helper class to retrieve Infer::arg if present.
Definition def.h:87
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 Thorin nodes (Defs).
Definition world.h:35
Definition span.h:103
The core Plugin
Definition core.h:8
Ref extract_unsafe(Ref d, Ref i)
Definition core.h:63
Ref insert_unsafe(Ref d, Ref i, Ref val)
Definition core.h:75
constexpr std::array< std::array< u64, 2 >, 2 > make_truth_table(bit2 id)
Use like this: a op b = tab[a][b]
Definition core.h:53
Ref op(trait o, Ref type)
Definition core.h:35
std::variant< Mode, nat_t, Ref > VMode
Give Mode as thorin::plug::math::Mode, thorin::nat_t or Ref.
Definition core.h:23
const Sigma * convert(const TBound< up > *b)
Definition core.cpp:19
Definition cfg.h:11
u8 sub_t
Definition types.h:49
uint64_t u64
Definition types.h:35
constexpr bool is_associative(Id id)
Definition axiom.h:135
u64 nat_t
Definition types.h:44
constexpr bool is_commutative(Id)
Definition axiom.h:132
#define THORIN_ENUM_OPERATORS(E)
Use this to declare all kind of bit and comparison operators for an enum E.
Definition util.h:189