IR生成文件即IRGenxx设计
This commit is contained in:
50
src/irgen/IRGen.h
Normal file
50
src/irgen/IRGen.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// 将 AST 翻译为极简 IR。
|
||||||
|
// 这里提供一个可运行的最小 IR 生成骨架,并把实现拆分到 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
|
||||||
|
// 同学可以在对应 .cpp 内进一步完善更多语义。
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "ir/IR.h"
|
||||||
|
|
||||||
|
namespace ast {
|
||||||
|
struct CompUnit;
|
||||||
|
struct Block;
|
||||||
|
struct VarDecl;
|
||||||
|
struct Stmt;
|
||||||
|
struct Expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
ir::Module& module_;
|
||||||
|
ir::Function* func_;
|
||||||
|
ir::IRBuilder builder_;
|
||||||
|
std::unordered_map<std::string, ir::Value*> locals_;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast);
|
||||||
@@ -2,3 +2,28 @@
|
|||||||
// - 处理全局变量与局部变量声明
|
// - 处理全局变量与局部变量声明
|
||||||
// - 处理数组初始化、空间分配与初值生成等
|
// - 处理数组初始化、空间分配与初值生成等
|
||||||
|
|
||||||
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "ast/AstNodes.h"
|
||||||
|
#include "ir/IR.h"
|
||||||
|
|
||||||
|
void IRGenImpl::GenBlock(const ast::Block& block) {
|
||||||
|
for (const auto& decl : block.varDecls) {
|
||||||
|
GenVarDecl(*decl);
|
||||||
|
}
|
||||||
|
for (const auto& stmt : block.stmts) {
|
||||||
|
GenStmt(*stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRGenImpl::GenVarDecl(const ast::VarDecl& decl) {
|
||||||
|
ir::Value* init = nullptr;
|
||||||
|
if (decl.init) {
|
||||||
|
init = GenExpr(*decl.init);
|
||||||
|
} else {
|
||||||
|
init = ir::DefaultContext().GetConstInt(0);
|
||||||
|
}
|
||||||
|
locals_[decl.name] = init;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,19 @@
|
|||||||
|
// 这是一个“可跑”的最小 IR 生成示例,便于对照/调试。
|
||||||
// IR 生成驱动(Driver):
|
// IR 生成驱动(Driver):
|
||||||
// - 驱动 Visitor 遍历 AST,调度各子模块完成翻译
|
// - 驱动 Visitor 遍历 AST,调度各子模块完成翻译
|
||||||
// - 统一管理模块级翻译入口与上下文(Module/IRBuilder 等)
|
// - 统一管理模块级翻译入口与上下文(Module/IRBuilder 等)
|
||||||
// - 组织函数/语句/表达式/声明等翻译流程
|
// - 组织函数/语句/表达式/声明等翻译流程
|
||||||
|
|
||||||
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "ast/AstNodes.h"
|
||||||
|
#include "ir/IR.h"
|
||||||
|
|
||||||
|
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast) {
|
||||||
|
auto module = std::make_unique<ir::Module>();
|
||||||
|
IRGenImpl gen(*module);
|
||||||
|
gen.Gen(ast);
|
||||||
|
return module;
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,3 +2,35 @@
|
|||||||
// - 处理算术运算、比较、逻辑运算、函数调用等表达式
|
// - 处理算术运算、比较、逻辑运算、函数调用等表达式
|
||||||
// - 生成对应的 IR 指令并返回 SSA 值
|
// - 生成对应的 IR 指令并返回 SSA 值
|
||||||
|
|
||||||
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "ast/AstNodes.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);
|
||||||
|
}
|
||||||
|
if (auto var = dynamic_cast<const ast::VarExpr*>(&expr)) {
|
||||||
|
auto it = locals_.find(var->name);
|
||||||
|
if (it == locals_.end()) {
|
||||||
|
throw std::runtime_error("变量未找到: " + var->name);
|
||||||
|
}
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
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("不支持的表达式类型");
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,3 +2,25 @@
|
|||||||
// - 处理函数定义、参数列表与返回值翻译
|
// - 处理函数定义、参数列表与返回值翻译
|
||||||
// - 创建并填充对应的 IR Function 对象
|
// - 创建并填充对应的 IR Function 对象
|
||||||
|
|
||||||
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "ast/AstNodes.h"
|
||||||
|
#include "ir/IR.h"
|
||||||
|
|
||||||
|
IRGenImpl::IRGenImpl(ir::Module& module)
|
||||||
|
: module_(module),
|
||||||
|
func_(module_.CreateFunction("main", ir::Type::Int32())),
|
||||||
|
builder_(func_->entry()) {}
|
||||||
|
|
||||||
|
void IRGenImpl::Gen(const ast::CompUnit& ast) {
|
||||||
|
if (!ast.func || !ast.func->body) {
|
||||||
|
throw std::runtime_error("AST 不完整:缺少 main 定义");
|
||||||
|
}
|
||||||
|
GenBlock(*ast.func->body);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<ir::Module> IRGenImpl::TakeModule() {
|
||||||
|
return std::make_unique<ir::Module>(std::move(module_));
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,3 +2,18 @@
|
|||||||
// - 处理 if/while/return 等控制流构造
|
// - 处理 if/while/return 等控制流构造
|
||||||
// - 负责基本块创建、分支跳转与控制流收束
|
// - 负责基本块创建、分支跳转与控制流收束
|
||||||
|
|
||||||
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "ast/AstNodes.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);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw std::runtime_error("不支持的语句类型");
|
||||||
|
}
|
||||||
|
|||||||
7
src/utils/Log.h
Normal file
7
src/utils/Log.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// 轻量日志接口。
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#define LOG_INFO(msg) std::cerr << "[info] " << msg << "\n"
|
||||||
|
#define LOG_ERROR(msg) std::cerr << "[error] " << msg << "\n"
|
||||||
Reference in New Issue
Block a user