fix(irgen): 规范irgen实现
This commit is contained in:
@@ -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_);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user