refactor(dev): 统一 IR/MIR 接口命名风格

This commit is contained in:
Lane0218
2026-03-12 15:17:02 +08:00
parent f26551a896
commit b1155d8fa9
18 changed files with 170 additions and 167 deletions

View File

@@ -13,7 +13,7 @@
namespace ir {
static const char* TypeToString(const Type& ty) {
switch (ty.kind()) {
switch (ty.GetKind()) {
case Type::Kind::Void:
return "void";
case Type::Kind::Int32:
@@ -46,54 +46,55 @@ static const char* OpcodeToString(Opcode op) {
static std::string ValueToString(const Value* v) {
if (auto* ci = dynamic_cast<const ConstantInt*>(v)) {
return std::to_string(ci->value());
return std::to_string(ci->GetValue());
}
return v ? v->name() : "<null>";
return v ? v->GetName() : "<null>";
}
void IRPrinter::Print(const Module& module, std::ostream& os) {
for (const auto& func : module.functions()) {
os << "define " << TypeToString(*func->type()) << " @" << func->name()
for (const auto& func : module.GetFunctions()) {
os << "define " << TypeToString(*func->GetType()) << " @" << func->GetName()
<< "() {\n";
for (const auto& bb : func->blocks()) {
for (const auto& bb : func->GetBlocks()) {
if (!bb) {
continue;
}
os << bb->name() << ":\n";
for (const auto& instPtr : bb->instructions()) {
os << bb->GetName() << ":\n";
for (const auto& instPtr : bb->GetInstructions()) {
const auto* inst = instPtr.get();
switch (inst->opcode()) {
switch (inst->GetOpcode()) {
case Opcode::Add:
case Opcode::Sub:
case Opcode::Mul: {
auto* bin = static_cast<const BinaryInst*>(inst);
os << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
<< " " << TypeToString(*bin->lhs()->type()) << " "
<< ValueToString(bin->lhs()) << ", "
<< ValueToString(bin->rhs()) << "\n";
os << " " << bin->GetName() << " = "
<< OpcodeToString(bin->GetOpcode()) << " "
<< TypeToString(*bin->GetLhs()->GetType()) << " "
<< ValueToString(bin->GetLhs()) << ", "
<< ValueToString(bin->GetRhs()) << "\n";
break;
}
case Opcode::Alloca: {
auto* alloca = static_cast<const AllocaInst*>(inst);
os << " " << alloca->name() << " = alloca i32\n";
os << " " << alloca->GetName() << " = alloca i32\n";
break;
}
case Opcode::Load: {
auto* load = static_cast<const LoadInst*>(inst);
os << " " << load->name() << " = load i32, i32* "
<< ValueToString(load->ptr()) << "\n";
os << " " << load->GetName() << " = load i32, i32* "
<< ValueToString(load->GetPtr()) << "\n";
break;
}
case Opcode::Store: {
auto* store = static_cast<const StoreInst*>(inst);
os << " store i32 " << ValueToString(store->value()) << ", i32* "
<< ValueToString(store->ptr()) << "\n";
os << " store i32 " << ValueToString(store->GetValue())
<< ", i32* " << ValueToString(store->GetPtr()) << "\n";
break;
}
case Opcode::Ret: {
auto* ret = static_cast<const ReturnInst*>(inst);
os << " ret " << TypeToString(*ret->value()->type()) << " "
<< ValueToString(ret->value()) << "\n";
os << " ret " << TypeToString(*ret->GetValue()->GetType()) << " "
<< ValueToString(ret->GetValue()) << "\n";
break;
}
}