IR生成文件即IRGenxx设计

This commit is contained in:
jing
2025-12-29 20:30:16 +08:00
parent e1c1f2a40d
commit bb7f42e06e
7 changed files with 166 additions and 0 deletions

50
src/irgen/IRGen.h Normal file
View 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);