refactor(dev): 移动头文件位置

This commit is contained in:
jing
2026-03-12 10:34:02 +08:00
parent 7f1b0aaead
commit d0a4c7d6d2
8 changed files with 1 additions and 1 deletions

View File

@@ -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);

View File

@@ -1,9 +0,0 @@
#pragma once
#include <iosfwd>
#include "antlr4-runtime.h"
// 以树状缩进形式直接打印 ANTLR parse tree。
void PrintSyntaxTree(antlr4::tree::ParseTree* tree, antlr4::Parser* parser,
std::ostream& os);

View File

@@ -1,228 +0,0 @@
// 极简 IR 定义:当前只支撑 i32 和加法,演示用。
// 可在此基础上扩展更多类型/指令
#pragma once
#include <iosfwd>
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
namespace ir {
class Type;
class ConstantInt;
class Instruction;
class BasicBlock;
class Function;
// IR 上下文:集中管理类型、常量等共享资源,便于复用与扩展。
class Context {
public:
Context() = default;
~Context();
const std::shared_ptr<Type>& Void();
const std::shared_ptr<Type>& Int32();
const std::shared_ptr<Type>& PtrInt32();
// 去重创建 i32 常量。
ConstantInt* GetConstInt(int v);
std::string NextTemp();
private:
std::shared_ptr<Type> void_;
std::shared_ptr<Type> int32_;
std::shared_ptr<Type> ptr_i32_;
std::unordered_map<int, std::unique_ptr<ConstantInt>> const_ints_;
int temp_index_ = -1;
};
class Type {
public:
enum class Kind { Void, Int32, PtrInt32 };
explicit Type(Kind k);
Kind kind() const;
bool IsVoid() const;
bool IsInt32() const;
bool IsPtrInt32() const;
private:
Kind kind_;
};
class Value {
public:
Value(std::shared_ptr<Type> ty, std::string name);
virtual ~Value() = default;
const std::shared_ptr<Type>& type() const;
const std::string& name() const;
void set_name(std::string n);
void AddUser(Instruction* user);
const std::vector<Instruction*>& users() const;
protected:
std::shared_ptr<Type> type_;
std::string name_;
std::vector<Instruction*> users_;
};
class ConstantInt : public Value {
public:
ConstantInt(std::shared_ptr<Type> ty, int v);
int value() const { return value_; }
private:
int value_{};
};
// 后续还需要扩展更多指令类型。
enum class Opcode { Add, Sub, Mul, Alloca, Load, Store, Ret };
class Instruction : public Value {
public:
Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name = "");
Opcode opcode() const;
bool IsTerminator() const;
BasicBlock* parent() const;
void set_parent(BasicBlock* parent);
private:
Opcode opcode_;
BasicBlock* parent_ = nullptr;
};
class BinaryInst : public Instruction {
public:
BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name);
Value* lhs() const;
Value* rhs() const;
private:
Value* lhs_;
Value* rhs_;
};
class ReturnInst : public Instruction {
public:
ReturnInst(std::shared_ptr<Type> void_ty, Value* val);
Value* value() const;
private:
Value* value_;
};
class AllocaInst : public Instruction {
public:
AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name);
};
class LoadInst : public Instruction {
public:
LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name);
Value* ptr() const;
private:
Value* ptr_;
};
class StoreInst : public Instruction {
public:
StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr);
Value* value() const;
Value* ptr() const;
private:
Value* value_;
Value* ptr_;
};
class BasicBlock {
public:
explicit BasicBlock(std::string name);
const std::string& name() const;
Function* parent() const;
void set_parent(Function* parent);
bool HasTerminator() const;
const std::vector<std::unique_ptr<Instruction>>& instructions() const;
const std::vector<BasicBlock*>& predecessors() const;
const std::vector<BasicBlock*>& successors() const;
template <typename T, typename... Args>
T* Append(Args&&... args) {
if (HasTerminator()) {
throw std::runtime_error("BasicBlock 已有 terminator不能继续追加指令: " +
name_);
}
auto inst = std::make_unique<T>(std::forward<Args>(args)...);
auto* ptr = inst.get();
ptr->set_parent(this);
instructions_.push_back(std::move(inst));
return ptr;
}
private:
std::string name_;
Function* parent_ = nullptr;
std::vector<std::unique_ptr<Instruction>> instructions_;
std::vector<BasicBlock*> predecessors_;
std::vector<BasicBlock*> successors_;
};
class Function : public Value {
public:
// 允许显式指定返回类型,便于后续扩展多种函数签名。
Function(std::string name, std::shared_ptr<Type> ret_type);
BasicBlock* CreateBlock(const std::string& name);
BasicBlock* entry();
const BasicBlock* entry() const;
const std::vector<std::unique_ptr<BasicBlock>>& blocks() const;
private:
BasicBlock* entry_ = nullptr;
std::vector<std::unique_ptr<BasicBlock>> blocks_;
};
class Module {
public:
Module() = default;
Context& context();
const Context& context() const;
// 创建函数时显式传入返回类型,便于在 IRGen 中根据语法树信息选择类型。
Function* CreateFunction(const std::string& name,
std::shared_ptr<Type> ret_type);
const std::vector<std::unique_ptr<Function>>& functions() const;
private:
Context context_;
std::vector<std::unique_ptr<Function>> functions_;
};
class IRBuilder {
public:
IRBuilder(Context& ctx, BasicBlock* bb);
void SetInsertPoint(BasicBlock* bb);
BasicBlock* GetInsertBlock() const;
// 构造常量、二元运算、返回指令的最小集合。
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);
AllocaInst* CreateAllocaI32(const std::string& name);
LoadInst* CreateLoad(Value* ptr, const std::string& name);
StoreInst* CreateStore(Value* val, Value* ptr);
ReturnInst* CreateRet(Value* v);
private:
Context& ctx_;
BasicBlock* insertBlock_;
};
class IRPrinter {
public:
void Print(const Module& module, std::ostream& os);
};
} // namespace ir

