fix(ast): 删掉ast结构

This commit is contained in:
jing
2026-03-09 12:42:12 +08:00
parent d4f4b77b1e
commit 03bd6d88e3
20 changed files with 147 additions and 594 deletions

View File

@@ -8,7 +8,6 @@ add_library(irgen STATIC
target_link_libraries(irgen PUBLIC
build_options
ast
sem
"${ANTLR4_RUNTIME_TARGET}"
ir
)

View File

@@ -1,45 +1,44 @@
// 将 AST 翻译为极简 IR。
// 这里提供一个可运行的最小 IR 生成骨架,并把实现拆分 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
// 同学可以在对应 .cpp 内进一步完善更多语义。
// 将 ANTLR parse tree 翻译为极简 IR。
// 实现拆分 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "SysYParser.h"
#include "ir/IR.h"
namespace ast {
struct CompUnit;
struct Block;
struct VarDecl;
struct Stmt;
struct Expr;
namespace antlr4 {
namespace tree {
class ParseTree;
}
} // namespace antlr4
namespace ir {
class Module;
class Function;
class IRBuilder;
class Value;
class ConstantInt;
}
// 最小 IR 生成器的内部实现,接口分散在各 IRGen*.cpp。
class IRGenImpl {
public:
explicit IRGenImpl(ir::Module& module);
void Gen(const ast::CompUnit& ast);
std::unique_ptr<ir::Module> TakeModule();
void Gen(SysYParser::CompUnitContext& cu);
private:
void GenBlock(const ast::Block& block);
void GenVarDecl(const ast::VarDecl& decl);
void GenStmt(const ast::Stmt& stmt);
ir::Value* GenExpr(const ast::Expr& expr);
void GenFuncDef(SysYParser::FuncDefContext& func);
void GenBlock(SysYParser::BlockContext& block);
void GenStmt(SysYParser::StmtContext& stmt);
void GenVarDecl(SysYParser::VarDeclContext& decl);
void GenReturnStmt(SysYParser::ReturnStmtContext& ret);
ir::Value* GenExpr(SysYParser::ExpContext& expr);
ir::Value* GenAddExpr(SysYParser::AddExpContext& add);
ir::Value* GenPrimary(SysYParser::PrimaryContext& primary);
ir::Module& module_;
ir::Function* func_;
@@ -47,4 +46,4 @@ class IRGenImpl {
std::unordered_map<std::string, ir::Value*> locals_;
};
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast);
std::unique_ptr<ir::Module> GenerateIR(antlr4::tree::ParseTree* tree);

View File

@@ -1,39 +1,36 @@
// 声明翻译模块:
// - 处理全局变量与局部变量声明
// - 处理数组初始化、空间分配与初值生成等
#include "irgen/IRGen.h"
#include <memory>
#include <stdexcept>
#include "ast/AstNodes.h"
#include "SysYParser.h"
#include "ir/IR.h"
void IRGenImpl::GenBlock(const ast::Block& block) {
// 先为所有局部变量创建栈槽,使 alloca 聚集在入口块前部。
for (const auto& decl : block.varDecls) {
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
locals_[decl->name] = slot;
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
for (auto* stmt : block.stmt()) {
if (stmt && stmt->varDecl()) {
const std::string name = stmt->varDecl()->Ident()->getText();
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
locals_[name] = slot;
}
}
for (const auto& decl : block.varDecls) {
GenVarDecl(*decl);
}
for (const auto& stmt : block.stmts) {
GenStmt(*stmt);
for (auto* stmt : block.stmt()) {
if (stmt) {
GenStmt(*stmt);
}
}
}
void IRGenImpl::GenVarDecl(const ast::VarDecl& decl) {
auto it = locals_.find(decl.name);
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
const std::string name = decl.Ident()->getText();
auto it = locals_.find(name);
if (it == locals_.end()) {
throw std::runtime_error("[irgen] 变量栈槽未创建: " + decl.name);
throw std::runtime_error("[irgen] 变量栈槽未创建: " + name);
}
ir::Value* init = nullptr;
if (decl.init) {
init = GenExpr(*decl.init);
if (decl.exp()) {
init = GenExpr(*decl.exp());
} else {
init = ir::DefaultContext().GetConstInt(0);
}

View File

@@ -1,19 +1,24 @@
// 这是一个“可跑”的最小 IR 生成示例,便于对照/调试。
// IR 生成驱动Driver
// - 驱动 Visitor 遍历 AST调度各子模块完成翻译
// - 统一管理模块级翻译入口与上下文Module/IRBuilder 等)
// - 组织函数/语句/表达式/声明等翻译流程
#include "irgen/IRGen.h"
#include <memory>
#include <stdexcept>
#include "ast/AstNodes.h"
#include "SysYParser.h"
#include "antlr4-runtime.h"
#include "ir/IR.h"
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast) {
std::unique_ptr<ir::Module> GenerateIR(antlr4::tree::ParseTree* tree) {
if (!tree) {
throw std::runtime_error("[irgen] parse tree 为空");
}
auto* cu = dynamic_cast<SysYParser::CompUnitContext*>(tree);
if (!cu) {
throw std::runtime_error("[irgen] parse tree 根节点不是 compUnit");
}
auto module = std::make_unique<ir::Module>();
IRGenImpl gen(*module);
gen.Gen(ast);
gen.Gen(*cu);
return module;
}

View File

@@ -1,37 +1,46 @@
// 表达式翻译模块:
// - 处理算术运算、比较、逻辑运算、函数调用等表达式
// - 生成对应的 IR 指令并返回 SSA 值
#include "irgen/IRGen.h"
#include <stdexcept>
#include "ast/AstNodes.h"
#include "SysYParser.h"
#include "ir/IR.h"
ir::Value* IRGenImpl::GenExpr(const ast::Expr& expr) {
if (auto num = dynamic_cast<const ast::NumberExpr*>(&expr)) {
return ir::DefaultContext().GetConstInt(num->value);
ir::Value* IRGenImpl::GenExpr(SysYParser::ExpContext& expr) {
if (!expr.addExp()) {
throw std::runtime_error("[irgen] 非法表达式");
}
if (auto var = dynamic_cast<const ast::VarExpr*>(&expr)) {
auto it = locals_.find(var->name);
if (it == locals_.end()) {
throw std::runtime_error("[irgen] 变量未找到: " + var->name);
}
std::string name = ir::DefaultContext().NextTemp();
return builder_.CreateLoad(it->second, name);
}
if (auto bin = dynamic_cast<const ast::BinaryExpr*>(&expr)) {
auto* lhs = GenExpr(*bin->lhs);
auto* rhs = GenExpr(*bin->rhs);
std::string name = ir::DefaultContext().NextTemp();
if (bin->op == ast::BinaryOp::Add) {
return builder_.CreateBinary(ir::Opcode::Add, lhs, rhs, name);
}
if (bin->op == ast::BinaryOp::Sub) {
// 当前子集只需要加法,减法复用 add 但保留分支,便于扩展
return builder_.CreateBinary(ir::Opcode::Add, lhs, rhs, name);
}
}
throw std::runtime_error("[irgen] 暂不支持的表达式类型");
return GenAddExpr(*expr.addExp());
}
ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
const auto& terms = add.primary();
if (terms.empty()) {
throw std::runtime_error("[irgen] 空加法表达式");
}
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();
acc = builder_.CreateBinary(ir::Opcode::Add, acc, rhs, name);
}
return acc;
}
ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
if (primary.Number()) {
return ir::DefaultContext().GetConstInt(std::stoi(primary.Number()->getText()));
}
if (primary.Ident()) {
const std::string name = primary.Ident()->getText();
auto it = locals_.find(name);
if (it == locals_.end()) {
throw std::runtime_error("[irgen] 变量未找到: " + name);
}
return builder_.CreateLoad(it->second, ir::DefaultContext().NextTemp());
}
if (primary.exp()) {
return GenExpr(*primary.exp());
}
throw std::runtime_error("[irgen] 暂不支持的 primary 形式");
}

View File

@@ -1,26 +1,28 @@
// 函数翻译模块:
// - 处理函数定义、参数列表与返回值翻译
// - 创建并填充对应的 IR Function 对象
#include "irgen/IRGen.h"
#include <stdexcept>
#include "ast/AstNodes.h"
#include "SysYParser.h"
#include "ir/IR.h"
IRGenImpl::IRGenImpl(ir::Module& module)
: module_(module),
func_(module_.CreateFunction("main", ir::Type::Int32())),
builder_(func_->entry()) {}
: module_(module), func_(nullptr), builder_(nullptr) {}
void IRGenImpl::Gen(const ast::CompUnit& ast) {
if (!ast.func || !ast.func->body) {
throw std::runtime_error("[irgen] AST 不完整:缺少 main 定义");
void IRGenImpl::Gen(SysYParser::CompUnitContext& cu) {
if (!cu.funcDef()) {
throw std::runtime_error("[irgen] 缺少 main 定义");
}
GenBlock(*ast.func->body);
GenFuncDef(*cu.funcDef());
}
std::unique_ptr<ir::Module> IRGenImpl::TakeModule() {
return std::make_unique<ir::Module>(std::move(module_));
void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
if (!func.block()) {
throw std::runtime_error("[irgen] 函数体为空");
}
func_ = module_.CreateFunction("main", ir::Type::Int32());
builder_.SetInsertPoint(func_->entry());
locals_.clear();
GenBlock(*func.block());
}

View File

@@ -1,19 +1,26 @@
// 语句翻译模块:
// - 处理 if/while/return 等控制流构造
// - 负责基本块创建、分支跳转与控制流收束
#include "irgen/IRGen.h"
#include <stdexcept>
#include "ast/AstNodes.h"
#include "SysYParser.h"
#include "ir/IR.h"
void IRGenImpl::GenStmt(const ast::Stmt& stmt) {
if (auto ret = dynamic_cast<const ast::ReturnStmt*>(&stmt)) {
ir::Value* v = GenExpr(*ret->value);
builder_.CreateRet(v);
void IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
if (stmt.varDecl()) {
GenVarDecl(*stmt.varDecl());
return;
}
if (stmt.returnStmt()) {
GenReturnStmt(*stmt.returnStmt());
return;
}
throw std::runtime_error("[irgen] 暂不支持的语句类型");
}
void IRGenImpl::GenReturnStmt(SysYParser::ReturnStmtContext& ret) {
if (!ret.exp()) {
throw std::runtime_error("[irgen] return 缺少表达式");
}
ir::Value* v = GenExpr(*ret.exp());
builder_.CreateRet(v);
}