forked from NUDT-compiler/nudt-compiler-cpp
parent
e941cced9b
commit
8903bf73f9
@ -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,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,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 +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,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 +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 +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,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…
Reference in new issue