Revert "可以处理生成加法的IR"

master
jing 3 months ago
parent e941cced9b
commit 8903bf73f9

@ -1,73 +1,35 @@
// SysY 子集语法:支持形如
// int main() { int a = 1; int b = 2; return a + b; }
// 的最小返回表达式编译。
// 后续需要自行添加
grammar SysY;
compUnit
: funcDef EOF
;
funcDef
: Int Main L_PAREN R_PAREN block
;
block
: L_BRACE stmt* R_BRACE
;
stmt
: varDecl
| returnStmt
;
varDecl
: Int Ident (Assign exp)? Semi
;
returnStmt
: Return exp Semi
;
exp
: addExp
;
// 说明:
// - 这是一个“最小可用”的 SysY.g4用于避免空文件导致的 ANTLR 解析报错。
// - 后续请按 SysY 语言规范逐步补全 lexer/parser 规则。
// - 本工程约定ANTLR 生成的 C++ 源码/头文件不进入仓库,统一生成到构建目录(例如 build/generated/antlr4/)。
addExp
: primary (AddOp primary)*
;
compilationUnit
: (statement)* EOF
;
primary
: Number
| Ident
| L_PAREN exp R_PAREN
;
statement
: 'return' expression? ';'
| ';'
;
Int : 'int';
Return : 'return';
Main : 'main';
expression
: IntegerLiteral
| Identifier
;
AddOp : '+';
Assign : '=';
Semi : ';';
L_PAREN : '(';
R_PAREN : ')';
L_BRACE : '{';
R_BRACE : '}';
// -------- lexer --------
Ident
: [a-zA-Z_][a-zA-Z_0-9]*
;
IntegerLiteral
: [0-9]+
;
Number
: [0-9]+
;
Identifier
: [a-zA-Z_] [a-zA-Z0-9_]*
;
WS
: [ \t\r\n]+ -> skip
;
Whitespace
: [ \t\r\n]+ -> skip
;
COMMENT
: '//' ~[\r\n]* -> skip
;

@ -1,3 +1,4 @@
// AST 节点简单实现:仅需包含头文件即可,析构函数默认生成。
// AST 节点定义与实现:
// - 表达式、语句、声明、函数、类型等节点
// - 支持后续阶段在节点上附加信息(类型、符号绑定、常量值等)
#include "ast/AstNodes.h"

@ -1,70 +0,0 @@
// Minimal AST definitions for the SysY subset used in this toy compiler.
#pragma once
#include <memory>
#include <string>
#include <vector>
namespace ast {
enum class BinaryOp { Add, Sub, Mul, Div };
struct Expr {
virtual ~Expr() = default;
};
struct NumberExpr : Expr {
int value{};
explicit NumberExpr(int v) : value(v) {}
};
struct VarExpr : Expr {
std::string name;
explicit VarExpr(std::string n) : name(std::move(n)) {}
};
struct BinaryExpr : Expr {
BinaryOp op;
std::shared_ptr<Expr> lhs;
std::shared_ptr<Expr> rhs;
BinaryExpr(BinaryOp op, std::shared_ptr<Expr> lhs, std::shared_ptr<Expr> rhs)
: op(op), lhs(std::move(lhs)), rhs(std::move(rhs)) {}
};
struct Stmt {
virtual ~Stmt() = default;
};
struct ReturnStmt : Stmt {
std::shared_ptr<Expr> value;
explicit ReturnStmt(std::shared_ptr<Expr> v) : value(std::move(v)) {}
};
struct VarDecl {
std::string name;
std::shared_ptr<Expr> init; // nullptr if no initializer
VarDecl(std::string n, std::shared_ptr<Expr> i)
: name(std::move(n)), init(std::move(i)) {}
};
struct Block {
std::vector<std::shared_ptr<VarDecl>> varDecls;
std::vector<std::shared_ptr<Stmt>> stmts;
};
struct FuncDef {
std::string name;
std::shared_ptr<Block> body;
FuncDef(std::string n, std::shared_ptr<Block> b)
: name(std::move(n)), body(std::move(b)) {}
};
struct CompUnit {
std::shared_ptr<FuncDef> func;
explicit CompUnit(std::shared_ptr<FuncDef> f) : func(std::move(f)) {}
};
// 调试打印
void PrintAST(const CompUnit& cu);
} // namespace ast

