-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflags.hpp
More file actions
83 lines (70 loc) · 2.1 KB
/
flags.hpp
File metadata and controls
83 lines (70 loc) · 2.1 KB
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
#ifndef CMCPP_FLAGS_HPP
#define CMCPP_FLAGS_HPP
#include <cstring>
#include "context.hpp"
#include "integer.hpp"
#include "util.hpp"
namespace cmcpp
{
namespace flags
{
template <Flags T>
int32_t pack_flags_into_int(const T &v)
{
return static_cast<int32_t>(v.to_ulong());
}
template <Flags T>
void store(LiftLowerContext &cx, const T &v, offset ptr)
{
auto i = pack_flags_into_int(v);
std::memcpy(&cx.opts.memory[ptr], &i, ValTrait<T>::size);
}
template <Flags T>
WasmValVector lower_flat(LiftLowerContext &cx, const T &v)
{
return {pack_flags_into_int(v)};
}
template <Flags T>
T unpack_flags_from_int(const uint32_t &buff)
{
T value{};
using bitset_type = typename ValTrait<T>::inner_type;
static_cast<bitset_type &>(value) = bitset_type(static_cast<unsigned long long>(buff));
return value;
}
template <Flags T>
T load(const LiftLowerContext &cx, uint32_t ptr)
{
uint32_t raw = 0;
std::memcpy(&raw, &cx.opts.memory[ptr], ValTrait<T>::size);
return unpack_flags_from_int<T>(raw);
}
template <Flags T>
T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
{
auto i = vi.next<int32_t>();
return unpack_flags_from_int<T>(checked_int32(cx, i, "flag value exceeds 32-bit range"));
}
}
template <Flags T>
inline void store(LiftLowerContext &cx, const T &v, uint32_t ptr)
{
flags::store(cx, v, ptr);
}
template <Flags T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v)
{
return flags::lower_flat(cx, v);
}
template <Flags T>
inline T load(const LiftLowerContext &cx, uint32_t ptr)
{
return flags::load<T>(cx, ptr);
}
template <Flags T>
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
{
return flags::lift_flat<T>(cx, vi);
}
}
#endif