fix(irgen): 规范irgen实现

This commit is contained in:
jing
2026-03-11 01:15:13 +08:00
parent 11f192cd10
commit a44a409d16
7 changed files with 51 additions and 44 deletions

View File

@@ -53,47 +53,47 @@ void IRPrinter::Print(const Module& module) {
for (const auto& func : module.functions()) {
std::cout << "define " << TypeToString(*func->type()) << " @"
<< func->name() << "() {\n";
const auto* bb = func->entry();
if (!bb) {
std::cout << "}\n";
continue;
}
std::cout << "entry:\n";
for (const auto& instPtr : bb->instructions()) {
const auto* inst = instPtr.get();
switch (inst->opcode()) {
case Opcode::Add:
case Opcode::Sub:
case Opcode::Mul: {
auto* bin = static_cast<const BinaryInst*>(inst);
std::cout << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
<< " " << TypeToString(*bin->lhs()->type()) << " "
<< ValueToString(bin->lhs()) << ", "
<< ValueToString(bin->rhs()) << "\n";
break;
}
case Opcode::Alloca: {
auto* alloca = static_cast<const AllocaInst*>(inst);
std::cout << " " << alloca->name() << " = alloca i32\n";
break;
}
case Opcode::Load: {
auto* load = static_cast<const LoadInst*>(inst);
std::cout << " " << load->name() << " = load i32, i32* "
<< ValueToString(load->ptr()) << "\n";
break;
}
case Opcode::Store: {
auto* store = static_cast<const StoreInst*>(inst);
std::cout << " store i32 " << ValueToString(store->value()) << ", i32* "
<< ValueToString(store->ptr()) << "\n";
break;
}
case Opcode::Ret: {
auto* ret = static_cast<const ReturnInst*>(inst);
std::cout << " ret " << TypeToString(*ret->value()->type()) << " "
<< ValueToString(ret->value()) << "\n";
break;
for (const auto& bb : func->blocks()) {
if (!bb) {
continue;
}
std::cout << bb->name() << ":\n";
for (const auto& instPtr : bb->instructions()) {
const auto* inst = instPtr.get();
switch (inst->opcode()) {
case Opcode::Add:
case Opcode::Sub:
case Opcode::Mul: {
auto* bin = static_cast<const BinaryInst*>(inst);
std::cout << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
<< " " << TypeToString(*bin->lhs()->type()) << " "
<< ValueToString(bin->lhs()) << ", "
<< ValueToString(bin->rhs()) << "\n";
break;
}
case Opcode::Alloca: {
auto* alloca = static_cast<const AllocaInst*>(inst);
std::cout << " " << alloca->name() << " = alloca i32\n";
break;
}
case Opcode::Load: {
auto* load = static_cast<const LoadInst*>(inst);
std::cout << " " << load->name() << " = load i32, i32* "
<< ValueToString(load->ptr()) << "\n";
break;
}
case Opcode::Store: {
auto* store = static_cast<const StoreInst*>(inst);
std::cout << " store i32 " << ValueToString(store->value()) << ", i32* "
<< ValueToString(store->ptr()) << "\n";
break;
}
case Opcode::Ret: {
auto* ret = static_cast<const ReturnInst*>(inst);
std::cout << " ret " << TypeToString(*ret->value()->type()) << " "
<< ValueToString(ret->value()) << "\n";
break;
}
}
}
}

View File

@@ -5,8 +5,6 @@
namespace ir {
ConstantInt::ConstantInt(int v) : Value(Type::Int32(), ""), value_(v) {
set_name(std::to_string(v));
}
ConstantInt::ConstantInt(int v) : Value(Type::Int32(), ""), value_(v) {}
} // namespace ir