@ -1,72 +1,4 @@
// 简单 AST 调试打印,便于前端验证。
// AST 调试打印:
// - 以可读形式打印 AST 结构
// - 用于验证 AST 构建与语义分析结果,便于定位问题
#include "ast/AstNodes.h"
#include <iostream>
namespace ast {
static void PrintExpr(const Expr* expr);
static void PrintIndent(int depth) {
for (int i = 0; i < depth; ++i) std::cout << " ";
}
static void PrintExpr(const Expr* expr) {
if (auto num = dynamic_cast<const NumberExpr*>(expr)) {
std::cout << num->value;
} else if (auto var = dynamic_cast<const VarExpr*>(expr)) {
std::cout << var->name;
} else if (auto bin = dynamic_cast<const BinaryExpr*>(expr)) {
std::cout << "(";
PrintExpr(bin->lhs.get());
const char* op = "?";
switch (bin->op) {
case BinaryOp::Add:
op = "+";
break;
case BinaryOp::Sub:
op = "-";
break;
case BinaryOp::Mul:
op = "*";
break;
case BinaryOp::Div:
op = "/";
break;
}
std::cout << " " << op << " ";
PrintExpr(bin->rhs.get());
std::cout << ")";
}
}
void PrintAST(const CompUnit& cu) {
if (!cu.func) return;
std::cout << "func " << cu.func->name << " () {\n";
const auto& body = cu.func->body;
if (!body) {
std::cout << "}\n";
return;
}
for (const auto& decl : body->varDecls) {
PrintIndent(1);
std::cout << "var " << decl->name;
if (decl->init) {
std::cout << " = ";
PrintExpr(decl->init.get());
}
std::cout << ";\n";
}
for (const auto& stmt : body->stmts) {
if (auto ret = dynamic_cast<ReturnStmt*>(stmt.get())) {
PrintIndent(1);
std::cout << "return ";
PrintExpr(ret->value.get());
std::cout << ";\n";
}
}
std::cout << "}\n";
}
} // namespace ast

@ -1,34 +1,5 @@
// 调用 ANTLR 生成的 Lexer/Parser返回 parse tree。
#include "frontend/AntlrDriver.h"
// 前端解析驱动:
// - 读取源代码
// - 调用 ANTLR 生成的 lexer/parser 得到 parse tree
// - 对外提供“可用的解析入口”(语法正确性由测试保证)
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "SysYLexer.h"
#include "SysYParser.h"
#include "antlr4-runtime.h"
AntlrResult ParseFileWithAntlr(const std::string& path) {
std::ifstream fin(path);
if (!fin.is_open()) {
throw std::runtime_error("无法打开输入文件: " + path);
}
std::ostringstream ss;
ss << fin.rdbuf();
auto input = std::make_unique<antlr4::ANTLRInputStream>(ss.str());
auto lexer = std::make_unique<SysYLexer>(input.get());
auto tokens = std::make_unique<antlr4::CommonTokenStream>(lexer.get());
auto parser = std::make_unique<SysYParser>(tokens.get());
parser->removeErrorListeners();
auto tree = parser->compUnit();
AntlrResult result;
result.input = std::move(input);
result.lexer = std::move(lexer);
result.tokens = std::move(tokens);
result.parser = std::move(parser);
result.tree = tree;
return result;
}

@ -1,20 +0,0 @@
// 包装 ANTLR4提供简易的解析入口。
#pragma once
#include <memory>
#include <string>
#include "SysYLexer.h"
#include "SysYParser.h"
#include "antlr4-runtime.h"
struct AntlrResult {
std::unique_ptr<antlr4::ANTLRInputStream> input;
std::unique_ptr<SysYLexer> lexer;
std::unique_ptr<antlr4::CommonTokenStream> tokens;
std::unique_ptr<SysYParser> parser;
antlr4::tree::ParseTree* tree = nullptr; // owned by parser
};
// 解析指定文件,发生错误时抛出 std::runtime_error。
AntlrResult ParseFileWithAntlr(const std::string& path);

