From c0eacfd236154874677e45ae378a55aeff87c1d2 Mon Sep 17 00:00:00 2001 From: PuqiAR Date: Wed, 11 Mar 2026 21:51:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9Disassembler?= =?UTF-8?q?=E4=BD=BF=E7=94=A8CoreIO=E7=9A=84stream?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Bytecode/Disassembler.cpp | 27 ++++++++++++++------------- src/Bytecode/Disassembler.hpp | 5 +++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Bytecode/Disassembler.cpp b/src/Bytecode/Disassembler.cpp index 80248d4..fe4583a 100644 --- a/src/Bytecode/Disassembler.cpp +++ b/src/Bytecode/Disassembler.cpp @@ -4,26 +4,27 @@ */ #include + #include #include namespace Fig { - void Disassembler::DisassembleModule(const CompiledModule *module) + void Disassembler::DisassembleModule(const CompiledModule *module, std::ostream &stream) { if (!module) return; - std::cout << "--- Module Disassembly ---" << std::endl; + stream << "--- Module Disassembly ---" << std::endl; for (auto *proto : module->protos) { DisassembleProto(proto); } } - void Disassembler::DisassembleProto(const Proto *proto) + void Disassembler::DisassembleProto(const Proto *proto, std::ostream &stream) { if (!proto) return; - std::cout << std::format("\n--- Proto: {} (Regs: {}, Params: {}) ---\n", + stream << std::format("\n--- Proto: {} (Regs: {}, Params: {}) ---\n", proto->name, proto->maxRegisters, proto->numParams); for (size_t i = 0; i < proto->code.size(); ++i) @@ -32,46 +33,46 @@ namespace Fig OpCode op = static_cast(inst & 0xFF); uint8_t a = (inst >> 8) & 0xFF; - std::cout << std::format("[{:04}] {:<12} ", i, magic_enum::enum_name(op)); + stream << std::format("[{:04}] {:<12} ", i, magic_enum::enum_name(op)); Format fmt = GetFormat(op); if (fmt == Format::ABC) { uint8_t b = (inst >> 16) & 0xFF; uint8_t c = (inst >> 24) & 0xFF; - std::cout << std::format("A:{:<3} B:{:<3} C:{:<3}", a, b, c); + stream << std::format("A:{:<3} B:{:<3} C:{:<3}", a, b, c); } else if (fmt == Format::ABx) { uint16_t bx = (inst >> 16) & 0xFFFF; - std::cout << std::format("A:{:<3} Bx:{:<5}", a, bx); + stream << std::format("A:{:<3} Bx:{:<5}", a, bx); // 自动关联常量池 if (op == OpCode::LoadK && bx < proto->constants.size()) { - std::cout << std::format(" ; {}", proto->constants[bx].ToString()); + stream << std::format(" ; {}", proto->constants[bx].ToString()); } } else if (fmt == Format::AsBx) { int16_t sbx = static_cast((inst >> 16) & 0xFFFF); - std::cout << std::format("A:{:<3} sBx:{:<5}", a, sbx); + stream << std::format("A:{:<3} sBx:{:<5}", a, sbx); // 计算跳转绝对地址 if (op == OpCode::Jmp || op == OpCode::JmpIfFalse) { - std::cout << std::format(" ; to [{:04}]", i + sbx + 1); + stream << std::format(" ; to [{:04}]", i + sbx + 1); } } - std::cout << "\n"; + stream << "\n"; } if (!proto->constants.empty()) { - std::cout << "Constants:\n"; + stream << "Constants:\n"; for (size_t i = 0; i < proto->constants.size(); ++i) { - std::cout << std::format(" [{}] {}\n", i, proto->constants[i].ToString()); + stream << std::format(" [{}] {}\n", i, proto->constants[i].ToString()); } } } diff --git a/src/Bytecode/Disassembler.hpp b/src/Bytecode/Disassembler.hpp index 43d3aba..133f5ca 100644 --- a/src/Bytecode/Disassembler.hpp +++ b/src/Bytecode/Disassembler.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace Fig { @@ -16,10 +17,10 @@ namespace Fig { public: // 反汇编整个模块 - static void DisassembleModule(const CompiledModule *module); + static void DisassembleModule(const CompiledModule *module, std::ostream & = CoreIO::GetStdOut()); // 反汇编单个函数原型 - static void DisassembleProto(const Proto *proto); + static void DisassembleProto(const Proto *proto, std::ostream & = CoreIO::GetStdOut()); private: enum class Format