fix(irgen): 规范irgen实现

master
jing 1 week ago
parent 11f192cd10
commit a44a409d16

@ -78,3 +78,7 @@ WS
COMMENT
: '//' ~[\r\n]* -> skip
;
BLOCK_COMMENT
: '/*' .*? '*/' -> skip
;

@ -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;
}
}
}
}

@ -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

@ -45,6 +45,7 @@ class IRGenImpl {
ir::Module& module_;
ir::Function* func_;
ir::IRBuilder builder_;
// 当前只维护函数级局部变量表;若后续引入嵌套块作用域,需要改成作用域栈。
std::unordered_map<std::string, ir::Value*> locals_;
};

@ -9,6 +9,7 @@ void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
for (auto* item : block.blockItem()) {
if (item) {
if (GenBlockItem(*item)) {
// 当前语法要求 return 为块内最后一条语句;命中后可停止生成。
break;
}
}

@ -13,6 +13,7 @@ ir::Value* IRGenImpl::GenExpr(SysYParser::ExpContext& expr) {
}
ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
// 当前表达式层次仍是最小实现,直接贴合 addExp -> primary 的语法形状。
const auto& terms = add.primary();
if (terms.empty()) {
throw std::runtime_error("[irgen] 空加法表达式");

@ -8,6 +8,7 @@
namespace {
void VerifyFunctionStructure(const ir::Function& func) {
// 当前 IRGen 仍是单入口、顺序生成;这里在生成结束后补一层块终结校验。
for (const auto& bb : func.blocks()) {
if (!bb || !bb->HasTerminator()) {
throw std::runtime_error("[irgen] 基本块未正确终结: " +
@ -41,5 +42,6 @@ void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
locals_.clear();
GenBlock(*func.block());
// 语义正确性主要由 sema 保证,这里只兜底检查 IR 结构是否合法。
VerifyFunctionStructure(*func_);
}

Loading…
Cancel
Save