@ -1,114 +1,4 @@
// 将 parse tree 转换为 AST。
#include "frontend/AstBuilder.h"
// AST 构建:
// - 将 ANTLR parse tree 转换为 AST对应 src/ast/*
// - 在 AST 节点上保留必要的定位信息(可选,用于调试/日志)
#include <any>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include "SysYBaseVisitor.h"
#include "SysYParser.h"
#include "ast/AstNodes.h"
#include "antlr4-runtime.h"
namespace {
using ast::BinaryExpr;
using ast::BinaryOp;
using ast::Block;
using ast::CompUnit;
using ast::FuncDef;
using ast::NumberExpr;
using ast::ReturnStmt;
using ast::VarDecl;
using ast::VarExpr;
template <typename T>
T Take(std::any&& value) {
if (auto* ptr = std::any_cast<T>(&value)) {
return std::move(*ptr);
}
throw std::runtime_error("AST 构建失败:类型不匹配");
}
class Builder : public SysYBaseVisitor {
public:
std::any visitCompUnit(SysYParser::CompUnitContext* ctx) override {
auto func = Take<std::shared_ptr<FuncDef>>(visit(ctx->funcDef()));
return std::make_shared<CompUnit>(std::move(func));
}
std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override {
auto body = Take<std::shared_ptr<Block>>(visit(ctx->block()));
return std::make_shared<FuncDef>("main", std::move(body));
}
std::any visitBlock(SysYParser::BlockContext* ctx) override {
auto block = std::make_shared<Block>();
for (auto stmtCtx : ctx->stmt()) {
if (stmtCtx->varDecl()) {
block->varDecls.emplace_back(
Take<std::shared_ptr<VarDecl>>(visit(stmtCtx->varDecl())));
} else if (stmtCtx->returnStmt()) {
block->stmts.emplace_back(
Take<std::shared_ptr<ReturnStmt>>(visit(stmtCtx->returnStmt())));
}
}
return block;
}
std::any visitVarDecl(SysYParser::VarDeclContext* ctx) override {
std::shared_ptr<ast::Expr> init;
if (ctx->exp()) {
init = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
}
return std::make_shared<VarDecl>(ctx->Ident()->getText(), std::move(init));
}
std::any visitReturnStmt(SysYParser::ReturnStmtContext* ctx) override {
auto expr = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
return std::make_shared<ReturnStmt>(std::move(expr));
}
std::any visitExp(SysYParser::ExpContext* ctx) override {
return visit(ctx->addExp());
}
std::any visitAddExp(SysYParser::AddExpContext* ctx) override {
auto node = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(0)));
for (size_t i = 1; i < ctx->primary().size(); ++i) {
auto rhs = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(i)));
auto opToken = ctx->AddOp(i - 1);
BinaryOp op = BinaryOp::Add;
if (opToken->getText() == "-") op = BinaryOp::Sub;
node = std::make_shared<BinaryExpr>(op, std::move(node), std::move(rhs));
}
return node;
}
std::any visitPrimary(SysYParser::PrimaryContext* ctx) override {
if (ctx->Number()) {
std::shared_ptr<ast::Expr> expr =
std::make_shared<NumberExpr>(std::stoi(ctx->Number()->getText()));
return expr;
}
if (ctx->Ident()) {
std::shared_ptr<ast::Expr> expr =
std::make_shared<VarExpr>(ctx->Ident()->getText());
return expr;
}
return visit(ctx->exp());
}
};
} // namespace
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree) {
if (!tree) {
throw std::runtime_error("parse tree 为空");
}
Builder visitor;
auto result = visitor.visit(tree);
return Take<std::shared_ptr<ast::CompUnit>>(std::move(result));
}

@ -1,16 +0,0 @@
// 将 ANTLR parse tree 转换为内部 AST。
#pragma once
#include <memory>
namespace antlr4 {
namespace tree {
class ParseTree;
}
} // namespace antlr4
namespace ast {
struct CompUnit;
}
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree);

@ -1 +1,4 @@
#include "ir/IR.h"
// IR 基本块:
// - 保存指令序列
// - 维护或可计算前驱/后继关系,用于 CFG 分析与优化

@ -1 +1,4 @@
#include "ir/IR.h"
// IR 上下文:
// - 管理类型与常量的创建/复用
// - 保存字符串常量、符号等公共资源(按需要扩展)

@ -1,15 +1,4 @@
#include "ir/IR.h"
// IR Function
// - 保存参数列表、基本块列表
// - 记录函数属性/元信息(按需要扩展)
namespace ir {
Function::Function(std::string name)
: Value(Type::Int32(), std::move(name)),
entry_(std::make_unique<BasicBlock>("entry")) {}
void Function::EnsureEntry() {
if (!entry_) {
entry_ = std::make_unique<BasicBlock>("entry");
}
}
} // namespace ir

