refactor: 修改Disassembler使用CoreIO的stream
This commit is contained in:
@@ -4,26 +4,27 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <Bytecode/Disassembler.hpp>
|
#include <Bytecode/Disassembler.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
|
||||||
namespace Fig
|
namespace Fig
|
||||||
{
|
{
|
||||||
void Disassembler::DisassembleModule(const CompiledModule *module)
|
void Disassembler::DisassembleModule(const CompiledModule *module, std::ostream &stream)
|
||||||
{
|
{
|
||||||
if (!module) return;
|
if (!module) return;
|
||||||
std::cout << "--- Module Disassembly ---" << std::endl;
|
stream << "--- Module Disassembly ---" << std::endl;
|
||||||
for (auto *proto : module->protos)
|
for (auto *proto : module->protos)
|
||||||
{
|
{
|
||||||
DisassembleProto(proto);
|
DisassembleProto(proto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Disassembler::DisassembleProto(const Proto *proto)
|
void Disassembler::DisassembleProto(const Proto *proto, std::ostream &stream)
|
||||||
{
|
{
|
||||||
if (!proto) return;
|
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);
|
proto->name, proto->maxRegisters, proto->numParams);
|
||||||
|
|
||||||
for (size_t i = 0; i < proto->code.size(); ++i)
|
for (size_t i = 0; i < proto->code.size(); ++i)
|
||||||
@@ -32,46 +33,46 @@ namespace Fig
|
|||||||
OpCode op = static_cast<OpCode>(inst & 0xFF);
|
OpCode op = static_cast<OpCode>(inst & 0xFF);
|
||||||
uint8_t a = (inst >> 8) & 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);
|
Format fmt = GetFormat(op);
|
||||||
if (fmt == Format::ABC)
|
if (fmt == Format::ABC)
|
||||||
{
|
{
|
||||||
uint8_t b = (inst >> 16) & 0xFF;
|
uint8_t b = (inst >> 16) & 0xFF;
|
||||||
uint8_t c = (inst >> 24) & 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)
|
else if (fmt == Format::ABx)
|
||||||
{
|
{
|
||||||
uint16_t bx = (inst >> 16) & 0xFFFF;
|
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())
|
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)
|
else if (fmt == Format::AsBx)
|
||||||
{
|
{
|
||||||
int16_t sbx = static_cast<int16_t>((inst >> 16) & 0xFFFF);
|
int16_t sbx = static_cast<int16_t>((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)
|
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())
|
if (!proto->constants.empty())
|
||||||
{
|
{
|
||||||
std::cout << "Constants:\n";
|
stream << "Constants:\n";
|
||||||
for (size_t i = 0; i < proto->constants.size(); ++i)
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <Bytecode/Bytecode.hpp>
|
#include <Bytecode/Bytecode.hpp>
|
||||||
#include <Compiler/Compiler.hpp>
|
#include <Compiler/Compiler.hpp>
|
||||||
|
#include <Core/CoreIO.hpp>
|
||||||
|
|
||||||
namespace Fig
|
namespace Fig
|
||||||
{
|
{
|
||||||
@@ -16,10 +17,10 @@ namespace Fig
|
|||||||
{
|
{
|
||||||
public:
|
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:
|
private:
|
||||||
enum class Format
|
enum class Format
|
||||||
|
|||||||
Reference in New Issue
Block a user