This commit is contained in:
jing
2026-03-12 00:57:57 +08:00
26 changed files with 156 additions and 339 deletions

View File

@@ -4,6 +4,7 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
for (auto* item : block.blockItem()) {
@@ -24,7 +25,7 @@ bool IRGenImpl::GenBlockItem(SysYParser::BlockItemContext& item) {
if (item.stmt()) {
return GenStmt(*item.stmt());
}
throw std::runtime_error("[irgen] 暂不支持的 blockItem 类型");
throw std::runtime_error(FormatError("irgen", "暂不支持的语句或声明"));
}
void IRGenImpl::GenDecl(SysYParser::DeclContext& decl) {
@@ -32,12 +33,12 @@ void IRGenImpl::GenDecl(SysYParser::DeclContext& decl) {
GenVarDecl(*decl.varDecl());
return;
}
throw std::runtime_error("[irgen] 暂不支持的声明类型");
throw std::runtime_error(FormatError("irgen", "暂不支持的声明类型"));
}
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
if (storage_map_.find(&decl) != storage_map_.end()) {
throw std::runtime_error("[irgen] 声明重复生成存储槽位");
throw std::runtime_error(FormatError("irgen", "声明重复生成存储槽位"));
}
auto* slot = builder_.CreateAllocaI32(module_.context().NextTemp());
storage_map_[&decl] = slot;

View File

@@ -1,10 +1,10 @@
#include "irgen/IRGen.h"
#include <memory>
#include <stdexcept>
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
std::unique_ptr<ir::Module> GenerateIR(SysYParser::CompUnitContext& tree,
const SemanticContext& sema) {

View File

@@ -4,10 +4,11 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
ir::Value* IRGenImpl::GenExpr(SysYParser::ExpContext& expr) {
if (!expr.addExp()) {
throw std::runtime_error("[irgen] 非法表达式");
throw std::runtime_error(FormatError("irgen", "非法表达式"));
}
return GenAddExpr(*expr.addExp());
}
@@ -16,7 +17,7 @@ ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
// 当前表达式层次仍是最小实现,直接贴合 addExp -> primary 的语法形状。
const auto& terms = add.primary();
if (terms.empty()) {
throw std::runtime_error("[irgen] 空加法表达式");
throw std::runtime_error(FormatError("irgen", "空加法表达式"));
}
ir::Value* acc = GenPrimary(*terms[0]);
@@ -35,18 +36,20 @@ ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
if (primary.Ident()) {
auto* decl = sema_.ResolveVarUse(&primary);
if (!decl) {
throw std::runtime_error("[irgen] 变量使用缺少语义绑定: " +
primary.Ident()->getText());
throw std::runtime_error(
FormatError("irgen",
"变量使用缺少语义绑定: " + primary.Ident()->getText()));
}
auto it = storage_map_.find(decl);
if (it == storage_map_.end()) {
throw std::runtime_error("[irgen] 变量声明缺少存储槽位: " +
primary.Ident()->getText());
throw std::runtime_error(
FormatError("irgen",
"变量声明缺少存储槽位: " + primary.Ident()->getText()));
}
return builder_.CreateLoad(it->second, module_.context().NextTemp());
}
if (primary.exp()) {
return GenExpr(*primary.exp());
}
throw std::runtime_error("[irgen] 暂不支持的 primary 形式");
throw std::runtime_error(FormatError("irgen", "暂不支持的表达式形式"));
}

View File

@@ -4,6 +4,7 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
namespace {
@@ -11,8 +12,9 @@ void VerifyFunctionStructure(const ir::Function& func) {
// 当前 IRGen 仍是单入口、顺序生成;这里在生成结束后补一层块终结校验。
for (const auto& bb : func.blocks()) {
if (!bb || !bb->HasTerminator()) {
throw std::runtime_error("[irgen] 基本块未正确终结: " +
(bb ? bb->name() : std::string("<null>")));
throw std::runtime_error(
FormatError("irgen", "基本块未正确终结: " +
(bb ? bb->name() : std::string("<null>"))));
}
}
}
@@ -27,17 +29,17 @@ IRGenImpl::IRGenImpl(ir::Module& module, const SemanticContext& sema)
void IRGenImpl::Gen(SysYParser::CompUnitContext& cu) {
if (!cu.funcDef()) {
throw std::runtime_error("[irgen] 缺少 main 定义");
throw std::runtime_error(FormatError("irgen", "缺少 main 定义"));
}
GenFuncDef(*cu.funcDef());
}
void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
if (!func.block()) {
throw std::runtime_error("[irgen] 函数体为空");
throw std::runtime_error(FormatError("irgen", "函数体为空"));
}
if (!func.Ident()) {
throw std::runtime_error("[irgen] 缺少函数名");
throw std::runtime_error(FormatError("irgen", "缺少函数名"));
}
func_ = module_.CreateFunction(

View File

@@ -4,18 +4,19 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
bool IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
if (stmt.returnStmt()) {
GenReturnStmt(*stmt.returnStmt());
return true;
}
throw std::runtime_error("[irgen] 暂不支持的语句类型");
throw std::runtime_error(FormatError("irgen", "暂不支持的语句类型"));
}
void IRGenImpl::GenReturnStmt(SysYParser::ReturnStmtContext& ret) {
if (!ret.exp()) {
throw std::runtime_error("[irgen] return 缺少表达式");
throw std::runtime_error(FormatError("irgen", "return 缺少表达式"));
}
ir::Value* v = GenExpr(*ret.exp());
builder_.CreateRet(v);