@ -1,144 +0,0 @@
// 极简 IR 定义:足以表示 int 返回 a+b。
#pragma once
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace ir {
class Type {
public:
enum class Kind { Void, Int32 };
explicit Type(Kind k) : kind_(k) {}
Kind kind() const { return kind_; }
static std::shared_ptr<Type> Void();
static std::shared_ptr<Type> Int32();
private:
Kind kind_;
};
class Value {
public:
Value(std::shared_ptr<Type> ty, std::string name)
: type_(std::move(ty)), name_(std::move(name)) {}
virtual ~Value() = default;
const std::shared_ptr<Type>& type() const { return type_; }
const std::string& name() const { return name_; }
void set_name(std::string n) { name_ = std::move(n); }
protected:
std::shared_ptr<Type> type_;
std::string name_;
};
class ConstantInt : public Value {
public:
explicit ConstantInt(int v);
int value() const { return value_; }
private:
int value_{};
};
enum class Opcode { Add, Sub, Mul, Div, Ret };
class Instruction : public Value {
public:
Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name = "")
: Value(std::move(ty), std::move(name)), opcode_(op) {}
Opcode opcode() const { return opcode_; }
private:
Opcode opcode_;
};
class BinaryInst : public Instruction {
public:
BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name);
Value* lhs() const { return lhs_; }
Value* rhs() const { return rhs_; }
private:
Value* lhs_;
Value* rhs_;
};
class ReturnInst : public Instruction {
public:
explicit ReturnInst(Value* val);
Value* value() const { return value_; }
private:
Value* value_;
};
class BasicBlock {
public:
explicit BasicBlock(std::string name) : name_(std::move(name)) {}
const std::string& name() const { return name_; }
const std::vector<std::unique_ptr<Instruction>>& instructions() const {
return instructions_;
}
template <typename T, typename... Args>
T* Append(Args&&... args) {
auto inst = std::make_unique<T>(std::forward<Args>(args)...);
auto* ptr = inst.get();
instructions_.push_back(std::move(inst));
return ptr;
}
private:
std::string name_;
std::vector<std::unique_ptr<Instruction>> instructions_;
};
class Function : public Value {
public:
explicit Function(std::string name);
BasicBlock* entry() { return entry_.get(); }
const BasicBlock* entry() const { return entry_.get(); }
void EnsureEntry();
private:
std::unique_ptr<BasicBlock> entry_;
};
class Module {
public:
Function* CreateFunction(const std::string& name);
const std::vector<std::unique_ptr<Function>>& functions() const {
return functions_;
}
private:
std::vector<std::unique_ptr<Function>> functions_;
};
class IRBuilder {
public:
explicit IRBuilder(BasicBlock* bb) : insertBlock_(bb) {}
void SetInsertPoint(BasicBlock* bb) { insertBlock_ = bb; }
BasicBlock* GetInsertBlock() const { return insertBlock_; }
ConstantInt* CreateConstInt(int v);
BinaryInst* CreateBinary(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
BinaryInst* CreateAdd(Value* lhs, Value* rhs, const std::string& name) {
return CreateBinary(Opcode::Add, lhs, rhs, name);
}
ReturnInst* CreateRet(Value* v);
private:
BasicBlock* insertBlock_;
};
class IRPrinter {
public:
void Print(const Module& module);
};
} // namespace ir

@ -1,19 +1,4 @@
#include "ir/IR.h"
// IR 构建工具:
// - 管理插入点(当前基本块/位置)
// - 提供创建各类指令的便捷接口,降低 IRGen 复杂度
namespace ir {
ConstantInt* IRBuilder::CreateConstInt(int v) {
// 常量不需要挂在基本块里,直接返回局部对象指针。
return new ConstantInt(v);
}
BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
const std::string& name) {
return insertBlock_->Append<BinaryInst>(op, Type::Int32(), lhs, rhs, name);
}
ReturnInst* IRBuilder::CreateRet(Value* v) {
return insertBlock_->Append<ReturnInst>(v);
}
} // namespace ir

