You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

#include "irgen/IRGen.h"
#include <stdexcept>
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
namespace {
void VerifyFunctionStructure(const ir::Function& func) {
// 当前 IRGen 仍是单入口、顺序生成;这里在生成结束后补一层块终结校验。
for (const auto& bb : func.GetBlocks()) {
if (!bb || !bb->HasTerminator()) {
throw std::runtime_error(
FormatError("irgen", "基本块未正确终结: " +
(bb ? bb->GetName() : std::string("<null>"))));
}
}
}
} // namespace
IRGenImpl::IRGenImpl(ir::Module& module, const SemanticContext& sema)
: module_(module),
sema_(sema),
func_(nullptr),
builder_(module.GetContext(), nullptr) {}
void IRGenImpl::Gen(SysYParser::CompUnitContext& cu) {
auto* func = cu.funcDef();
if (func && func->ID() && func->ID()->getText() == "main") {
GenFuncDef(*func);
return;
}
throw std::runtime_error(FormatError("irgen", "缺少 main 定义"));
}
void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
if (!func.blockStmt()) {
throw std::runtime_error(FormatError("irgen", "函数体为空"));
}
if (!func.ID()) {
throw std::runtime_error(FormatError("irgen", "缺少函数名"));
}
if (!func.funcType() || !func.funcType()->INT()) {
throw std::runtime_error(FormatError("irgen", "当前仅支持 int main"));
}
func_ = module_.CreateFunction(
func.ID()->getText(), module_.GetContext().Int32());
builder_.SetInsertPoint(func_->GetEntry());
storage_map_.clear();
GenBlock(*func.blockStmt());
// 语义正确性主要由 sema 保证,这里只兜底检查 IR 结构是否合法。
VerifyFunctionStructure(*func_);
}