13#if !defined(LIKELY) && !defined(UNLIKELY)
15# if (__GNUC__ > 5) || defined(__clang__)
16# define LIKELY(x) __builtin_expect(!!(x), 1)
17# define UNLIKELY(x) __builtin_expect(!!(x), 0)
19# define LIKELY(x) !!(x)
20# define UNLIKELY(x) !!(x)
23# define LIKELY(x) !!(x)
24# define UNLIKELY(x) !!(x)
28namespace sysio {
namespace vm {
30 inline std::vector<uint8_t>
read_wasm(
const std::string& fname) {
31 std::ifstream wasm_file(fname, std::ios::binary);
32 if (!wasm_file.is_open())
33 throw std::runtime_error(
"wasm file not found");
34 wasm_file.seekg(0, std::ios::end);
35 std::vector<uint8_t> wasm;
36 int len = wasm_file.tellg();
38 throw std::runtime_error(
"wasm file length is -1");
40 wasm_file.seekg(0, std::ios::beg);
41 wasm_file.read((
char*)wasm.data(), wasm.size());
52 template <
typename StackElem>
53 inline void print_result(
const std::optional<StackElem>& result) {
55 std::cout <<
"result: ";
56 if (result->template is_a<i32_const_t>())
57 std::cout <<
"i32:" << result->to_ui32();
58 else if (result->template is_a<i64_const_t>())
59 std::cout <<
"i64:" << result->to_ui64();
60 else if (result->template is_a<f32_const_t>())
61 std::cout <<
"f32:" << result->to_f32();
62 else if (result->template is_a<f64_const_t>())
63 std::cout <<
"f64:" << result->to_f64();
64 std::cout << std::endl;
69 template <
class... Ts>
71 using Ts::operator()...;
73 template <
class... Ts>
85 static inline std::string demangle(
const char* mangled_name) {
88 ::std::unique_ptr<char,
decltype(&::std::free)> ptr(
89 __cxxabiv1::__cxa_demangle(mangled_name,
nullptr, &
len, &status), &::std::free);
100 template<
typename...
T>
105 template <
size_t N,
size_t I,
typename T,
typename... Ts>
108 template <
size_t N,
size_t I,
typename T,
typename... Ts>
110 using type =
decltype( std::tuple_cat( std::declval<std::tuple<T>>(),
111 std::declval<
typename subtuple_impl<
N, I+1, std::tuple<Ts...>>::type>() ) );
114 template <
size_t N,
typename T,
typename... Ts>
116 using type = std::tuple<T>;
119 template <
size_t N,
typename T>
125 template <
typename T,
typename... Ts>
127 template <
size_t... Is>
128 static constexpr auto value( std::index_sequence<Is...> ) {
129 return std::make_tuple(std::declval<
subtuple_t<Is, std::tuple<T, Ts...>>>()...);
133 template <
typename T>
134 using generate_subtuples_t =
decltype(generate_subtuples_impl<T>::value( std::make_index_sequence<std::tuple_size_v<T>>{} ));
136 template <
typename T,
typename... Ts>
139 template <
typename T,
typename... Ts>
147 using type = std::tuple<>;
150 template <
typename Tuple>
152 return std::declval<typename generate_all_subtuples_impl<Tuple>::type>();
157 static constexpr size_t value =
N;
160 template <
size_t N,
size_t I,
typename Insert,
typename Tuple>
162 using type = std::tuple<Insert>;
165 template <
size_t N,
size_t I,
typename Insert,
template<
typename...>
class Tuple,
typename T,
typename... Ts>
167 using type =
decltype(std::tuple_cat(std::tuple<T>{},
typename insert_type<
N, I+1, Insert, Tuple<Ts...>>::type{}));
170 template <
size_t N,
typename Insert,
template<
typename...>
class Tuple,
typename T,
typename... Ts>
172 using type = std::tuple<Insert,
T, Ts...>;
175 template <
size_t I,
typename Tuple,
typename Indices>
177 if constexpr (I >= std::tuple_size_v<Tuple>)
180 return std::tuple_size_v<std::tuple_element_t<std::tuple_element_t<I, Indices>::value, Tuple>>;
183 template <
size_t I,
typename Tuple>
185 if constexpr (I >= std::tuple_size_v<Tuple>)
188 return std::tuple_size_v<std::tuple_element_t<I, Tuple>>;
191 template <
size_t N,
typename Insert,
typename Tuple>
194 template <
size_t N,
size_t I,
typename Tuple,
typename Indices>
198 template <
size_t M = N,
size_t J = I>
199 static constexpr auto value(std::enable_if_t<M!=J, int> = 0) {
206 template <
size_t M = N,
size_t J = I>
207 static constexpr auto value(std::enable_if_t<M==J, int> = 0) {
208 return std::tuple_cat(Indices{}, std::tuple<tuple_index<N>>());
212 template <
size_t N,
typename Tuple,
typename Indices>
215 template <
typename Tuple,
typename Indices,
size_t... Is>
217 static_assert(std::tuple_size_v<Tuple> == std::tuple_size_v<Indices>);
218 return std::tuple<std::tuple_element_t<std::tuple_element_t<Is, Indices>::value, Tuple>...>();
221 template <
typename Tuple,
typename Indices>
225 template <
size_t N,
size_t I,
typename Tuple,
typename Indices>
227 if constexpr (
N == I)
233 template <
size_t N,
size_t I,
typename Tuple>
236 template <
size_t N,
size_t I,
template <
typename...>
class Tuple,
typename T,
typename... Ts>
241 template <
size_t N,
template <
typename...>
class Tuple,
typename T,
typename... Ts>
243 using type = Tuple<
T, Ts...>;
247 template <
typename Tuple>
251 template <
typename Tuple>
254 template <
size_t N,
typename Tuple>
constexpr auto get_tuple_size_from_index()
constexpr auto get_tuple_size()
decltype(generate_subtuples_impl< T >::value(std::make_index_sequence< std::tuple_size_v< T > >{})) generate_subtuples_t
typename insert_type< N, 0, Insert, Tuple >::type insert_type_t
constexpr auto tuple_sort()
typename subtuple_impl< N, 0, T >::type subtuple_t
constexpr auto generate_all_subtuples(Tuple &&tup)
constexpr auto reorder_tuple(std::index_sequence< Is... >)
decltype(index_inserter< N, 0, Tuple, Indices >::value()) index_insert_t
decltype(reorder_tuple< Tuple, Indices >(std::make_index_sequence< std::tuple_size_v< Indices > -1 >{})) reorder_tuple_t
overloaded(Ts...) -> overloaded< Ts... >
void print_result(const std::optional< StackElem > &result)
typename detail::tuple_trim< N, 0, Tuple >::type tuple_trim_t
void ignore_unused_variable_warning(T &...)
decltype(detail::tuple_sort< std::tuple_size_v< Tuple > -1, 0, Tuple, std::tuple<> >()) tuple_sort_t
std::vector< uint8_t > read_wasm(const std::string &fname)
decltype(detail::generate_all_subtuples(std::declval< Tuple >())) generate_all_subtuples_t
#define T(meth, val, expected)
decltype(std::tuple_cat(std::declval< typename generate_all_subtuples_impl< std::tuple< Ts... > >::type >(), std::declval< generate_subtuples_t< std::tuple< T, Ts... > > >())) type
static constexpr auto value(std::index_sequence< Is... >)
static constexpr auto value(std::enable_if_t< M!=J, int >=0)
static constexpr int32_t size_of_element
static constexpr auto value(std::enable_if_t< M==J, int >=0)
decltype(std::tuple_cat(std::tuple< T >{}, typename insert_type< N, I+1, Insert, Tuple< Ts... > >::type{})) type
std::tuple< Insert, T, Ts... > type
std::tuple< Insert > type
decltype(std::tuple_cat(std::declval< std::tuple< T > >(), std::declval< typename subtuple_impl< N, I+1, std::tuple< Ts... > >::type >())) type
static constexpr size_t value
typename tuple_trim< N, I+1, Tuple< Ts... > >::type type
constexpr friend T && operator,(T &&val, maybe_void_t)