|
2 | 2 | #define CMCPP_FUNC_HPP |
3 | 3 |
|
4 | 4 | #include "context.hpp" |
| 5 | +#include "flags.hpp" |
5 | 6 |
|
6 | 7 | namespace cmcpp |
7 | 8 | { |
8 | 9 | namespace func |
9 | 10 | { |
10 | | - // template <Flags T> |
11 | | - // int32_t pack_flags_into_int(const T &v) |
12 | | - // { |
13 | | - // return v.to_ulong(); |
14 | | - // } |
15 | | - |
16 | | - // template <Flags T> |
17 | | - // void store(LiftLowerContext &cx, const T &v, offset ptr) |
18 | | - // { |
19 | | - // auto i = pack_flags_into_int(v); |
20 | | - // std::memcpy(&cx.opts.memory[ptr], i, ValTrait<T>::size); |
21 | | - // } |
22 | | - |
23 | | - // template <Flags T> |
24 | | - // WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
25 | | - // { |
26 | | - // return {pack_flags_into_int(v)}; |
27 | | - // } |
28 | | - |
29 | | - // template <Flags T> |
30 | | - // T unpack_flags_from_int(const uint32_t &buff) |
31 | | - // { |
32 | | - // return {buff}; |
33 | | - // } |
34 | | - |
35 | | - // template <Flags T> |
36 | | - // T load(const LiftLowerContext &cx, uint32_t ptr) |
37 | | - // { |
38 | | - // uint8_t buff[ValTrait<T>::size]; |
39 | | - // std::memcpy(&buff, &cx.opts.memory[ptr], ValTrait<T>::size); |
40 | | - // return unpack_flags_from_int<T>(buff); |
41 | | - // } |
42 | | - |
43 | | - // template <Flags T> |
44 | | - // T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi) |
45 | | - // { |
46 | | - // auto i = vi.next<int32_t>(); |
47 | | - // uint8_t buff[ValTrait<T>::size]; |
48 | | - // std::memcpy(&buff, &i, ValTrait<T>::size); |
49 | | - // return unpack_flags_from_int<T>(i); |
50 | | - // } |
51 | | - |
52 | | - // enum class ContextType |
53 | | - // { |
54 | | - // Lift, |
55 | | - // Lower |
56 | | - // }; |
57 | | - |
58 | | - // template <Func T> |
59 | | - // inline core_func_t flatten(LiftLowerContext &cx, ContextType context) |
60 | | - // { |
61 | | - // std::vector<WasmValType> flat_params(ValTrait<T>::flat_params_types.begin(), ValTrait<T>::flat_params_types.end()); |
62 | | - // std::vector<WasmValType> flat_results(ValTrait<T>::flat_result_types.begin(), ValTrait<T>::flat_result_types.end()); |
63 | | - // // if (cx.opts.sync == true) |
64 | | - // { |
65 | | - // if (flat_params.size() > MAX_FLAT_PARAMS) |
66 | | - // { |
67 | | - // flat_params = {WasmValType::i32}; |
68 | | - // } |
69 | | - // if (flat_results.size() > MAX_FLAT_RESULTS) |
70 | | - // { |
71 | | - // switch (context) |
72 | | - // { |
73 | | - // case ContextType::Lift: |
74 | | - // flat_results = {WasmValType::i32}; |
75 | | - // break; |
76 | | - // case ContextType::Lower: |
77 | | - // flat_params.push_back(WasmValType::i32); |
78 | | - // flat_results = {}; |
79 | | - // } |
80 | | - // } |
81 | | - // } |
82 | | - // return {flat_params, flat_results}; |
83 | | - // } |
84 | | - |
85 | | - // template <Flags T> |
86 | | - // inline void store(LiftLowerContext &cx, const T &v, uint32_t ptr) |
87 | | - // { |
88 | | - // flags::store(cx, v, ptr); |
89 | | - // } |
90 | | - |
91 | | - // template <Flags T> |
92 | | - // inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
93 | | - // { |
94 | | - // return flags::lower_flat(cx, v); |
95 | | - // } |
96 | | - |
97 | | - // template <Flags T> |
98 | | - // inline T load(const LiftLowerContext &cx, uint32_t ptr) |
99 | | - // { |
100 | | - // return flags::load<T>(cx, ptr); |
101 | | - // } |
102 | | - |
103 | | - // template <Flags T> |
104 | | - // inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi) |
105 | | - // { |
106 | | - // return flags::lift_flat<T>(cx, vi); |
107 | | - // } |
| 11 | + enum class ContextType |
| 12 | + { |
| 13 | + Lift, |
| 14 | + Lower |
| 15 | + }; |
| 16 | + |
| 17 | + template <Flags T> |
| 18 | + inline int32_t pack_flags_into_int(const T &v) |
| 19 | + { |
| 20 | + return flags::pack_flags_into_int(v); |
| 21 | + } |
| 22 | + |
| 23 | + template <Flags T> |
| 24 | + inline void store(LiftLowerContext &cx, const T &v, offset ptr) |
| 25 | + { |
| 26 | + flags::store(cx, v, ptr); |
| 27 | + } |
| 28 | + |
| 29 | + template <Flags T> |
| 30 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
| 31 | + { |
| 32 | + return flags::lower_flat(cx, v); |
| 33 | + } |
| 34 | + |
| 35 | + template <Flags T> |
| 36 | + inline T unpack_flags_from_int(uint32_t value) |
| 37 | + { |
| 38 | + return flags::unpack_flags_from_int<T>(value); |
| 39 | + } |
| 40 | + |
| 41 | + template <Flags T> |
| 42 | + inline T load(const LiftLowerContext &cx, uint32_t ptr) |
| 43 | + { |
| 44 | + return flags::load<T>(cx, ptr); |
| 45 | + } |
| 46 | + |
| 47 | + template <Flags T> |
| 48 | + inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi) |
| 49 | + { |
| 50 | + return flags::lift_flat<T>(cx, vi); |
| 51 | + } |
| 52 | + |
| 53 | + template <Func T> |
| 54 | + inline core_func_t flatten(const CanonicalOptions &opts, ContextType context) |
| 55 | + { |
| 56 | + using params_trait = ValTrait<typename ValTrait<T>::params_t>; |
| 57 | + using result_trait = ValTrait<typename ValTrait<T>::result_t>; |
| 58 | + |
| 59 | + WasmValTypeVector flat_params(params_trait::flat_types.begin(), params_trait::flat_types.end()); |
| 60 | + WasmValTypeVector flat_results(result_trait::flat_types.begin(), result_trait::flat_types.end()); |
| 61 | + |
| 62 | + const size_t raw_param_count = flat_params.size(); |
| 63 | + const size_t raw_result_count = flat_results.size(); |
| 64 | + |
| 65 | + auto pointer_type = []() -> WasmValTypeVector |
| 66 | + { |
| 67 | + return {WasmValType::i32}; |
| 68 | + }; |
| 69 | + |
| 70 | + if (opts.sync) |
| 71 | + { |
| 72 | + if (raw_param_count > MAX_FLAT_PARAMS) |
| 73 | + { |
| 74 | + flat_params = pointer_type(); |
| 75 | + } |
| 76 | + |
| 77 | + if (raw_result_count > MAX_FLAT_RESULTS) |
| 78 | + { |
| 79 | + if (context == ContextType::Lift) |
| 80 | + { |
| 81 | + flat_results = pointer_type(); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + flat_params.push_back(WasmValType::i32); |
| 86 | + flat_results.clear(); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + if (context == ContextType::Lift) |
| 93 | + { |
| 94 | + if (raw_param_count > MAX_FLAT_PARAMS) |
| 95 | + { |
| 96 | + flat_params = pointer_type(); |
| 97 | + } |
| 98 | + |
| 99 | + if (opts.callback.has_value()) |
| 100 | + { |
| 101 | + flat_results = pointer_type(); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + flat_results.clear(); |
| 106 | + } |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + if (raw_param_count > MAX_FLAT_ASYNC_PARAMS) |
| 111 | + { |
| 112 | + flat_params = pointer_type(); |
| 113 | + } |
| 114 | + |
| 115 | + if (raw_result_count > 0) |
| 116 | + { |
| 117 | + flat_params.push_back(WasmValType::i32); |
| 118 | + } |
| 119 | + |
| 120 | + flat_results = pointer_type(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return {std::move(flat_params), std::move(flat_results)}; |
| 125 | + } |
108 | 126 | } |
109 | 127 | } |
110 | 128 | #endif |
0 commit comments