Skip to content

Commit b49b11d

Browse files
committed
feat: Remove one level of redirection in lower_flat_values
Minor code cleanup Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 66c0ec3 commit b49b11d

9 files changed

Lines changed: 108 additions & 112 deletions

File tree

include/cmcpp/float.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace cmcpp
1010
{
11-
1211
namespace float_
1312
{
1413
inline int32_t encode_float_as_i32(float32_t f)
@@ -22,16 +21,6 @@ namespace cmcpp
2221
return *reinterpret_cast<int64_t *>(&f);
2322
}
2423

25-
inline float32_t decode_i32_as_float(int32_t i)
26-
{
27-
return *reinterpret_cast<float32_t *>(&i);
28-
}
29-
30-
inline float64_t decode_i64_as_float(int64_t i)
31-
{
32-
return *reinterpret_cast<float64_t *>(&i);
33-
}
34-
3524
inline float32_t core_f32_reinterpret_i32(int32_t i)
3625
{
3726
float f;

include/cmcpp/func.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace cmcpp
77
{
8-
98
namespace func
109
{
1110
// template <Flags T>

include/cmcpp/integer.hpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace cmcpp
1212
{
13-
using offset = uint32_t;
14-
1513
namespace integer
1614
{
1715
template <typename T>
@@ -98,6 +96,40 @@ namespace cmcpp
9896
return integer::load<T>(cx, ptr);
9997
}
10098

99+
template <Boolean T>
100+
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v)
101+
{
102+
using WasmValType = WasmValTypeTrait<ValTrait<T>::flat_types[0]>::type;
103+
return {static_cast<WasmValType>(v)};
104+
}
105+
106+
template <Char T>
107+
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v)
108+
{
109+
using WasmValType = WasmValTypeTrait<ValTrait<T>::flat_types[0]>::type;
110+
return {static_cast<WasmValType>(char_to_i32(cx, v))};
111+
}
112+
113+
template <UnsignedInteger T>
114+
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v)
115+
{
116+
using WasmValType = WasmValTypeTrait<ValTrait<T>::flat_types[0]>::type;
117+
WasmValType fv = v;
118+
return {fv};
119+
}
120+
121+
template <Boolean T>
122+
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
123+
{
124+
return convert_int_to_bool(vi.next<int32_t>());
125+
}
126+
127+
template <Char T>
128+
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
129+
{
130+
return convert_i32_to_char(cx, vi.next<int32_t>());
131+
}
132+
101133
template <UnsignedInteger T>
102134
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
103135
{

include/cmcpp/lift.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88
namespace cmcpp
99
{
1010
template <Boolean T>
11-
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
12-
{
13-
return convert_int_to_bool(vi.next<int32_t>());
14-
}
11+
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi);
1512

1613
template <Char T>
17-
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi)
18-
{
19-
return convert_i32_to_char(cx, vi.next<int32_t>());
20-
}
14+
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi);
2115

2216
template <UnsignedInteger T>
2317
inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi);

include/cmcpp/list.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212

1313
namespace cmcpp
1414
{
15-
using offset = uint32_t;
16-
using size = uint32_t;
17-
1815
namespace list
1916
{
2017
template <typename T>

include/cmcpp/lower.hpp

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,13 @@
1717
namespace cmcpp
1818
{
1919
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);
2521

2622
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);
3224

3325
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);
4027

4128
template <SignedInteger T>
4229
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
@@ -65,54 +52,42 @@ namespace cmcpp
6552
template <Option T>
6653
inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v);
6754

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)
7057
{
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);
7460
WasmValVector flat_vals = {ptr};
7561
trap_if(cx, ptr != align_to(ptr, ValTrait<tuple_type>::alignment));
7662
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);
7864
return flat_vals;
7965
}
8066

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)
8369
{
84-
// cx.inst.may_leave=false;
8570
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;
8773
if (flat_types.size() > max_flat)
8874
{
89-
retVal = lower_heap_values(cx, vs);
75+
retVal = lower_heap_values(cx, std::forward<Ts>(vs)...);
9076
}
9177
else
9278
{
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;
9486
}
9587
// cx.inst.may_leave=true;
9688
return retVal;
9789
}
9890

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-
}
11691
}
11792

11893
#endif

