style(sema): 规范符号表代码位置

This commit is contained in:
jing
2026-03-11 23:29:17 +08:00
parent 0e5a75eaf3
commit 2737ebd9fc
6 changed files with 23 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
// 将语法树翻译为极简 IR。
// 将语法树翻译为 IR。
// 实现拆分在 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
#pragma once
@@ -11,12 +11,6 @@
#include "ir/IR.h"
#include "sem/Sema.h"
namespace antlr4 {
namespace tree {
class ParseTree;
}
} // namespace antlr4
namespace ir {
class Module;
class Function;

View File

@@ -22,7 +22,7 @@ ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
ir::Value* acc = GenPrimary(*terms[0]);
for (size_t i = 1; i < terms.size(); ++i) {
ir::Value* rhs = GenPrimary(*terms[i]);
std::string name = ir::DefaultContext().NextTemp();
std::string name = module_.context().NextTemp();
acc = builder_.CreateBinary(ir::Opcode::Add, acc, rhs, name);
}
return acc;
@@ -30,7 +30,7 @@ ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
if (primary.Number()) {
return ir::DefaultContext().GetConstInt(std::stoi(primary.Number()->getText()));
return builder_.CreateConstInt(std::stoi(primary.Number()->getText()));
}
if (primary.Ident()) {
auto* decl = sema_.ResolveVarUse(&primary);
@@ -43,7 +43,7 @@ ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
throw std::runtime_error("[irgen] 变量声明缺少存储槽位: " +
primary.Ident()->getText());
}
return builder_.CreateLoad(it->second, ir::DefaultContext().NextTemp());
return builder_.CreateLoad(it->second, module_.context().NextTemp());
}
if (primary.exp()) {
return GenExpr(*primary.exp());

View File

@@ -41,8 +41,7 @@ void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
}
func_ = module_.CreateFunction(
func.Ident()->getText(),
std::make_shared<ir::Type>(ir::Type::Kind::Int32));
func.Ident()->getText(), module_.context().Int32());
builder_.SetInsertPoint(func_->entry());
storage_map_.clear();