From 900fe95462405ec33854d9aab449873e2d12d6e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xenxo=20Espasand=C3=ADn?= Date: Sun, 14 May 2023 17:41:06 +0200 Subject: [PATCH] fix(arc): handle namespaces & hashes (#121) --- include/xsk/arc/decompiler.hpp | 4 ++- src/arc/context.cpp | 5 ++++ src/arc/decompiler.cpp | 46 ++++++++++++++++++++++++++++++++-- src/arc/source.cpp | 16 ++++++------ 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/include/xsk/arc/decompiler.hpp b/include/xsk/arc/decompiler.hpp index 2757d733..bf9203c8 100644 --- a/include/xsk/arc/decompiler.hpp +++ b/include/xsk/arc/decompiler.hpp @@ -22,6 +22,7 @@ class decompiler std::vector locals_; std::vector params_; std::stack 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; }; diff --git a/src/arc/context.cpp b/src/arc/context.cpp index ea1872ad..6f0b254e 100644 --- a/src/arc/context.cpp +++ b/src/arc/context.cpp @@ -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(std::stoul(name.substr(4), nullptr, 16)); + } + if (props_ & props::hashids) { auto* str = name.data(); diff --git a/src/arc/decompiler.cpp b/src/arc/decompiler.cpp index 38e16f90..5d92e7e1 100644 --- a/src/arc/decompiler.cpp +++ b/src/arc/decompiler.cpp @@ -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().value); break; + case node::expr_reference: + process_expr_reference(exp->as()); + break; case node::expr_array: process_expr_array(exp->as()); 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); diff --git a/src/arc/source.cpp b/src/arc/source.cpp index 71a2b254..1173ee1f 100644 --- a/src/arc/source.cpp +++ b/src/arc/source.cpp @@ -187,7 +187,11 @@ auto source::dump_program(program const& data) -> void auto source::dump_include(include const& inc) -> void { - fmt::format_to(std::back_inserter(buf_), "#include "); + 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() || stm.time->is()) - { - fmt::format_to(std::back_inserter(buf_), "waitrealtime "); - dump_expr(*stm.time); - fmt::format_to(std::back_inserter(buf_), ";"); - } - else if (stm.time->is()) + if (stm.time->is()) { 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