fix(arc): handle namespaces & hashes (#121)

This commit is contained in:
Xenxo Espasandín 2023-05-14 17:41:06 +02:00 committed by GitHub
parent 40f9e5a4b2
commit 900fe95462
4 changed files with 59 additions and 12 deletions

View File

@ -22,6 +22,7 @@ class decompiler
std::vector<std::string> locals_;
std::vector<param_type> params_;
std::stack<node::ptr> stack_;
std::string namespace_;
locjmp locs_;
bool in_waittill_;
bool retbool_;
@ -94,9 +95,10 @@ private:
auto process_expr_method_function(expr_function& exp, expr::ptr& obj) -> void;
auto process_expr_parameters(expr_parameters& exp) -> void;
auto process_expr_arguments(expr_arguments& exp) -> void;
auto process_expr_size(expr_size& exp) -> void;
auto process_expr_reference(expr_reference& exp) -> void;
auto process_expr_array(expr_array& exp) -> void;
auto process_expr_field(expr_field& exp) -> void;
auto process_expr_size(expr_size& exp) -> void;
auto process_expr_vector(expr_vector& exp) -> void;
auto process_expr_identifier(expr_identifier& exp) -> void;
};

View File

@ -260,6 +260,11 @@ auto context::opcode_enum(u16 id) const -> opcode
auto context::hash_id(std::string const& name) const -> u32
{
if (name.starts_with("_id_"))
{
return static_cast<u32>(std::stoul(name.substr(4), nullptr, 16));
}
if (props_ & props::hashids)
{
auto* str = name.data();

View File

@ -18,6 +18,7 @@ decompiler::decompiler(context const* ctx) : ctx_{ ctx }
auto decompiler::decompile(assembly const& data) -> program::ptr
{
program_ = program::make();
namespace_ = {};
for (auto it = data.includes.rbegin(); it != data.includes.rend(); it++)
{
@ -1998,6 +1999,17 @@ auto decompiler::process_function(decl_function& func) -> void
{
process_stmt_comp(*func.body);
process_expr_parameters(*func.params);
if (ctx_->props() & props::spaces)
{
if (namespace_ != func.space->value)
{
namespace_ = func.space->value;
program_->declarations.push_back(decl_namespace::make(func.loc(), expr_string::make(func.loc(), func.space->value)));
}
}
func.space->value = {};
}
auto decompiler::process_stmt(stmt& stm) -> void
@ -2354,6 +2366,9 @@ auto decompiler::process_expr(expr::ptr& exp) -> void
case node::expr_isdefined:
process_expr(exp->as<expr_isdefined>().value);
break;
case node::expr_reference:
process_expr_reference(exp->as<expr_reference>());
break;
case node::expr_array:
process_expr_array(exp->as<expr_array>());
break;
@ -2534,6 +2549,14 @@ auto decompiler::process_expr_call_pointer(expr_pointer& exp) -> void
auto decompiler::process_expr_call_function(expr_function& exp) -> void
{
process_expr_arguments(*exp.args);
if (ctx_->props() & props::spaces)
{
if (exp.path->value == namespace_)
{
exp.path->value = {};
}
}
}
auto decompiler::process_expr_method_pointer(expr_pointer& exp, expr::ptr& obj) -> void
@ -2547,6 +2570,14 @@ auto decompiler::process_expr_method_function(expr_function& exp, expr::ptr& obj
{
process_expr_arguments(*exp.args);
process_expr(obj);
if (ctx_->props() & props::spaces)
{
if (exp.path->value == namespace_)
{
exp.path->value = {};
}
}
}
auto decompiler::process_expr_parameters(expr_parameters& exp) -> void
@ -2600,9 +2631,15 @@ auto decompiler::process_expr_arguments(expr_arguments& exp) -> void
}
}
auto decompiler::process_expr_size(expr_size& exp) -> void
auto decompiler::process_expr_reference(expr_reference& exp) -> void
{
process_expr(exp.obj);
if (ctx_->props() & props::spaces)
{
if (exp.path->value == namespace_)
{
exp.path->value = {};
}
}
}
auto decompiler::process_expr_array(expr_array& exp) -> void
@ -2616,6 +2653,11 @@ auto decompiler::process_expr_field(expr_field& exp) -> void
process_expr(exp.obj);
}
auto decompiler::process_expr_size(expr_size& exp) -> void
{
process_expr(exp.obj);
}
auto decompiler::process_expr_vector(expr_vector& exp) -> void
{
process_expr(exp.z);

View File

@ -187,7 +187,11 @@ auto source::dump_program(program const& data) -> void
auto source::dump_include(include const& inc) -> void
{
if (ctx_->props() & props::size64)
fmt::format_to(std::back_inserter(buf_), "#using ");
else
fmt::format_to(std::back_inserter(buf_), "#include ");
dump_expr_path(*inc.path);
fmt::format_to(std::back_inserter(buf_), ";\n");
}
@ -562,13 +566,7 @@ auto source::dump_stmt_waittillframeend(stmt_waittillframeend const&) -> void
auto source::dump_stmt_waitrealtime(stmt_waitrealtime const& stm) -> void
{
if (stm.time->is<expr_float>() || stm.time->is<expr_integer>())
{
fmt::format_to(std::back_inserter(buf_), "waitrealtime ");
dump_expr(*stm.time);
fmt::format_to(std::back_inserter(buf_), ";");
}
else if (stm.time->is<expr_paren>())
if (stm.time->is<expr_paren>())
{
fmt::format_to(std::back_inserter(buf_), "waitrealtime");
dump_expr(*stm.time);
@ -1594,7 +1592,7 @@ auto source::dump_expr_string(expr_string const& exp) -> void
auto source::dump_expr_hash(expr_hash const& exp) -> void
{
fmt::format_to(std::back_inserter(buf_), "{}", utils::string::to_literal(exp.value));
fmt::format_to(std::back_inserter(buf_), "#{}", utils::string::to_literal(exp.value));
}
auto source::dump_expr_vector(expr_vector const& exp) -> void