|
17 | 17 | namespace cmcpp |
18 | 18 | { |
19 | 19 | template <Boolean T> |
20 | | - inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
21 | | - { |
22 | | - using WasmValType = WasmValTypeTrait<ValTrait<T>::flat_types[0]>::type; |
23 | | - return {static_cast<WasmValType>(v)}; |
24 | | - } |
| 20 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v); |
25 | 21 |
|
26 | 22 | template <Char T> |
27 | | - inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
28 | | - { |
29 | | - using WasmValType = WasmValTypeTrait<ValTrait<T>::flat_types[0]>::type; |
30 | | - return {static_cast<WasmValType>(char_to_i32(cx, v))}; |
31 | | - } |
| 23 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v); |
32 | 24 |
|
33 | 25 | template <UnsignedInteger T> |
34 | | - inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
35 | | - { |
36 | | - using WasmValType = WasmValTypeTrait<ValTrait<T>::flat_types[0]>::type; |
37 | | - WasmValType fv = v; |
38 | | - return {fv}; |
39 | | - } |
| 26 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v); |
40 | 27 |
|
41 | 28 | template <SignedInteger T> |
42 | 29 | inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v); |
@@ -65,54 +52,42 @@ namespace cmcpp |
65 | 52 | template <Option T> |
66 | 53 | inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v); |
67 | 54 |
|
68 | | - template <Tuple T> |
69 | | - inline WasmValVector lower_heap_values(LiftLowerContext &cx, const T &vs) |
| 55 | + template <Field... Ts> |
| 56 | + inline WasmValVector lower_heap_values(LiftLowerContext &cx, Ts &&...vs) |
70 | 57 | { |
71 | | - using tuple_type = tuple_t<T>; |
72 | | - tuple_type tuple_value = vs; |
73 | | - auto ptr = cx.opts.realloc(0, 0, ValTrait<T>::alignment, ValTrait<T>::size); |
| 58 | + using tuple_type = tuple_t<Ts...>; |
| 59 | + auto ptr = cx.opts.realloc(0, 0, ValTrait<tuple_type>::alignment, ValTrait<tuple_type>::size); |
74 | 60 | WasmValVector flat_vals = {ptr}; |
75 | 61 | trap_if(cx, ptr != align_to(ptr, ValTrait<tuple_type>::alignment)); |
76 | 62 | trap_if(cx, ptr + ValTrait<tuple_type>::size > cx.opts.memory.size()); |
77 | | - store(cx, vs, ptr); |
| 63 | + store<tuple_type>(cx, std::forward<Ts>(vs)..., ptr); |
78 | 64 | return flat_vals; |
79 | 65 | } |
80 | 66 |
|
81 | | - template <Tuple T> |
82 | | - inline WasmValVector lower_flat_values(LiftLowerContext &cx, uint max_flat, const T &vs) |
| 67 | + template <Field... Ts> |
| 68 | + inline WasmValVector lower_flat_values(LiftLowerContext &cx, uint max_flat, Ts &&...vs) |
83 | 69 | { |
84 | | - // cx.inst.may_leave=false; |
85 | 70 | WasmValVector retVal = {}; |
86 | | - auto flat_types = ValTrait<T>::flat_types; |
| 71 | + // cx.inst.may_leave=false; |
| 72 | + constexpr auto flat_types = ValTrait<tuple_t<Ts...>>::flat_types; |
87 | 73 | if (flat_types.size() > max_flat) |
88 | 74 | { |
89 | | - retVal = lower_heap_values(cx, vs); |
| 75 | + retVal = lower_heap_values(cx, std::forward<Ts>(vs)...); |
90 | 76 | } |
91 | 77 | else |
92 | 78 | { |
93 | | - retVal = lower_flat(cx, vs); |
| 79 | + auto lower_v = [&](auto &&v) |
| 80 | + { |
| 81 | + auto flat = lower_flat(cx, v); |
| 82 | + retVal.insert(retVal.end(), flat.begin(), flat.end()); |
| 83 | + }; |
| 84 | + (lower_v(vs), ...); |
| 85 | + return retVal; |
94 | 86 | } |
95 | 87 | // cx.inst.may_leave=true; |
96 | 88 | return retVal; |
97 | 89 | } |
98 | 90 |
|
99 | | - template <Field... Args> |
100 | | - inline WasmValVector lower_flat_values(LiftLowerContext &cx, uint max_flat, Args &&...args) |
101 | | - { |
102 | | - using tuple_type = std::tuple<std::decay_t<Args>...>; |
103 | | - tuple_type vs{std::forward<Args>(args)...}; |
104 | | - WasmValVector retVal = {}; |
105 | | - auto flat_types = ValTrait<tuple_type>::flat_types; |
106 | | - if (flat_types.size() > max_flat) |
107 | | - { |
108 | | - retVal = lower_heap_values(cx, vs); |
109 | | - } |
110 | | - else |
111 | | - { |
112 | | - retVal = lower_flat(cx, vs); |
113 | | - } |
114 | | - return retVal; |
115 | | - } |
116 | 91 | } |
117 | 92 |
|
118 | 93 | #endif |
0 commit comments