@ -1,55 +1,4 @@
#include "ir/IR.h"
// IR 文本输出:
// - 将 IR 打印为 .ll 风格的文本
// - 支撑调试与测试对比diff
#include <iostream>
namespace ir {
static const char* OpcodeToString(Opcode op) {
switch (op) {
case Opcode::Add:
return "add";
case Opcode::Sub:
return "sub";
case Opcode::Mul:
return "mul";
case Opcode::Div:
return "div";
case Opcode::Ret:
return "ret";
}
return "?";
}
void IRPrinter::Print(const Module& module) {
for (const auto& func : module.functions()) {
std::cout << "define i32 @" << func->name() << "() {\n";
const auto* bb = func->entry();
if (!bb) {
std::cout << "}\n";
continue;
}
for (const auto& instPtr : bb->instructions()) {
const auto* inst = instPtr.get();
switch (inst->opcode()) {
case Opcode::Add:
case Opcode::Sub:
case Opcode::Mul:
case Opcode::Div: {
auto* bin = static_cast<const BinaryInst*>(inst);
std::cout << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
<< " " << bin->lhs()->name() << ", " << bin->rhs()->name()
<< "\n";
break;
}
case Opcode::Ret: {
auto* ret = static_cast<const ReturnInst*>(inst);
std::cout << " ret " << ret->value()->name() << "\n";
break;
}
}
}
std::cout << "}\n";
}
}
} // namespace ir

@ -1,12 +1,4 @@
#include "ir/IR.h"
// IR 指令体系:
// - 二元运算/比较、load/store、call、br/condbr、ret、phi、alloca 等
// - 指令操作数与结果类型管理,支持打印与优化
namespace ir {
BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
Value* rhs, std::string name)
: Instruction(op, std::move(ty), std::move(name)), lhs_(lhs), rhs_(rhs) {}
ReturnInst::ReturnInst(Value* val)
: Instruction(Opcode::Ret, Type::Void(), ""), value_(val) {}
} // namespace ir

@ -1,10 +1,4 @@
#include "ir/IR.h"
// IR Module
// - 保存全局变量与函数列表
// - 维护与目标相关的模块级信息(如需要)与符号表
namespace ir {
Function* Module::CreateFunction(const std::string& name) {
functions_.push_back(std::make_unique<Function>(name));
return functions_.back().get();
}
} // namespace ir

@ -1,16 +1,4 @@
// 极简类型系统:仅支持 void 与 i32。
#include "ir/IR.h"
// IR 类型系统:
// - i32/f32/void、指针、数组、函数类型等
// - 按 SysY 支持范围裁剪并逐步补齐
namespace ir {
std::shared_ptr<Type> Type::Void() {
static auto ty = std::make_shared<Type>(Kind::Void);
return ty;
}
std::shared_ptr<Type> Type::Int32() {
static auto ty = std::make_shared<Type>(Kind::Int32);
return ty;
}
} // namespace ir

@ -1,9 +1,4 @@
#include "ir/IR.h"
// SSA 值体系抽象:
// - 常量、参数、指令结果等统一为 Value
// - 提供类型信息与使用/被使用关系(按需要实现)
namespace ir {
ConstantInt::ConstantInt(int v) : Value(Type::Int32(), ""), value_(v) {
set_name(std::to_string(v));
}
} // namespace ir

@ -1 +1,4 @@
#include <vector>
// 支配树分析:
// - 构建/查询 Dominator Tree 及相关关系
// - 为 mem2reg、CFG 优化与循环分析提供基础能力

@ -1 +1,4 @@
#include <vector>
// 循环分析:
// - 识别循环结构与层级关系
// - 为后续优化(可选)提供循环信息

@ -1 +1,4 @@
#include <vector>
// CFG 简化:
// - 删除不可达块、合并空块、简化分支等
// - 改善 IR 结构,便于后续优化与后端生成

@ -1 +1,4 @@
#include <vector>
// IR 常量折叠:
// - 折叠可判定的常量表达式
// - 简化常量控制流分支(按实现范围裁剪)

@ -1 +1,4 @@
#include <vector>
// 死代码删除DCE
// - 删除无用指令与无用基本块
// - 通常与 CFG 简化配合使用

@ -1 +1,4 @@
#include <vector>
// Mem2RegSSA 构造):
// - 将局部变量的 alloca/load/store 提升为 SSA 形式
// - 插入 PHI 并重写使用,依赖支配树等分析

@ -1 +1,4 @@
#include <vector>
// IR Pass 管理:
// - 按优化级别组织优化 pipeline
// - 统一运行 pass、统计与调试输出按需要扩展

@ -1,14 +0,0 @@
// 将 AST 翻译为极简 IR。
#pragma once
#include <memory>
namespace ast {
struct CompUnit;
}
namespace ir {
class Module;
}
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast);