include/cmcpp/traits.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace cmcpp
2626
{
2727
using float32_t = float;
2828
using float64_t = double;
29+
using offset = uint32_t;
30+
using bytes = uint32_t;
31+
using size = uint32_t;
2932

3033
enum class WasmValType : uint8_t
3134
{
@@ -728,9 +731,6 @@ namespace cmcpp
728731
concept Func = ValTrait<T>::type == ValType::Func;
729732

730733
// --------------------------------------------------------------------
731-
732-
using offset = uint32_t;
733-
using bytes = uint32_t;
734734
}
735735

736736
#endif

include/cmcpp/util.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ namespace cmcpp
4444
return static_cast<int32_t>(x);
4545
}
4646

47+
inline float32_t decode_i32_as_float(int32_t i)
48+
{
49+
return *reinterpret_cast<float32_t *>(&i);
50+
}
51+
52+
inline float64_t decode_i64_as_float(int64_t i)
53+
{
54+
return *reinterpret_cast<float64_t *>(&i);
55+
}
56+
4757
class CoreValueIter
4858
{
4959
mutable WasmValVector::const_iterator it;
@@ -70,6 +80,45 @@ namespace cmcpp
7080
}
7181
};
7282

83+
class CoerceValueIter : public CoreValueIter
84+
{
85+
const CoreValueIter &vi;
86+
WasmValTypeVector &flat_types;
87+
88+
public:
89+
CoerceValueIter(const CoreValueIter &vi, WasmValTypeVector &flat_types) : CoreValueIter({}), vi(vi), flat_types(flat_types)
90+
{
91+
}
92+
93+
virtual WasmVal next(const WasmValType &want) const override
94+
{
95+
auto have = flat_types.front();
96+
flat_types.erase(flat_types.begin());
97+
auto x = vi.next(have);
98+
if (have == WasmValType::i32 && want == WasmValType::f32)
99+
{
100+
return decode_i32_as_float(std::get<int32_t>(x));
101+
}
102+
else if (have == WasmValType::i64 && want == WasmValType::i32)
103+
{
104+
return wrap_i64_to_i32(std::get<int64_t>(x));
105+
}
106+
else if (have == WasmValType::i64 && want == WasmValType::f32)
107+
{
108+
return decode_i32_as_float(wrap_i64_to_i32(std::get<int64_t>(x)));
109+
}
110+
else if (have == WasmValType::i64 && want == WasmValType::f64)
111+
{
112+
return decode_i64_as_float(std::get<int64_t>(x));
113+
}
114+
else
115+
{
116+
assert(have == want);
117+
return x;
118+
}
119+
}
120+
};
121+
73122
}
74123

75124
#endif

include/cmcpp/variant.hpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,6 @@
1212

1313
namespace cmcpp
1414
{
15-
class CoerceValueIter : public CoreValueIter
16-
{
17-
const CoreValueIter &vi;
18-
WasmValTypeVector &flat_types;
19-
20-
public:
21-
CoerceValueIter(const CoreValueIter &vi, WasmValTypeVector &flat_types) : CoreValueIter({}), vi(vi), flat_types(flat_types)
22-
{
23-
}
24-
25-
virtual WasmVal next(const WasmValType &want) const override
26-
{
27-
auto have = flat_types.front();
28-
flat_types.erase(flat_types.begin());
29-
auto x = vi.next(have);
30-
if (have == WasmValType::i32 && want == WasmValType::f32)
31-
{
32-
return float_::decode_i32_as_float(std::get<int32_t>(x));
33-
}
34-
else if (have == WasmValType::i64 && want == WasmValType::i32)
35-
{
36-
return wrap_i64_to_i32(std::get<int64_t>(x));
37-
}
38-
else if (have == WasmValType::i64 && want == WasmValType::f32)
39-
{
40-
return float_::decode_i32_as_float(wrap_i64_to_i32(std::get<int64_t>(x)));
41-
}
42-
else if (have == WasmValType::i64 && want == WasmValType::f64)
43-
{
44-
return float_::decode_i64_as_float(std::get<int64_t>(x));
45-
}
46-
else
47-
{
48-
assert(have == want);
49-
return x;
50-
}
51-
}
52-
};
53-
5415
namespace variant
5516
{
5617
template <size_t N, Variant T>

0 commit comments

Comments
 (0)