Skip to content

Commit 0a43071

Browse files
committed
fix: Finish function flattening utilities
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent d71dc7f commit 0a43071

File tree

5 files changed

+224
-108
lines changed

5 files changed

+224
-108
lines changed

include/cmcpp/flags.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef CMCPP_FLAGS_HPP
22
#define CMCPP_FLAGS_HPP
33

4+
#include <cstring>
5+
46
#include "context.hpp"
57
#include "integer.hpp"
68
#include "util.hpp"
@@ -12,14 +14,14 @@ namespace cmcpp
1214
template <Flags T>
1315
int32_t pack_flags_into_int(const T &v)
1416
{
15-
return v.to_ulong();
17+
return static_cast<int32_t>(v.to_ulong());
1618
}
1719

1820
template <Flags T>
1921
void store(LiftLowerContext &cx, const T &v, offset ptr)
2022
{
2123
auto i = pack_flags_into_int(v);
22-
std::memcpy(&cx.opts.memory[ptr], i, ValTrait<T>::size);
24+
std::memcpy(&cx.opts.memory[ptr], &i, ValTrait<T>::size);
2325
}
2426

2527
template <Flags T>
@@ -31,24 +33,25 @@ namespace cmcpp
3133
template <Flags T>
3234
T unpack_flags_from_int(const uint32_t &buff)
3335
{
34-
return {buff};
36+
T value{};
37+
using bitset_type = typename ValTrait<T>::inner_type;
38+
static_cast<bitset_type &>(value) = bitset_type(static_cast<unsigned long long>(buff));
39+
return value;
3540
}
3641

3742
template <Flags T>
3843
T load(const LiftLowerContext &cx, uint32_t ptr)
3944
{
40-
uint8_t buff[ValTrait<T>::size];
41-
std::memcpy(&buff, &cx.opts.memory[ptr], ValTrait<T>::size);
42-
return unpack_flags_from_int<T>(buff);
45+
uint32_t raw = 0;
46+
std::memcpy(&raw, &cx.opts.memory[ptr], ValTrait<T>::size);
47+
return unpack_flags_from_int<T>(raw);
4348
}
4449

4550
template <Flags T>
4651
T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
4752
{
4853
auto i = vi.next<int32_t>();
49-
uint8_t buff[ValTrait<T>::size];
50-
std::memcpy(&buff, &i, ValTrait<T>::size);
51-
return unpack_flags_from_int<T>(i);
54+
return unpack_flags_from_int<T>(static_cast<uint32_t>(i));
5255
}
5356
}
5457

include/cmcpp/func.hpp

Lines changed: 116 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,127 @@
22
#define CMCPP_FUNC_HPP
33

44
#include "context.hpp"
5+
#include "flags.hpp"
56

67
namespace cmcpp
78
{
89
namespace func
910
{
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+
}
108126
}
109127
}
110128
#endif

include/cmcpp/lower.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace cmcpp
6262
if (out_param == nullptr)
6363
{
6464
ptr = cx.opts.realloc(0, 0, ValTrait<tuple_type>::alignment, ValTrait<tuple_type>::size);
65-
flat_vals = {ptr};
65+
flat_vals = {static_cast<int32_t>(ptr)};
6666
}
6767
else
6868
{

include/cmcpp/traits.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ namespace cmcpp
809809
// Func --------------------------------------------------------------------
810810
constexpr uint32_t MAX_FLAT_PARAMS = 16;
811811
constexpr uint32_t MAX_FLAT_RESULTS = 1;
812+
constexpr uint32_t MAX_FLAT_ASYNC_PARAMS = 4;
812813

813814
template <typename>
814815
struct func_t_impl;

0 commit comments

Comments
 (0)