View File

@@ -1,49 +0,0 @@
// 将语法树翻译为 IR。
// 实现拆分在 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include "SysYParser.h"
#include "ir/IR.h"
#include "sem/Sema.h"
namespace ir {
class Module;
class Function;
class IRBuilder;
class Value;
}
class IRGenImpl {
public:
IRGenImpl(ir::Module& module, const SemanticContext& sema);
void Gen(SysYParser::CompUnitContext& cu);
private:
void GenFuncDef(SysYParser::FuncDefContext& func);
void GenBlock(SysYParser::BlockContext& block);
bool GenBlockItem(SysYParser::BlockItemContext& item);
void GenDecl(SysYParser::DeclContext& decl);
bool 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_;
const SemanticContext& sema_;
ir::Function* func_;
ir::IRBuilder builder_;
// 名称绑定由 Sema 负责IRGen 只维护“声明 -> 存储槽位”的代码生成状态。
std::unordered_map<SysYParser::VarDeclContext*, ir::Value*> storage_map_;
};
std::unique_ptr<ir::Module> GenerateIR(SysYParser::CompUnitContext& tree,
const SemanticContext& sema);

View File

@@ -1,30 +0,0 @@
// 基于语法树的语义检查与名称绑定。
#pragma once
#include <unordered_map>
#include "SysYParser.h"
class SemanticContext {
public:
void BindVarUse(SysYParser::PrimaryContext* use,
SysYParser::VarDeclContext* decl) {
var_uses_[use] = decl;
}
SysYParser::VarDeclContext* ResolveVarUse(
const SysYParser::PrimaryContext* use) const {
auto it = var_uses_.find(use);
return it == var_uses_.end() ? nullptr : it->second;
}
private:
std::unordered_map<const SysYParser::PrimaryContext*,
SysYParser::VarDeclContext*>
var_uses_;
};
// 目前仅检查:
// - 变量先声明后使用
// - 局部变量不允许重复定义
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);

View File

@@ -1,17 +0,0 @@
// 极简符号表:记录局部变量定义点。
#pragma once
#include <string>
#include <unordered_map>
#include "SysYParser.h"
class SymbolTable {
public:
void Add(const std::string& name, SysYParser::VarDeclContext* decl);
bool Contains(const std::string& name) const;
SysYParser::VarDeclContext* Lookup(const std::string& name) const;
private:
std::unordered_map<std::string, SysYParser::VarDeclContext*> table_;
};

View File

@@ -1,14 +0,0 @@
// 简易命令行解析:支持帮助、输入文件与输出阶段选择。
#pragma once
#include <string>
struct CLIOptions {
std::string input;
bool emit_parse_tree = false;
bool emit_ir = true;
bool emit_asm = false;
bool show_help = false;
};
CLIOptions ParseCLI(int argc, char** argv);

View File

@@ -1,20 +0,0 @@
// 轻量日志接口。
#pragma once
#include <cstddef>
#include <exception>
#include <iosfwd>
#include <string>
#include <string_view>
void LogInfo(std::string_view msg, std::ostream& os);
void LogError(std::string_view msg, std::ostream& os);
std::string FormatError(std::string_view stage, std::string_view msg);
std::string FormatErrorAt(std::string_view stage, std::size_t line,
std::size_t column, std::string_view msg);
bool HasErrorPrefix(std::string_view msg, std::string_view stage);
void PrintException(std::ostream& os, const std::exception& ex);
// 打印命令行帮助信息(用于 `compiler --help`)。
void PrintHelp(std::ostream& os);