@ -1 +1,4 @@
#include "irgen/IRGen.h"
// 声明翻译模块:
// - 处理全局变量与局部变量声明
// - 处理数组初始化、空间分配与初值生成等

@ -1,95 +1,4 @@
#include "irgen/IRGen.h"
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ast/AstNodes.h"
#include "ir/IR.h"
namespace {
class IRGenImpl {
public:
explicit IRGenImpl(ir::Module& module)
: module_(module),
func_(module_.CreateFunction("main")),
builder_(func_->entry()) {}
void 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> TakeModule() {
return std::make_unique<ir::Module>(std::move(module_));
}
private:
void GenBlock(const ast::Block& block) {
for (const auto& decl : block.varDecls) {
ir::Value* init = nullptr;
if (decl->init) {
init = GenExpr(*decl->init);
} else {
const_pool_.push_back(std::make_unique<ir::ConstantInt>(0));
init = const_pool_.back().get();
}
locals_[decl->name] = init;
}
for (const auto& stmt : block.stmts) {
if (auto ret = dynamic_cast<ast::ReturnStmt*>(stmt.get())) {
ir::Value* v = GenExpr(*ret->value);
builder_.CreateRet(v);
}
}
}
ir::Value* GenExpr(const ast::Expr& expr) {
if (auto num = dynamic_cast<const ast::NumberExpr*>(&expr)) {
const_pool_.push_back(std::make_unique<ir::ConstantInt>(num->value));
return const_pool_.back().get();
}
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 = "%t" + std::to_string(temp_index_++);
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("不支持的表达式类型");
}
ir::Module& module_;
ir::Function* func_;
ir::IRBuilder builder_;
std::unordered_map<std::string, ir::Value*> locals_;
std::vector<std::unique_ptr<ir::ConstantInt>> const_pool_;
int temp_index_ = 0;
};
} // namespace
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;
}
// IR 生成驱动Driver
// - 驱动 Visitor 遍历 AST调度各子模块完成翻译
// - 统一管理模块级翻译入口与上下文Module/IRBuilder 等)
// - 组织函数/语句/表达式/声明等翻译流程

@ -1 +1,4 @@
#include "irgen/IRGen.h"
// 表达式翻译模块:
// - 处理算术运算、比较、逻辑运算、函数调用等表达式
// - 生成对应的 IR 指令并返回 SSA 值

@ -1 +1,4 @@
#include "irgen/IRGen.h"
// 函数翻译模块:
// - 处理函数定义、参数列表与返回值翻译
// - 创建并填充对应的 IR Function 对象

@ -1 +1,4 @@
#include "irgen/IRGen.h"
// 语句翻译模块:
// - 处理 if/while/return 等控制流构造
// - 负责基本块创建、分支跳转与控制流收束

