Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
plugin.cpp
Go to the documentation of this file.
1#include "thorin/plugin.h"
2
3#include "thorin/world.h"
4
5using namespace std::literals;
6
7namespace thorin {
8
9std::optional<plugin_t> Annex::mangle(Sym s) {
10 auto n = s.size();
11 if (n > Max_Plugin_Size) return {};
12
13 u64 result = 0;
14 for (size_t i = 0; i != Max_Plugin_Size; ++i) {
15 u64 u = '\0';
16
17 if (i < n) {
18 auto c = s[i];
19 if (c == '_')
20 u = 1;
21 else if ('a' <= c && c <= 'z')
22 u = c - 'a' + 2_u64;
23 else if ('A' <= c && c <= 'Z')
24 u = c - 'A' + 28_u64;
25 else if ('0' <= c && c <= '9')
26 u = c - '0' + 54_u64;
27 else
28 return {};
29 }
30
31 result = (result << 6_u64) | u;
32 }
33
34 return result << 16_u64;
35}
36
38 std::string result;
39 for (size_t i = 0; i != Max_Plugin_Size; ++i) {
40 u64 c = (u & 0xfc00000000000000_u64) >> 58_u64;
41 if (c == 0)
42 return world.sym(result);
43 else if (c == 1)
44 result += '_';
45 else if (2 <= c && c < 28)
46 result += 'a' + ((char)c - 2);
47 else if (28 <= c && c < 54)
48 result += 'A' + ((char)c - 28);
49 else
50 result += '0' + ((char)c - 54);
51
52 u <<= 6_u64;
53 }
54
55 return world.sym(result);
56}
57
58std::array<Sym, 3> Annex::split(World& world, Sym s) {
59 if (!s) return {};
60 if (s[0] != '%') return {};
61 auto sv = subview(s, 1);
62
63 auto dot = sv.find('.');
64 if (dot == std::string_view::npos) return {};
65
66 auto plugin = world.sym(subview(sv, 0, dot));
67 if (!mangle(plugin)) return {};
68
69 auto tag = subview(sv, dot + 1);
70 if (auto dot = tag.find('.'); dot != std::string_view::npos) {
71 auto sub = world.sym(subview(tag, dot + 1));
72 tag = subview(tag, 0, dot);
73 return {plugin, world.sym(tag), sub};
74 }
75
76 return {plugin, world.sym(tag), {}};
77}
78
79} // namespace thorin
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Sym sym(std::string_view)
Definition world.cpp:77
Definition cfg.h:11
std::string_view subview(std::string_view s, size_t i, size_t n=std::string_view::npos)
Like std::string::substr, but works on std::string_view instead.
Definition util.h:64
uint64_t u64
Definition types.h:35
u64 plugin_t
Definition types.h:47
static Sym demangle(World &, plugin_t u)
Reverts an Axiom::mangled string to a Sym.
Definition plugin.cpp:37
static std::optional< plugin_t > mangle(Sym s)
Mangles s into a dense 48-bit representation.
Definition plugin.cpp:9
static constexpr flags_t Base
Definition plugin.h:131
static std::array< Sym, 3 > split(World &, Sym)
Definition plugin.cpp:58
static constexpr size_t Max_Plugin_Size
Definition plugin.h:68