Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdlib>
4#include <functional>
5#include <fstream>
6#include <iostream>
7#include <optional>
8#include <string>
9#include <memory>
10#include <vector>
11#include <cxxabi.h>
12
13#if !defined(LIKELY) && !defined(UNLIKELY)
14# if defined(__GNUC__)
15# if (__GNUC__ > 5) || defined(__clang__)
16# define LIKELY(x) __builtin_expect(!!(x), 1)
17# define UNLIKELY(x) __builtin_expect(!!(x), 0)
18# else
19# define LIKELY(x) !!(x)
20# define UNLIKELY(x) !!(x)
21# endif
22# else
23# define LIKELY(x) !!(x)
24# define UNLIKELY(x) !!(x)
25# endif
26#endif
27
28namespace sysio { namespace vm {
29 // helper to read a wasm file into a vector of bytes
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();
37 if (len < 0)
38 throw std::runtime_error("wasm file length is -1");
39 wasm.resize(len);
40 wasm_file.seekg(0, std::ios::beg);
41 wasm_file.read((char*)wasm.data(), wasm.size());
42 wasm_file.close();
43 return wasm;
44 }
45
46 // forward declarations
47 struct i32_const_t;
48 struct i64_const_t;
49 struct f32_const_t;
50 struct f64_const_t;
51
52 template <typename StackElem>
53 inline void print_result(const std::optional<StackElem>& result) {
54 if(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;
65 }
66 }
67
68 // helpers for std::visit
69 template <class... Ts>
70 struct overloaded : Ts... {
71 using Ts::operator()...;
72 };
73 template <class... Ts>
74 overloaded(Ts...)->overloaded<Ts...>;
75
76 // helpers for handling void returns
77 struct maybe_void_t {
78 template <typename T>
79 inline constexpr friend T&& operator, (T&& val, maybe_void_t) {return std::forward<T>(val);}
80 };
81
83
84 // simple utility function to demangle C++ type names
85 static inline std::string demangle(const char* mangled_name) {
86 size_t len = 0;
87 int status = 0;
88 ::std::unique_ptr<char, decltype(&::std::free)> ptr(
89 __cxxabiv1::__cxa_demangle(mangled_name, nullptr, &len, &status), &::std::free);
90 return ptr.get();
91 }
92
93 template<typename F>
94 struct scope_guard {
95 scope_guard(F&& f) : _f(static_cast<F&&>(f)) {}
97 F _f;
98 };
99
100 template<typename... T>
102
103 // helpers for creating subtuples
104 namespace detail {
105 template <size_t N, size_t I, typename T, typename... Ts>
106 struct subtuple_impl;
107
108 template <size_t N, size_t I, typename T, typename... Ts>
109 struct subtuple_impl <N, I, std::tuple<T, 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>() ) );
112 };
113
114 template <size_t N, typename T, typename... Ts>
115 struct subtuple_impl <N, N, std::tuple<T, Ts...>> {
116 using type = std::tuple<T>;
117 };
118
119 template <size_t N, typename T>
121
122 template <typename>
124
125 template <typename T, typename... Ts>
126 struct generate_subtuples_impl<std::tuple<T, 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...>>>()...);
130 }
131 };
132
133 template <typename T>
134 using generate_subtuples_t = decltype(generate_subtuples_impl<T>::value( std::make_index_sequence<std::tuple_size_v<T>>{} ));
135
136 template <typename T, typename... Ts>
137 struct generate_all_subtuples_impl;
138
139 template <typename T, typename... Ts>
140 struct generate_all_subtuples_impl<std::tuple<T, Ts...>> {
141 using type = decltype( std::tuple_cat( std::declval<typename generate_all_subtuples_impl<std::tuple<Ts...>>::type>(),
142 std::declval<generate_subtuples_t<std::tuple<T, Ts...>>>() ) );
143 };
144
145 template <>
147 using type = std::tuple<>;
148 };
149
150 template <typename Tuple>
151 constexpr auto generate_all_subtuples( Tuple&& tup) {
152 return std::declval<typename generate_all_subtuples_impl<Tuple>::type>();
153 }
154
155 template <size_t N>
156 struct tuple_index {
157 static constexpr size_t value = N;
158 };
159
160 template <size_t N, size_t I, typename Insert, typename Tuple>
161 struct insert_type {
162 using type = std::tuple<Insert>;
163 };
164
165 template <size_t N, size_t I, typename Insert, template<typename...> class Tuple, typename T, typename... Ts>
166 struct insert_type<N, I, Insert, Tuple<T, Ts...>> {
167 using type = decltype(std::tuple_cat(std::tuple<T>{}, typename insert_type<N, I+1, Insert, Tuple<Ts...>>::type{}));
168 };
169
170 template <size_t N, typename Insert, template<typename...> class Tuple, typename T, typename... Ts>
171 struct insert_type<N, N, Insert, Tuple<T, Ts...>> {
172 using type = std::tuple<Insert, T, Ts...>;
173 };
174
175 template <size_t I, typename Tuple, typename Indices>
176 constexpr auto get_tuple_size_from_index() {
177 if constexpr (I >= std::tuple_size_v<Tuple>)
178 return -1;
179 else
180 return std::tuple_size_v<std::tuple_element_t<std::tuple_element_t<I, Indices>::value, Tuple>>;
181 }
182
183 template <size_t I, typename Tuple>
184 constexpr auto get_tuple_size() {
185 if constexpr (I >= std::tuple_size_v<Tuple>)
186 return -1;
187 else
188 return std::tuple_size_v<std::tuple_element_t<I, Tuple>>;
189 }
190
191 template <size_t N, typename Insert, typename Tuple>
193
194 template <size_t N, size_t I, typename Tuple, typename Indices>
195 struct index_inserter {
197
198 template <size_t M = N, size_t J = I>
199 static constexpr auto value(std::enable_if_t<M!=J, int> = 0) {
201 return insert_type_t<I, tuple_index<N>, Indices>();
202 else
204 }
205
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>>());
209 }
210 };
211
212 template <size_t N, typename Tuple, typename Indices>
214
215 template <typename Tuple, typename Indices, size_t... Is>
216 constexpr auto reorder_tuple(std::index_sequence<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>...>();
219 }
220
221 template <typename Tuple, typename Indices>
222 using reorder_tuple_t = decltype(reorder_tuple<Tuple, Indices>(std::make_index_sequence<std::tuple_size_v<Indices>-1>{}));
223
224 // sort the tuple by largest subtuple to smallest subtuple
225 template <size_t N, size_t I, typename Tuple, typename Indices>
226 constexpr auto tuple_sort() {
227 if constexpr (N == I)
229 else
231 }
232
233 template <size_t N, size_t I, typename Tuple>
234 struct tuple_trim;
235
236 template <size_t N, size_t I, template <typename...> class Tuple, typename T, typename... Ts>
237 struct tuple_trim<N, I, Tuple<T, Ts...>> {
238 using type = typename tuple_trim<N, I+1, Tuple<Ts...>>::type;
239 };
240
241 template <size_t N, template <typename...> class Tuple, typename T, typename... Ts>
242 struct tuple_trim<N, N, Tuple<T, Ts...>> {
243 using type = Tuple<T, Ts...>;
244 };
245 } // namespace detail
246
247 template <typename Tuple>
248 using generate_all_subtuples_t = decltype(detail::generate_all_subtuples(std::declval<Tuple>()));
249
250 // sort the tuple of subtuples by largest subtuple to smallest subtuple
251 template <typename Tuple>
252 using tuple_sort_t = decltype(detail::tuple_sort<std::tuple_size_v<Tuple>-1, 0, Tuple, std::tuple<>>());
253
254 template <size_t N, typename Tuple>
256}} // namespace sysio::vm
Definition name.hpp:106
constexpr auto get_tuple_size_from_index()
Definition utils.hpp:176
constexpr auto get_tuple_size()
Definition utils.hpp:184
decltype(generate_subtuples_impl< T >::value(std::make_index_sequence< std::tuple_size_v< T > >{})) generate_subtuples_t
Definition utils.hpp:134
typename insert_type< N, 0, Insert, Tuple >::type insert_type_t
Definition utils.hpp:192
constexpr auto tuple_sort()
Definition utils.hpp:226
typename subtuple_impl< N, 0, T >::type subtuple_t
Definition utils.hpp:120
constexpr auto generate_all_subtuples(Tuple &&tup)
Definition utils.hpp:151
constexpr auto reorder_tuple(std::index_sequence< Is... >)
Definition utils.hpp:216
decltype(index_inserter< N, 0, Tuple, Indices >::value()) index_insert_t
Definition utils.hpp:213
decltype(reorder_tuple< Tuple, Indices >(std::make_index_sequence< std::tuple_size_v< Indices > -1 >{})) reorder_tuple_t
Definition utils.hpp:222
overloaded(Ts...) -> overloaded< Ts... >
void print_result(const std::optional< StackElem > &result)
Definition utils.hpp:53
typename detail::tuple_trim< N, 0, Tuple >::type tuple_trim_t
Definition utils.hpp:255
void ignore_unused_variable_warning(T &...)
Definition utils.hpp:101
decltype(detail::tuple_sort< std::tuple_size_v< Tuple > -1, 0, Tuple, std::tuple<> >()) tuple_sort_t
Definition utils.hpp:252
maybe_void_t maybe_void
Definition utils.hpp:82
std::vector< uint8_t > read_wasm(const std::string &fname)
Definition utils.hpp:30
decltype(detail::generate_all_subtuples(std::declval< Tuple >())) generate_all_subtuples_t
Definition utils.hpp:248
#define T(meth, val, expected)
const int N
Definition quantize.cpp:54
signed int int32_t
Definition stdint.h:123
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
Definition utils.hpp:141
static constexpr auto value(std::index_sequence< Is... >)
Definition utils.hpp:128
static constexpr auto value(std::enable_if_t< M!=J, int >=0)
Definition utils.hpp:199
static constexpr int32_t size_of_element
Definition utils.hpp:196
static constexpr auto value(std::enable_if_t< M==J, int >=0)
Definition utils.hpp:207
decltype(std::tuple_cat(std::tuple< T >{}, typename insert_type< N, I+1, Insert, Tuple< Ts... > >::type{})) type
Definition utils.hpp:167
std::tuple< Insert > type
Definition utils.hpp:162
decltype(std::tuple_cat(std::declval< std::tuple< T > >(), std::declval< typename subtuple_impl< N, I+1, std::tuple< Ts... > >::type >())) type
Definition utils.hpp:110
static constexpr size_t value
Definition utils.hpp:157
typename tuple_trim< N, I+1, Tuple< Ts... > >::type type
Definition utils.hpp:238
constexpr friend T && operator,(T &&val, maybe_void_t)
Definition utils.hpp:79
yh_object_type type
Definition yubihsm.h:672
size_t len