@ -1,29 +1,30 @@
#include <exception>
#include <iostream>
#include <string>
#include "frontend/AntlrDriver.h"
#include "frontend/AstBuilder.h"
#include "ir/IR.h"
#include "irgen/IRGen.h"
#include "sem/Sema.h"
#include "utils/CLI.h"
#include "utils/Log.h"
#include "ast/AstNodes.h"
static void PrintUsage(const char* argv0) {
std::cerr << "用法: " << (argv0 ? argv0 : "compiler") << " <input.sy> [options]\n";
std::cerr << "说明: 当前为工程骨架阶段,暂不执行完整编译流程,仅用于验证可编译/可链接。\n";
}
int main(int argc, char** argv) {
try {
auto opts = ParseCLI(argc, argv);
auto antlr = ParseFileWithAntlr(opts.input);
auto ast = BuildAst(antlr.tree);
ast::PrintAST(*ast); // 调试 AST
ast = RunSema(std::move(ast));
auto module = GenerateIR(*ast);
if (argc <= 1) {
PrintUsage(argv[0]);
return 0;
}
ir::IRPrinter printer;
printer.Print(*module);
} catch (const std::exception& ex) {
LOG_ERROR(ex.what());
return 1;
std::string input_path = argv[1];
if (input_path == "-h" || input_path == "--help") {
PrintUsage(argv[0]);
return 0;
}
// TODO: 后续在此接入完整流水线:
// 1) frontend: ANTLR 解析 + AST 构建
// 2) sem: 语义分析
// 3) irgen: AST -> IR
// 4) ir passes: 可选优化
// 5) mir/backend: AArch64 指令选择、寄存器分配、栈帧、汇编输出
(void)input_path;
return 0;
}

@ -1 +1,4 @@
#include <optional>
// 常量求值:
// - 处理数组维度、全局初始化、const 表达式等编译期可计算场景
// - 为语义分析与 IR 生成提供常量折叠/常量值信息

@ -1,52 +1,5 @@
// 极简语义分析:只检查变量是否先声明再使用。
#include "sem/Sema.h"
// 语义分析主流程:
// - 符号解析与绑定、类型检查、控制流规则检查
// - 记录/插入必要的隐式转换(或在节点上标注)
// - 输出为“带类型 / 符号 / 常量信息”的 AST
#include <stdexcept>
#include <string>
#include <unordered_set>
#include "ast/AstNodes.h"
#include "sem/SymbolTable.h"
namespace {
class SemaVisitor {
public:
explicit SemaVisitor(SymbolTable& table) : table_(table) {}
void CheckBlock(const ast::Block& block) {
for (const auto& decl : block.varDecls) {
table_.Add(decl->name);
if (decl->init) CheckExpr(*decl->init);
}
for (const auto& stmt : block.stmts) {
if (auto ret = dynamic_cast<ast::ReturnStmt*>(stmt.get())) {
CheckExpr(*ret->value);
}
}
}
void CheckExpr(const ast::Expr& expr) {
if (auto var = dynamic_cast<const ast::VarExpr*>(&expr)) {
if (!table_.Contains(var->name)) {
throw std::runtime_error("使用了未定义的变量: " + var->name);
}
} else if (auto bin = dynamic_cast<const ast::BinaryExpr*>(&expr)) {
CheckExpr(*bin->lhs);
CheckExpr(*bin->rhs);
}
}
private:
SymbolTable& table_;
};
} // namespace
std::shared_ptr<ast::CompUnit> RunSema(std::shared_ptr<ast::CompUnit> ast) {
if (!ast || !ast->func || !ast->func->body) return ast;
SymbolTable table;
SemaVisitor visitor(table);
visitor.CheckBlock(*ast->func->body);
return ast;
}

@ -1,11 +0,0 @@
// 语义检查(极简版)。
#pragma once
#include <memory>
namespace ast {
struct CompUnit;
}
// 返回经过检查的 AST当前直接返回原 AST
std::shared_ptr<ast::CompUnit> RunSema(std::shared_ptr<ast::CompUnit> ast);

@ -1 +1,4 @@
#include "sem/SymbolTable.h"
// 符号表与作用域管理:
// - 支持嵌套作用域(块/函数/全局)
// - 变量/函数/参数/常量的注册、查找与遮蔽规则

@ -1,16 +0,0 @@
// 极简符号表:记录局部变量是否定义。
#pragma once
#include <string>
#include <unordered_map>
class SymbolTable {
public:
void Add(const std::string& name) { table_[name] = true; }
bool Contains(const std::string& name) const {
return table_.find(name) != table_.end();
}
private:
std::unordered_map<std::string, bool> table_;
};

@ -1,12 +1,5 @@
#include "utils/CLI.h"
// 命令行参数解析:
// - 解析输入/输出路径
// - 解析输出类型IR/MIR/ASM与优化级别等选项
// - 将参数传递给 main.cpp 的编译流水线驱动
#include <stdexcept>
CLIOptions ParseCLI(int argc, char** argv) {
if (argc <= 1) {
throw std::runtime_error("用法: compiler <input.sy>");
}
CLIOptions opt;
opt.input = argv[1];
return opt;
}

@ -1,10 +0,0 @@
// 简易命令行解析:仅支持输入文件路径。
#pragma once
#include <string>
struct CLIOptions {
std::string input;
};
CLIOptions ParseCLI(int argc, char** argv);

@ -1 +1,4 @@
#include "utils/Log.h"
// 日志模块:
// - 统一输出调试信息、阶段信息与错误信息
// - 提供可配置的日志级别与输出位置(按需要实现)

@ -1,7 +0,0 @@
// 轻量日志接口。
#pragma once
#include <iostream>
#define LOG_INFO(msg) std::cerr << "[info] " << msg << "\n"
#define LOG_ERROR(msg) std::cerr << "[error] " << msg << "\n"

@ -1,5 +0,0 @@
int main() {
int a = 1;
int b = 2;
return a + b;
}
Loading…
Cancel
Save