summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWouter van Oortmerssen <aardappel@gmail.com>2019-10-24 08:08:25 -0700
committerGitHub <noreply@github.com>2019-10-24 08:08:25 -0700
commitffefb3ba2cdfca91fd20a8ac99c668f80140760d (patch)
treec9bc8b910dfb2623d3e800f7fdc747565d3d6b05 /src
parent73f141d49a8759993e0a79c3a0e86dd23784378c (diff)
downloadwabt-ffefb3ba2cdfca91fd20a8ac99c668f80140760d.tar.gz
wabt-ffefb3ba2cdfca91fd20a8ac99c668f80140760d.tar.bz2
wabt-ffefb3ba2cdfca91fd20a8ac99c668f80140760d.zip
wasm-decompile: Added initial tests. (#1195)
These are pretty minimal, more will be added as part of feature-PRs.
Diffstat (limited to 'src')
-rw-r--r--src/decompiler-ast.inl14
-rw-r--r--src/decompiler.cc26
2 files changed, 22 insertions, 18 deletions
diff --git a/src/decompiler-ast.inl b/src/decompiler-ast.inl
index b8a02bbc..d7a5284a 100644
--- a/src/decompiler-ast.inl
+++ b/src/decompiler-ast.inl
@@ -58,13 +58,14 @@ struct Node {
// Node specific annotations.
union {
LabelType lt; // br/br_if target.
+ const Var* var; // Decl/DeclInit.
};
Node()
: ntype(NodeType::Expr), etype(ExprType::Nop), e(nullptr),
- lt(LabelType::First) {}
- Node(NodeType ntype, ExprType etype, const Expr* e)
- : ntype(ntype), etype(etype), e(e), lt(LabelType::First) {}
+ var(nullptr) {}
+ Node(NodeType ntype, ExprType etype, const Expr* e, const Var* v)
+ : ntype(ntype), etype(etype), e(e), var(v) {}
// This value should really never be copied, only moved.
Node(Node&& rhs) = default;
@@ -90,7 +91,7 @@ struct AST {
void NewNode(NodeType ntype, ExprType etype, const Expr* e, Index nargs) {
assert(stack.size() >= nargs);
- Node n { ntype, etype, e };
+ Node n { ntype, etype, e, nullptr };
n.children.reserve(nargs);
std::move(stack.end() - nargs, stack.end(),
std::back_inserter(n.children));
@@ -100,7 +101,7 @@ struct AST {
template<ExprType T> void PreDecl(const VarExpr<T>& ve) {
stack.emplace(stack.begin() + pre_decl_insertion_point++,
- NodeType::Decl, ExprType::Nop, &ve);
+ NodeType::Decl, ExprType::Nop, nullptr, &ve.var);
}
template<ExprType T> void Get(const VarExpr<T>& ve, bool local) {
@@ -116,7 +117,8 @@ struct AST {
if (local && vars_defined.insert(ve.var.name()).second) {
if (stack_depth == 1) {
// Top level, declare it here.
- NewNode(NodeType::DeclInit, ExprType::Nop, &ve, 1);
+ NewNode(NodeType::DeclInit, ExprType::Nop, nullptr, 1);
+ stack.back().var = &ve.var;
return;
} else {
// Inside exp, better leave it as assignment exp and lift the decl out.
diff --git a/src/decompiler.cc b/src/decompiler.cc
index 76d9e873..33597df7 100644
--- a/src/decompiler.cc
+++ b/src/decompiler.cc
@@ -253,17 +253,13 @@ struct Decompiler {
return WrapNAry(args, "return ", "");
}
case NodeType::Decl: {
- // FIXME: this is icky.
- auto lg = reinterpret_cast<const VarExpr<ExprType::LocalGet> *>(n.e);
- ss << "var " << lg->var.name() << ":"
- << GetDecompTypeName(cur_func->GetLocalType(lg->var));
+ ss << "var " << n.var->name() << ":"
+ << GetDecompTypeName(cur_func->GetLocalType(*n.var));
return PushSStream();
}
case NodeType::DeclInit: {
- // FIXME: this is icky.
- auto lg = reinterpret_cast<const VarExpr<ExprType::LocalGet> *>(n.e);
- return WrapChild(args[0], "var " + lg->var.name() + ":"
- + GetDecompTypeName(cur_func->GetLocalType(lg->var)) + " = ", "");
+ return WrapChild(args[0], "var " + n.var->name() + ":"
+ + GetDecompTypeName(cur_func->GetLocalType(*n.var)) + " = ", "");
}
case NodeType::Expr:
// We're going to fall thru to the second switch to deal with ExprType.
@@ -279,12 +275,18 @@ struct Decompiler {
case Type::I64:
ss << static_cast<int64_t>(c.u64) << "L";
break;
- case Type::F32:
- ss << *reinterpret_cast<const float *>(&c.f32_bits) << "f";
+ case Type::F32: {
+ float f;
+ memcpy(&f, &c.f32_bits, sizeof(float));
+ ss << f << "f";
break;
- case Type::F64:
- ss << *reinterpret_cast<const double *>(&c.f64_bits) << "d";
+ }
+ case Type::F64: {
+ double d;
+ memcpy(&d, &c.f64_bits, sizeof(double));
+ ss << d << "d";
break;
+ }
case Type::V128:
ss << "V128"; // FIXME
break;