MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
plugin.cpp
Go to the documentation of this file.
1#include "mim/plugin.h"
2
3#include "mim/driver.h"
4
5using namespace std::literals;
6
7namespace mim {
8
9std::optional<plugin_t> Annex::mangle(Sym plugin) {
10 auto n = plugin.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 = plugin[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
37Sym Annex::demangle(Driver& driver, plugin_t plugin) {
38 std::string result;
39 for (size_t i = 0; i != Max_Plugin_Size; ++i) {
40 u64 c = (plugin & 0xfc00000000000000_u64) >> 58_u64;
41 if (c == 0)
42 return driver.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 plugin <<= 6_u64;
53 }
54
55 return driver.sym(result);
56}
57
58std::tuple<Sym, Sym, Sym> Annex::split(Driver& driver, 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 = driver.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 = driver.sym(subview(tag, dot + 1));
72 tag = subview(tag, 0, dot);
73 return {plugin, driver.sym(tag), sub};
74 }
75
76 return {plugin, driver.sym(tag), {}};
77}
78
79} // namespace mim
Some "global" variables needed all over the place.
Definition driver.h:17
Definition cfg.h:11
u64 plugin_t
Definition types.h:46
uint64_t u64
Definition types.h:34
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
static std::tuple< Sym, Sym, Sym > split(Driver &, Sym)
Definition plugin.cpp:58
static constexpr size_t Max_Plugin_Size
Definition plugin.h:57
static Sym demangle(Driver &, plugin_t plugin)
Reverts an Axiom::mangled string to a Sym.
Definition plugin.cpp:37
static std::optional< plugin_t > mangle(Sym plugin)
Mangles s into a dense 48-bit representation.
Definition plugin.cpp:9