Skip to content

Commit

Permalink
初步增加中间层
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Sep 27, 2024
1 parent 9ceaae5 commit 1812f24
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ message("Current C compiler: ${CMAKE_C_COMPILER_ID}")
message("Current build type: ${CMAKE_BUILD_TYPE}")
message("Current generator: ${CMAKE_GENERATOR}")

#Enter the system type for better searching of the database.
# Enter the system type for better searching of the database.
if(WIN32)
set(PLATFORM_DEFINES "-DWIN32")
else(LINUX)
Expand Down
14 changes: 14 additions & 0 deletions src/IR/Builder.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module;
#include "llvm/IR/Module.h"
export module Builder;
export namespace Riddle {
class Builder {
public:
llvm::Module &module;
llvm::LLVMContext &context;

explicit Builder(llvm::Module &module): module(module), context(module.getContext()) {}


};
}// namespace Riddle
63 changes: 63 additions & 0 deletions src/IR/Context.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module;
#include "llvm/IR/LLVMContext.h"

#include <stack>
#include <unordered_map>
#include <unordered_set>
export module Context;

import Type.Variable;
import Types.UnionFind;

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

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

/// @brief 获取一个变量
Variable &getVariable(const std::string &name) {
if(!Vars.contains(name) || Vars.find(name)->second.empty()) {
throw std::out_of_range("Variable not found");
}
return *Vars[name].top();
}

void 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 push() {
Defined.emplace();
}

void pop() {
for(auto i: Defined.top()) {
Vars[i].pop();
if(Vars[i].empty()) {
Vars.erase(i);
}
}
Defined.pop();
}
};
}// namespace Riddle
export namespace Riddle {
class Context {
public:
llvm::LLVMContext &context;
VarManager varManager;

explicit Context(llvm::LLVMContext &context): context(context) {}

void addVar(Variable var) {
varManager.addVariable(var);
}
};
}
54 changes: 54 additions & 0 deletions src/Types/UnionFind.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module;
#include <algorithm>
#include <unordered_map>
#include <memory>
export module Types.UnionFind;
using lld = unsigned long long;
export namespace Riddle {
template<typename T>
class UnionFind {
std::unordered_map<T, std::shared_ptr<T>> parent;// 存储指向父节点的指针
std::unordered_map<T, lld> rank; // 存储秩(用于按秩合并)

public:
// 添加一个新的元素,并初始化它为自己的父节点
void add(const T &x) {
if(!parent.contains(x) ) {
parent[x] = std::make_shared<T>(x);// 使用共享指针存储
rank[x] = 1; // 初始化秩为1
}
}

// 查找元素 x 的根,并进行路径压缩
T find(const T &x) {
if(*parent[x] != x) {
T root = find(*parent[x]);
parent[x] = std::make_shared<T>(root);// 路径压缩时更新指针
}
return *parent[x];
}

// 合并两个元素 x 和 y 所在的集合,按秩合并
void unionSets(const T &x, const T &y) {
T rootX = find(x);
T rootY = find(y);

if(rootX != rootY) {
if(rank[rootX] > rank[rootY]) {
parent[rootY] = std::make_shared<T>(rootX);// 合并并更新指针
} else if(rank[rootX] < rank[rootY]) {
parent[rootX] = std::make_shared<T>(rootY);
} else {
parent[rootY] = std::make_shared<T>(rootX);
rank[rootX] += 1;// 增加根节点的秩
}
}
}

// 判断两个元素是否属于同一个集合
bool isConnected(const T& x, const T& y) {
return find(x) == find(y);
}
};

}// namespace Riddle
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ int main(const int argv, char *argc[]) {
// Parser
Riddle::buildQueue.parserFile(opt.source);
Riddle::buildQueue.start();
Riddle::GenVisitor a("123");
return 0;
}

0 comments on commit 1812f24

Please sign in to comment.