Skip to content

Commit

Permalink
Value i32
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 2, 2020
1 parent 3dbd671 commit c52ead7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ std::unique_ptr<Instance> instantiate(Module module,
{
// Offset is validated to be i32, but it's used in 64-bit calculation below.
const uint64_t offset =
eval_constant_expression(data.offset, imported_globals, globals).i64;
eval_constant_expression(data.offset, imported_globals, globals).i32;

if (offset + data.init.size() > memory->size())
throw instantiate_error{"data segment is out of memory bounds"};
Expand All @@ -737,7 +737,7 @@ std::unique_ptr<Instance> instantiate(Module module,
{
// Offset is validated to be i32, but it's used in 64-bit calculation below.
const uint64_t offset =
eval_constant_expression(element.offset, imported_globals, globals).i64;
eval_constant_expression(element.offset, imported_globals, globals).i32;

if (offset + element.init.size() > table->size())
throw instantiate_error{"element segment is out of table bounds"};
Expand Down
9 changes: 5 additions & 4 deletions lib/fizzy/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace fizzy
{
union Value
{
uint32_t i32;
uint64_t i64;
float f32;
double f64;
Expand All @@ -25,11 +26,11 @@ union Value
/// We need to support {signed,unsigned} x {32,64} integers. However, due to uint64_t being
/// defined differently in different implementations we need to avoid the alias and provide
/// constructors for unsigned long and unsigned long long independently.
constexpr Value(unsigned int v) noexcept : i64{v} {}
constexpr Value(unsigned int v) noexcept : i32{v} {}
constexpr Value(unsigned long v) noexcept : i64{v} {}
constexpr Value(unsigned long long v) noexcept : i64{v} {}
constexpr Value(int64_t v) noexcept : i64{static_cast<uint64_t>(v)} {}
constexpr Value(int32_t v) noexcept : i64{static_cast<uint32_t>(v)} {}
constexpr Value(int32_t v) noexcept : i32{static_cast<uint32_t>(v)} {}

constexpr Value(float v) noexcept : f32{v} {}
constexpr Value(double v) noexcept : f64{v} {}
Expand All @@ -53,7 +54,7 @@ constexpr uint64_t Value::as<uint64_t>() const noexcept
template <>
constexpr uint32_t Value::as<uint32_t>() const noexcept
{
return static_cast<uint32_t>(i64);
return i32;
}

template <>
Expand All @@ -65,7 +66,7 @@ constexpr int64_t Value::as<int64_t>() const noexcept
template <>
constexpr int32_t Value::as<int32_t>() const noexcept
{
return static_cast<int32_t>(i64);
return static_cast<int32_t>(i32);
}

template <>
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/execute_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ TEST(execute_call, call_indirect)

for (const auto param : {0u, 1u, 2u})
{
constexpr uint64_t expected_results[]{3, 2, 1};
constexpr uint32_t expected_results[]{3, 2, 1};

EXPECT_THAT(execute(module, 5, {param}), Result(expected_results[param]));
}
Expand Down Expand Up @@ -163,7 +163,7 @@ TEST(execute_call, call_indirect_imported_table)

for (const auto param : {0u, 1u, 2u})
{
constexpr uint64_t expected_results[]{3, 2, 1};
constexpr uint32_t expected_results[]{3, 2, 1};

EXPECT_THAT(execute(*instance, 0, {param}), Result(expected_results[param]));
}
Expand Down
6 changes: 3 additions & 3 deletions test/unittests/execute_control_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ TEST(execute_control, br_if_with_result)

for (const auto param : {0u, 1u})
{
constexpr uint64_t expected_results[]{
constexpr uint32_t expected_results[]{
3, // br_if not taken, result: 1 xor 2 == 3.
2, // br_if taken, result: 2, remaining item dropped.
};
Expand All @@ -639,7 +639,7 @@ TEST(execute_control, br_if_out_of_function)

for (const auto param : {0u, 1u})
{
constexpr uint64_t expected_results[]{
constexpr uint32_t expected_results[]{
1, // br_if not taken.
2, // br_if taken.
};
Expand Down Expand Up @@ -720,7 +720,7 @@ TEST(execute_control, br_table)

for (const auto param : {0u, 1u, 2u, 3u, 4u, 5u})
{
constexpr uint64_t expected_results[]{103, 102, 101, 100, 104, 104};
constexpr uint32_t expected_results[]{103, 102, 101, 100, 104, 104};

EXPECT_THAT(execute(parse(bin), 0, {param}), Result(expected_results[param]));
}
Expand Down
8 changes: 5 additions & 3 deletions test/unittests/execute_numeric_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace fizzy::test;

namespace
{
ExecutionResult execute_unary_operation(Instr instr, uint64_t arg)
ExecutionResult execute_unary_operation(Instr instr, Value arg)
{
Module module;
// type is currently needed only to get arity of function, so exact value types don't matter
Expand Down Expand Up @@ -463,8 +463,10 @@ TEST(execute_numeric, i64_extend_i32_s_1)

TEST(execute_numeric, i64_extend_i32_u)
{
EXPECT_THAT(
execute_unary_operation(Instr::i64_extend_i32_u, 0xff000000), Result(0x00000000ff000000));
Value v;
v.i64 = 0xdeaddeaddeaddead;
v.i32 = 0xff000000;
EXPECT_THAT(execute_unary_operation(Instr::i64_extend_i32_u, v), Result(0x00000000ff000000));
}

TEST(execute_numeric, i64_clz)
Expand Down
5 changes: 2 additions & 3 deletions test/utils/asserts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MATCHER_P(Result, value, "") // NOLINT(readability-redundant-string-init)
if constexpr (std::is_floating_point_v<value_type>)
return arg.value.template as<value_type>() == fizzy::test::FP{value};
else // always check 64 bit of result for all integers, including 32-bit results
return arg.value.i64 == static_cast<std::make_unsigned_t<value_type>>(value);
return arg.value.template as<value_type>() == value;
}

#define EXPECT_THROW_MESSAGE(stmt, ex_type, expected) \
Expand Down Expand Up @@ -60,7 +60,6 @@ namespace fizzy::test
{
inline uint32_t as_uint32(fizzy::Value value)
{
EXPECT_EQ(value.i64 & 0xffffffff00000000, 0);
return static_cast<uint32_t>(value.i64);
return value.i32;
}
} // namespace fizzy::test

0 comments on commit c52ead7

Please sign in to comment.