-
Notifications
You must be signed in to change notification settings - Fork 3
/
core-misc.cc
192 lines (169 loc) · 11.2 KB
/
core-misc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// core-misc.cc -- core miscellaneous
/* Copyright (C) 2018, 2019, 2020 Alexey Protasov (AKA Alex or rusini)
This file is part of MANOOL.
MANOOL is free software: you can redistribute it and/or modify it under the terms of the version 3 of the GNU General Public License
as published by the Free Software Foundation (and only version 3).
MANOOL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with MANOOL. If not, see <https://www.gnu.org/licenses/>. */
# include "config.tcc"
# include "mnl-aux-core.tcc"
# include <time.h> // clock_gettime
# include <cstdio> // sprintf, stderr, fprintf, fputs, fflush
namespace MNL_AUX_UUID { using namespace aux;
namespace aux {
using std::_Exit; using std::srand; // <cstdlib>
using std::sprintf; using std::fprintf; using std::fputs; using std::fflush; // <cstdio>
}
// Translation Infrastructure //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MNL_IF_WITH_MT(thread_local) decltype(symtab) pub::symtab;
namespace aux { namespace { struct stub {
MNL_INLINE static val execute(bool) { MNL_UNREACHABLE(); }
MNL_INLINE static void exec_in(val &&) { MNL_UNREACHABLE(); }
MNL_INLINE static val exec_out() { MNL_UNREACHABLE(); }
MNL_INLINE static bool is_rvalue() { return false; }
MNL_INLINE static bool is_lvalue() { return false; }
MNL_INLINE static code compile(code &&, const form &, const loc &_loc) { err_compile("invalid form", _loc); }
}; }}
code pub::compile(const form &form, const loc &_loc) { return // *** The Compiler Core Dispatcher! ***
test<sym>(form) && symtab[cast<const sym &>(form)] ?
symtab[cast<const sym &>(form)] :
test<sym>(form) && (((const string &)cast<const sym &>(form))[0] >= 'a' && ((const string &)cast<const sym &>(form))[0] <= 'z' ||
((const string &)cast<const sym &>(form))[0] == '_' || ((const string &)cast<const sym &>(form))[0] == '`') ?
MNL_AUX_INIT((code)stub{}) :
test<long long>(form) || test<string>(form) || test<sym>(form) ?
[&]()->code{ code make_lit(const val &); return make_lit(form); }() : // actually from MANOOL API
form.is_list() && !form.empty() ?
compile(form.front(), form._loc(_loc)).compile(form, form._loc(_loc)) :
test<code>(form) ?
cast<const code &>(form) :
((form.is_list() ? err_compile("invalid form", form._loc(_loc)) : MNL_ERR(MNL_SYM("TypeMismatch"))), code{});
}
void pub::err_compile(const char *msg, const loc &loc) {
char buf[sizeof " 2147483647:2147483647-2147483647:2147483647 Error: "];
throw pair<sym, val>{MNL_SYM("CompileError"), (!loc.origin ? (string)"<unknown location> Error: " : '(' + *loc.origin + ')' +
(sprintf(buf, " %d:%d-%d:%d Error: ", loc._start.first, loc._start.second, loc._final.first, loc._final.second - 1), buf)) + msg};
}
// class sym ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MNL_PRIORITY(1003) decltype(sym::pool) sym::pool;
MNL_IF_WITH_MT(MNL_PRIORITY(1004) decltype(sym::mutex) sym::mutex;)
sym::sym(string txt): rep([&]()->decltype(rep){ // precond: txt[0] != '`'
MNL_IF_WITH_MT( return std::lock_guard<std::mutex>{mutex}, [&]()->decltype(rep){ )
decltype(dict)::const_iterator it = dict.find(txt);
if (it != dict.end())
return addref(it->second), it->second;
if (!pool.empty()) {
inverse[pool.back()] = it = dict.insert(make_pair(move(txt), pool.back())).first, pool.pop_back();
} else {
if (!static_cast<decltype(rep)>(dict.size())) // wrap-around means no more IDs available
fputs("MANOOL: FATAL ERROR: Symbol space exhausted\n", stderr), fflush(stderr), _Exit(EXIT_FAILURE);
pool.reserve(dict.size() + 1);
it = dict.insert(make_pair(move(txt), static_cast<decltype(rep)>(dict.size()))).first, inverse[dict.size() - 1] = it;
}
return rc[it->second] = 1, it->second;
MNL_IF_WITH_MT( }(); )
}()) {}
sym::sym(const char *txt): sym((string)txt)
{}
sym::sym(decltype(nullptr)): rep([&]()->decltype(rep){
MNL_IF_WITH_MT( return std::lock_guard<std::mutex>{mutex}, [&]()->decltype(rep){ )
auto mask = MNL_AUX_RAND(unsigned short);
decltype(dict)::const_iterator it;
if (!pool.empty()) {
char txt[sizeof "`65535"];
sprintf(txt, "`%u", (unsigned)pool.back() ^ mask);
inverse[pool.back()] = it = dict.insert(make_pair(move(txt), pool.back())).first, pool.pop_back();
} else {
if (!static_cast<decltype(rep)>(dict.size())) // wrap-around means no more IDs available
fputs("MANOOL: FATAL ERROR: Symbol space exhausted\n", stderr), fflush(stderr), _Exit(EXIT_FAILURE);
char txt[sizeof "`65535"];
sprintf(txt, "`%u", (unsigned)dict.size() ^ mask);
pool.reserve(dict.size() + 1);
it = dict.insert(make_pair(move(txt), static_cast<decltype(rep)>(dict.size()))).first, inverse[dict.size() - 1] = it;
}
return rc[it->second] = 1, it->second;
MNL_IF_WITH_MT( }(); )
}()) {}
void sym::free(decltype(rep) rep) noexcept {
MNL_IF_WITH_MT( std::lock_guard<std::mutex>{mutex}, [&]{ if (!__atomic_load_n(&rc[rep], __ATOMIC_RELAXED)) )
dict.erase(inverse[rep]), pool.push_back(rep);
MNL_IF_WITH_MT( }(); )
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
val val::default_invoke(const sym &op, int argc, val argv[]) {
switch (MNL_DISP("==", "<>", "Order", "Clone", "DeepClone", "Str")[op]) {
case 1: // ==
if (MNL_UNLIKELY(argc != 1)) MNL_ERR(MNL_SYM("InvalidInvocation"));
return MNL_LIKELY(argv[0].rep.tag() == 0x7FF8u) && argv[0].rep.dat<void *>() == rep.dat<void *>();
case 2: // <>
if (MNL_UNLIKELY(argc != 1)) MNL_ERR(MNL_SYM("InvalidInvocation"));
return !MNL_LIKELY(argv[0].rep.tag() == 0x7FF8u) || argv[0].rep.dat<void *>() != rep.dat<void *>();
case 3: // Order
if (MNL_UNLIKELY(argc != 1)) MNL_ERR(MNL_SYM("InvalidInvocation"));
{ auto mask = MNL_AUX_RAND(uintptr_t);
int res = default_order(argv[0]);
return MNL_UNLIKELY(res) ? res :
((reinterpret_cast<uintptr_t>(rep.dat<void *>()) ^ mask) < (reinterpret_cast<uintptr_t>(argv[0].rep.dat<void *>()) ^ mask)) -
((reinterpret_cast<uintptr_t>(argv[0].rep.dat<void *>()) ^ mask) < (reinterpret_cast<uintptr_t>(rep.dat<void *>()) ^ mask));
}
case 4: // Clone
if (MNL_UNLIKELY(argc != 0)) MNL_ERR(MNL_SYM("InvalidInvocation"));
return move(*this);
case 5: // DeepClone
if (MNL_UNLIKELY(argc != 0)) MNL_ERR(MNL_SYM("InvalidInvocation"));
return static_cast<root *>(rep.dat<void *>())->invoke(move(*this), MNL_SYM("Clone"), 0, {});
case 6: // Str
if (MNL_UNLIKELY(argc != 0)) MNL_ERR(MNL_SYM("InvalidInvocation"));
{ static MNL_IF_WITH_MT(thread_local) const val res = "value/object"; return res; }
case 0:
MNL_ERR(MNL_SYM("UnrecognizedOperation"));
}}
// Signals, Exceptions, and Invocation Traces //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MNL_IF_WITH_MT(thread_local) decltype(sig_state) pub::sig_state;
MNL_IF_WITH_MT(thread_local) decltype(sig_trace) pub::sig_trace = []()->decltype(sig_trace){ decltype(sig_trace) res; res.reserve(100 + 1); return res; }();
void aux::error(const sym &err)
{ throw make_pair(err, val{}); }
void aux::error(const loc &loc, const sym &err)
{ if (loc.origin && sig_trace.size() < sig_trace.capacity()) sig_trace.push_back({loc, "evaluating"}); throw make_pair(err, val{}); }
void pub::trace_execute (const loc &loc) { if (loc.origin && sig_trace.size() < sig_trace.capacity()) sig_trace.push_back({loc, "evaluating"}); throw; }
void pub::trace_exec_in (const loc &loc) { if (loc.origin && sig_trace.size() < sig_trace.capacity()) sig_trace.push_back({loc, "storing a value"}); throw; }
void pub::trace_exec_out(const loc &loc) { if (loc.origin && sig_trace.size() < sig_trace.capacity()) sig_trace.push_back({loc, "moving-out a value"}); throw; }
// Record Descriptors //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
record_descr::record_descr(const record_descr &rhs) noexcept: rep(rhs.rep) { addref(); }
record_descr::~record_descr() { release(); }
record_descr &record_descr::operator=(const record_descr &rhs) noexcept { rhs.addref(), release(), rep = rhs.rep; return *this; }
void record_descr::addref() const noexcept {
if (rep == store.end()) return;
MNL_IF_WITHOUT_MT(++rep->second.second) MNL_IF_WITH_MT(__atomic_add_fetch(&rep->second.second, 1, __ATOMIC_RELAXED));
}
void record_descr::release() const noexcept {
if (rep == store.end()) return;
if (MNL_UNLIKELY(! MNL_IF_WITHOUT_MT(--rep->second.second) MNL_IF_WITH_MT(__atomic_sub_fetch(&rep->second.second, 1, __ATOMIC_RELAXED)) ))
MNL_IF_WITH_MT( std::lock_guard<std::mutex>{mutex}, [=]{ if (!__atomic_load_n(&rep->second.second, __ATOMIC_RELAXED)) )
store.erase(rep);
MNL_IF_WITH_MT( }(); )
}
record_descr::record_descr(set<sym> items)
: rep((MNL_IF_WITH_MT(std::lock_guard<std::mutex>{mutex},) [&]()->decltype(rep){
{ auto it = store.find(items); if (it != store.end())
return MNL_IF_WITHOUT_MT(++it->second.second) MNL_IF_WITH_MT(__atomic_add_fetch(&it->second.second, 1, __ATOMIC_RELAXED)), it;
}
sym::tab<unsigned char> tab(-1); { int disp/*lacement*/ = 0; for (const auto &el: items) tab.update(el, disp++); }
return store.insert(make_pair(move(items), make_pair(move((tab.shrink_to_fit(), tab)), 1))).first;
}())) {}
record_descr::record_descr(initializer_list<const char *> il): record_descr([=]()->set<sym>{
set<sym> res; for (auto el: il) res.insert(el); return res;
}()) {}
int pub::order(const record_descr &lhs, const record_descr &rhs) noexcept {
auto mask = MNL_AUX_RAND(uintptr_t);
return
((reinterpret_cast<uintptr_t>(&*lhs.rep) ^ mask) < (reinterpret_cast<uintptr_t>(&*rhs.rep) ^ mask)) -
((reinterpret_cast<uintptr_t>(&*rhs.rep) ^ mask) < (reinterpret_cast<uintptr_t>(&*lhs.rep) ^ mask));
}
decltype(record_descr::store) record_descr::store;
MNL_IF_WITH_MT(decltype(record_descr::mutex) record_descr::mutex;)
// Seed Legacy Random Number Generator /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace aux { namespace { MNL_PRIORITY(1000) class { int _ = []()->int{
struct ::timespec ts; ::clock_gettime(CLOCK_REALTIME, &ts); return srand(ts.tv_sec ^ (unsigned)ts.tv_nsec / 1000), 0;
}(); } _srand; }}
} // namespace MNL_AUX_UUID