forked from GordonSmith/component-model-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlower.hpp
More file actions
104 lines (86 loc) · 2.99 KB
/
lower.hpp
File metadata and controls
104 lines (86 loc) · 2.99 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef CMCPP_LOWER_HPP
#define CMCPP_LOWER_HPP
#include "context.hpp"
#include "integer.hpp"
#include "float.hpp"
#include "string.hpp"
#include "list.hpp"
#include "flags.hpp"
#include "tuple.hpp"
#include "func.hpp"
#include "util.hpp"
#include <tuple>
#include <cassert>
namespace cmcpp
{
template <Boolean T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Char T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <UnsignedInteger T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <SignedInteger T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Float T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <String T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Flags T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <List T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Tuple T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Record T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Variant T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Option T>
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
template <Field... Ts>
inline WasmValVector lower_heap_values(LiftLowerContext &cx, uint32_t *out_param, Ts &&...vs)
{
using tuple_type = tuple_t<Ts...>;
tuple_type tuple_value = {std::forward<Ts>(vs)...};
uint32_t ptr;
WasmValVector flat_vals = {};
if (out_param == nullptr)
{
ptr = cx.opts.realloc(0, 0, ValTrait<tuple_type>::alignment, ValTrait<tuple_type>::size);
flat_vals = {ptr};
}
else
{
ptr = *out_param;
flat_vals = {};
}
trap_if(cx, ptr != align_to(ptr, ValTrait<tuple_type>::alignment));
trap_if(cx, ptr + ValTrait<tuple_type>::size > cx.opts.memory.size());
store<tuple_type>(cx, tuple_value, ptr);
return flat_vals;
}
template <Field... Ts>
inline WasmValVector lower_flat_values(LiftLowerContext &cx, uint32_t max_flat, uint32_t *out_param, Ts &&...vs)
{
WasmValVector retVal = {};
// cx.inst.may_leave=false;
constexpr auto flat_types = ValTrait<tuple_t<Ts...>>::flat_types;
if (flat_types.size() > max_flat)
{
retVal = lower_heap_values(cx, out_param, std::forward<Ts>(vs)...);
}
else
{
auto lower_v = [&](auto &&v)
{
auto flat = lower_flat(cx, v);
retVal.insert(retVal.end(), flat.begin(), flat.end());
};
(lower_v(vs), ...);
return retVal;
}
// cx.inst.may_leave=true;
return retVal;
}
}
#endif