From 5504bef1899e66a182479e33ee42bbb3775be74d Mon Sep 17 00:00:00 2001 From: xensik Date: Mon, 21 Feb 2022 20:44:37 +0100 Subject: [PATCH] t6 disassemble discard align and #include fix --- src/t6/xsk/compiler.cpp | 49 +++++++++++++++++++------------------ src/t6/xsk/compiler.hpp | 2 +- src/t6/xsk/decompiler.cpp | 8 ++++++ src/t6/xsk/disassembler.cpp | 46 +++++++++++++++++++++++++--------- 4 files changed, 69 insertions(+), 36 deletions(-) diff --git a/src/t6/xsk/compiler.cpp b/src/t6/xsk/compiler.cpp index 933ac98f..3a98a5e2 100644 --- a/src/t6/xsk/compiler.cpp +++ b/src/t6/xsk/compiler.cpp @@ -101,6 +101,8 @@ void compiler::compile_program(const ast::program::ptr& program) { emit_declaration(declaration); } + + std::reverse(assembly_->includes.begin(), assembly_->includes.end()); } void compiler::emit_include(const ast::include::ptr& include) @@ -1247,10 +1249,10 @@ void compiler::emit_expr_call_pointer(const ast::expr_pointer::ptr& expr) void compiler::emit_expr_call_function(const ast::expr_function::ptr& expr) { - bool found = false; - - if(expr->path->value != "") + if (expr->path->value != "") { + bool found = false; + for (const auto& entry : assembly_->includes) { if (entry == expr->path->value) @@ -1259,11 +1261,11 @@ void compiler::emit_expr_call_function(const ast::expr_function::ptr& expr) break; } } - } - if(!found) - { - assembly_->includes.push_back(expr->path->value); + if (!found) + { + assembly_->includes.push_back(expr->path->value); + } } // TODO: resolve import calls path @@ -1323,10 +1325,10 @@ void compiler::emit_expr_method_pointer(const ast::expr_pointer::ptr& expr, cons void compiler::emit_expr_method_function(const ast::expr_function::ptr& expr, const ast::expr& obj) { - bool found = false; - - if(expr->path->value != "") + if (expr->path->value != "") { + bool found = false; + for (const auto& entry : assembly_->includes) { if (entry == expr->path->value) @@ -1335,11 +1337,11 @@ void compiler::emit_expr_method_function(const ast::expr_function::ptr& expr, co break; } } - } - if(!found) - { - assembly_->includes.push_back(expr->path->value); + if (!found) + { + assembly_->includes.push_back(expr->path->value); + } } // TODO: resolve import calls path @@ -1512,10 +1514,10 @@ void compiler::emit_expr_getnextarraykey(const ast::expr_getnextarraykey::ptr& e void compiler::emit_expr_reference(const ast::expr_reference::ptr& expr) { - bool found = false; - - if(expr->path->value != "") + if (expr->path->value != "") { + bool found = false; + for (const auto& entry : assembly_->includes) { if (entry == expr->path->value) @@ -1524,11 +1526,11 @@ void compiler::emit_expr_reference(const ast::expr_reference::ptr& expr) break; } } - } - if(!found) - { - assembly_->includes.push_back(expr->path->value); + if (!found) + { + assembly_->includes.push_back(expr->path->value); + } } // TODO: resolve import calls path @@ -1834,9 +1836,7 @@ void compiler::emit_expr_animation(const ast::expr_animation::ptr& expr) throw comp_error(expr->loc(), "trying to use animation without specified using animtree"); } - auto& tree = animtrees_.back(); - - emit_opcode(opcode::OP_GetAnimation, { tree.name, expr->value }); + emit_opcode(opcode::OP_GetAnimation, { animtrees_.back(), expr->value }); } void compiler::emit_expr_istring(const ast::expr_istring::ptr& expr) @@ -2394,6 +2394,7 @@ void compiler::insert_label(const std::string& name) case opcode::OP_Jump: case opcode::OP_JumpBack: case opcode::OP_Switch: + case opcode::OP_DevblockBegin: if (inst->data[0] == name) inst->data[0] = itr->second; break; diff --git a/src/t6/xsk/compiler.hpp b/src/t6/xsk/compiler.hpp index 0ef3c97f..fcb7322e 100644 --- a/src/t6/xsk/compiler.hpp +++ b/src/t6/xsk/compiler.hpp @@ -21,7 +21,7 @@ class compiler : public arc::compiler std::vector local_stack_; std::vector local_functions_; std::vector includes_; - std::vector animtrees_; + std::vector animtrees_; std::unordered_map constants_; std::vector blocks_; bool can_break_; diff --git a/src/t6/xsk/decompiler.cpp b/src/t6/xsk/decompiler.cpp index 3f8a693f..b87488e1 100644 --- a/src/t6/xsk/decompiler.cpp +++ b/src/t6/xsk/decompiler.cpp @@ -29,6 +29,14 @@ void decompiler::decompile(const std::string& file, const assembly::ptr& data) filename_ = file; program_ = std::make_unique(); + std::reverse(data->includes.begin(), data->includes.end()); + + for (const auto& inc : data->includes) + { + auto include = std::make_unique(std::make_unique(inc)); + program_->includes.push_back(std::move(include)); + } + for (const auto& func : data->functions) { auto name = std::make_unique(func->name); diff --git a/src/t6/xsk/disassembler.cpp b/src/t6/xsk/disassembler.cpp index f2d761f5..35598999 100644 --- a/src/t6/xsk/disassembler.cpp +++ b/src/t6/xsk/disassembler.cpp @@ -40,7 +40,7 @@ void disassembler::disassemble(const std::string& file, std::vector(data); assembly_ = std::make_unique(); - std::memset(&header_, 0 ,sizeof(header_)); + std::memset(&header_, 0, sizeof(header_)); exports_.clear(); imports_.clear(); strings_.clear(); @@ -191,19 +191,25 @@ void disassembler::disassemble(const std::string& file, std::vectorsize = (exports_[i+1]->offset - entry->offset) - 4; + entry->size = (exports_[i+1]->offset - entry->offset); - auto end_pos = entry->offset + entry->size; + auto end_pos = entry->offset + entry->size - 4; - for (auto j = 1; j < 4; j++) + script_->pos(end_pos); + + if (script_->read() == 0) { - script_->pos(end_pos - j); - auto op = script_->read(); + entry->size -= 4; + + for (auto j = 1; j < 4; j++) + { + script_->pos(end_pos - j); + auto op = script_->read(); + + if (op <= 0x01) break; - if (op == '\x00' || op == '\x01') - break; - else entry->size--; + } } } else @@ -243,13 +249,31 @@ void disassembler::disassemble_function(const function::ptr& func) inst->opcode = script_->read(); inst->size = opcode_size(inst->opcode); + if (size < 4 && inst->opcode >= std::uint8_t(opcode::OP_Count)) + { + func->instructions.pop_back(); + break; + } + this->disassemble_instruction(inst); size -= inst->size; } + + for (auto i = func->instructions.size() - 1; i >= 1; i--) + { + auto& inst = func->instructions.at(i); + auto& last = func->instructions.at(i-1); + + if (labels_.contains(inst->index)) + break; + + if (inst->opcode <= 0x01 && (last->opcode > 0x01)) + break; + + func->instructions.pop_back(); + } } - - void disassembler::disassemble_instruction(const instruction::ptr& inst) { switch (opcode(inst->opcode))