Skip to content

Commit

Permalink
清除冗余结构
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Oct 8, 2024
1 parent a8793f5 commit c805be6
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 79 deletions.
17 changes: 9 additions & 8 deletions src/IR/Builder.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ export namespace Riddle {
llvm::IRBuilder<> llvmBuilder;

public:
explicit Builder(Context &context): ctx(&context), llvmBuilder(ctx->context) {}
explicit Builder(Context &context): ctx(&context), llvmBuilder(ctx->llvm_context) {}

Context &getContext() const { return *ctx; }
// constant

/// @brief 获取不同位数的整数类型
/// @param bits 整数类型的位数
/// @return llvm:IntegerTy
inline llvm::IntegerType *getIntegerTy(const unsigned bits = 32) const {
return llvm::Type::getIntNTy(ctx->context, bits);
return llvm::Type::getIntNTy(ctx->llvm_context, bits);
}

/// @brief 获取单浮点类型
inline llvm::Type *getFloatTy() const {
return llvm::Type::getFloatTy(ctx->context);
return llvm::Type::getFloatTy(ctx->llvm_context);
}

/// @brief 获取双浮点类型
inline llvm::Type *getDoubleTy() const {
return llvm::Type::getDoubleTy(ctx->context);
return llvm::Type::getDoubleTy(ctx->llvm_context);
}

/// @brief 获取空类型
inline llvm::Type *getVoidTy() const {
return llvm::Type::getVoidTy(ctx->context);
return llvm::Type::getVoidTy(ctx->llvm_context);
}

/// @brief 获取布尔类型
Expand All @@ -52,12 +53,12 @@ export namespace Riddle {
}

/// @brief 将 int N 类型 转化为 llvm::Constant 类型
inline llvm::Constant* getIntN(const unsigned long long bits,const long long int) {
return llvmBuilder.getIntN(bits,bits);
inline llvm::Constant *getIntN(const unsigned long long bits, const long long int) {
return llvmBuilder.getIntN(bits, bits);
}

inline llvm::Constant *getDouble(const double &value) const {
return llvm::ConstantFP::get(llvm::Type::getDoubleTy(ctx->context), value);
return llvm::ConstantFP::get(llvm::Type::getDoubleTy(ctx->llvm_context), value);
}

/// @brief 创建一个变量
Expand Down
33 changes: 0 additions & 33 deletions src/IR/Context.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,2 @@
module;
#include <stdexcept>
module IR.Context;

namespace Riddle {
Variable &VarManager::getVariable(const std::string &name) {
if(!Vars.contains(name) || Vars.find(name)->second.empty()) {
throw std::logic_error("Variable not found");
}
return *Vars[name].top();
}

void VarManager::addVariable(Variable &var) {
if(Defined.top().contains(var.name)) {
throw std::logic_error("Variable already exists");
}
Defined.top().insert(var.name);
Vars[var.name].push(&var);
}

void VarManager::push() {
Defined.emplace();
}

void VarManager::pop() {
for(const auto &i: Defined.top()) {
Vars[i].pop();
if(Vars[i].empty()) {
Vars.erase(i);
}
}
Defined.pop();
}

}// namespace Riddle
34 changes: 5 additions & 29 deletions src/IR/Context.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,21 @@ module;
export module IR.Context;

import Type.Variable;

namespace Riddle {
class VarManager {
std::unordered_map<std::string, std::stack<Variable *>> Vars;
std::stack<std::unordered_set<std::string>> Defined = {};

public:
VarManager(): Vars({}) {}

/// @brief 获取一个变量
/// @param name 变量名
/// @return 变量
Variable &getVariable(const std::string &name);

/// @brief 添加一个变量
/// @param var 变量
void addVariable(Variable &var);

/// @brief 进入下一个生命周期
void push();

/// @brief 退出当前作用域
void pop();
};
}// namespace Riddle
import Manager.VarManager;

export namespace Riddle {
class Context {
int _deep = 0;

public:
llvm::LLVMContext &context;
llvm::LLVMContext &llvm_context;
llvm::Module module;
VarManager varManager;

explicit Context(llvm::LLVMContext &context): context(context), module("", context) {}
explicit Context(llvm::LLVMContext &context): llvm_context(context), module("", context) {}

inline void addVariable(Variable var) {
varManager.addVariable(var);
inline void addVariable(const Variable &var) {
varManager.addVar(var);
}

inline void push() {
Expand Down
13 changes: 13 additions & 0 deletions src/IR/ParserStmt.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ module;
export module IR.ParserStmt;
import Types.Statements;
import Manager.ClassManager;
import Manager.VarManager;
import IR.Context;
import IR.Builder;
export namespace Riddle {
class ParserStmt {
Builder builder;
ClassManager classManager;
VarManager varManager;

public:
ParserStmt(Context &ctx): classManager(ctx.llvm_context), builder(ctx) {}

std::any accept(BaseStmt *stmt) {
if(stmt->getStmtTypeID() == BaseStmt::StmtTypeID::ProgramStmtID) {
Program(static_cast<ProgramStmt *>(stmt));
Expand All @@ -20,7 +25,10 @@ export namespace Riddle {
return Integer(static_cast<IntegerStmt *>(stmt));
} else if(stmt->getStmtTypeID() == BaseStmt::StmtTypeID::DoubleStmtID) {
return Double(static_cast<DoubleStmt *>(stmt));
} else if(stmt->getStmtTypeID() == BaseStmt::StmtTypeID::ObjStmtID) {
return Object(static_cast<ObjectStmt *>(stmt));
}
return nullptr;
}

llvm::Value *Integer(const IntegerStmt *stmt) {
Expand All @@ -43,5 +51,10 @@ export namespace Riddle {
const std::string name = stmt->getName();
return builder.createVariable(type, value, name);
}

llvm::Value *Object(const ObjectStmt *stmt) {
const std::string name = stmt->getName();
return varManager.getVar(name).var;
}
};
}// namespace Riddle
9 changes: 7 additions & 2 deletions src/Tools/Managers/VarManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module;
#include <stdexcept>
#include <llvm/IR/Value.h>
#include <stdexcept>
#include <ranges>
module Manager.VarManager;

namespace Riddle {
Expand All @@ -14,7 +15,7 @@ namespace Riddle {
Defined.emplace();
}
void VarManager::pop() {
for(auto [name, iDefined]: Defined.top()) {
for(const auto& name: Defined.top() | std::views::keys) {
NamedVar[name].pop();
if(NamedVar[name].empty()) {
NamedVar.erase(name);
Expand All @@ -29,6 +30,10 @@ namespace Riddle {
NamedVar[name].push(Variable(name, value, isConst));
Defined.top()[name] = true;
}
void VarManager::addVar(const Variable &var) {
NamedVar[var.name].push(var);
Defined.top()[var.name] = true;
}
Variable VarManager::getVar(const std::string &name) {
return NamedVar[name].top();
}
Expand Down
1 change: 1 addition & 0 deletions src/Tools/Managers/VarManager.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export namespace Riddle {
/// @param isConst 是否不变
/// @param value 指某个局部变量的地址
void defineVar(const std::string &name, const bool &isConst, llvm::Value *value = nullptr);
void addVar(const Variable &var);
/// @brief 获取一个变量的属性
/// @param name 变量名
/// @returns 变量的属性
Expand Down
27 changes: 20 additions & 7 deletions src/Types/Statements.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export namespace Riddle {

class ProgramStmt final : public BaseStmt {
public:
std::vector<BaseStmt*> body;
explicit ProgramStmt(std::vector<BaseStmt*> body): body{std::move(body)} {}
std::vector<BaseStmt *> body;
explicit ProgramStmt(std::vector<BaseStmt *> body): body{std::move(body)} {}
};

/// @brief 是多个语句的组合
Expand Down Expand Up @@ -166,9 +166,9 @@ export namespace Riddle {
std::string return_type,
BaseStmt *body,
std::vector<DefineArg> args = {}): BaseStmt(StmtTypeID::FuncDefineStmtID),
func_name(std::move(func_name)),
return_type(std::move(return_type)),
args(std::move(args)), body(body) {}
func_name(std::move(func_name)),
return_type(std::move(return_type)),
args(std::move(args)), body(body) {}

[[nodiscard]] inline std::string getFuncName() const { return func_name; }
[[nodiscard]] inline std::string getReturnType() const { return return_type; }
Expand Down Expand Up @@ -233,9 +233,22 @@ export namespace Riddle {
BaseStmt *condition;
BaseStmt *thenBody;
BaseStmt *elseBody;
public:
IfStmt(BaseStmt* cond,BaseStmt* thenBody,BaseStmt* elseBody):condition(cond),thenBody(thenBody),elseBody(elseBody){}

public:
IfStmt(BaseStmt *cond, BaseStmt *thenBody, BaseStmt *elseBody): condition(cond),thenBody(thenBody),elseBody(elseBody){}

[[nodiscard]] inline BaseStmt *getCondition() const { return condition; }
[[nodiscard]] inline BaseStmt *getThenBody() const { return thenBody; }
[[nodiscard]] inline BaseStmt *getElseBody() const { return elseBody; }
};

class ObjectStmt final : public BaseStmt {
protected:
std::string name;
public:
explicit ObjectStmt(std::string name): name(std::move(name)) {}

[[nodiscard]] inline std::string getName() const { return name; }
};

}// namespace Riddle

0 comments on commit c805be6

Please sign in to comment.