-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ceaae5
commit 1812f24
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters