Skip to content

Commit

Permalink
Support floating-point globals
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jul 31, 2020
1 parent 5cd90c0 commit f0597f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ std::tuple<bytes_ptr, Limits> allocate_memory(const std::vector<Memory>& module_
}
}

uint64_t eval_constant_expression(ConstantExpression expr,
const std::vector<ExternalGlobal>& imported_globals, const std::vector<uint64_t>& globals)
Value eval_constant_expression(ConstantExpression expr,
const std::vector<ExternalGlobal>& imported_globals, const std::vector<Value>& globals)
{
if (expr.kind == ConstantExpression::Kind::Constant)
return expr.value.constant;
Expand Down Expand Up @@ -486,7 +486,7 @@ std::unique_ptr<Instance> instantiate(Module module,
match_imported_globals(module.imported_global_types, imported_globals);

// Init globals
std::vector<uint64_t> globals;
std::vector<Value> globals;
globals.reserve(module.globalsec.size());
for (auto const& global : module.globalsec)
{
Expand Down
6 changes: 3 additions & 3 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct ExternalMemory

struct ExternalGlobal
{
uint64_t* value = nullptr;
Value* value = nullptr;
bool is_mutable = false;
};

Expand All @@ -77,12 +77,12 @@ struct Instance
// For these cases unique_ptr would either have a normal deleter or noop deleter respectively.
table_ptr table = {nullptr, [](table_elements*) {}};
Limits table_limits;
std::vector<uint64_t> globals;
std::vector<Value> globals;
std::vector<ExternalFunction> imported_functions;
std::vector<ExternalGlobal> imported_globals;

Instance(Module _module, bytes_ptr _memory, Limits _memory_limits, table_ptr _table,
Limits _table_limits, std::vector<uint64_t> _globals,
Limits _table_limits, std::vector<Value> _globals,
std::vector<ExternalFunction> _imported_functions,
std::vector<ExternalGlobal> _imported_globals)
: module(std::move(_module)),
Expand Down
16 changes: 11 additions & 5 deletions lib/fizzy/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ inline parser_result<GlobalType> parse_global_type(const uint8_t* pos, const uin
return {type, pos};
}

template <class T>
inline parser_result<T> parse_floating_point(const uint8_t* pos, const uint8_t* end)
{
if ((pos + sizeof(T)) > end)
throw parser_error{"unexpected EOF"};

return {*reinterpret_cast<const T*>(pos), pos + sizeof(T)};
}

inline parser_result<ConstantExpression> parse_constant_expression(
ValType expected_type, const uint8_t* pos, const uint8_t* end)
{
Expand Down Expand Up @@ -174,17 +183,14 @@ inline parser_result<ConstantExpression> parse_constant_expression(
break;
}
case Instr::f32_const:
// TODO: support this once floating points are implemented
result.kind = ConstantExpression::Kind::Constant;
result.value.constant = 0;
pos = skip(4, pos, end);
std::tie(result.value.constant, pos) = parse_floating_point<uint32_t>(pos, end);
constant_actual_type = ValType::f32;
break;
case Instr::f64_const:
// TODO: support this once floating points are implemented
result.kind = ConstantExpression::Kind::Constant;
result.value.constant = 0;
pos = skip(8, pos, end);
std::tie(result.value.constant, pos) = parse_floating_point<uint64_t>(pos, end);
constant_actual_type = ValType::f64;
break;
}
Expand Down

0 comments on commit f0597f0

Please sign in to comment.