-
Notifications
You must be signed in to change notification settings - Fork 7
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
1f9534e
commit 7b6e7c3
Showing
8 changed files
with
142 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module; | ||
#include <llvm/IR/IRBuilder.h> | ||
#include <llvm/IR/Value.h> | ||
module IR.Builder; | ||
import Type.Variable; | ||
namespace Riddle { | ||
llvm::Value *Builder::CreateVariable(llvm::Type *type, llvm::Value *value, const std::string &name, const bool is_const){ | ||
llvm::Value *var; | ||
// 对全局变量特判 | ||
if(ctx->deep() <= 1) { | ||
llvm::Constant *CV = llvm::dyn_cast<llvm::Constant>(value); | ||
var = new llvm::GlobalVariable(ctx->module, type, is_const, llvm::GlobalVariable::LinkageTypes::ExternalLinkage, CV, name); | ||
} else { | ||
var = llvmBuilder.CreateAlloca(type, nullptr, name); | ||
llvmBuilder.CreateStore(value, var); | ||
} | ||
ctx->addVariable(Variable(name,var,is_const)); | ||
return 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 |
---|---|---|
@@ -1,14 +1,42 @@ | ||
module; | ||
#include "llvm/IR/Module.h" | ||
export module Builder; | ||
#include <llvm/IR/IRBuilder.h> | ||
export module IR.Builder; | ||
|
||
import IR.Context; | ||
|
||
export namespace Riddle { | ||
class Builder { | ||
Context *ctx = nullptr; | ||
llvm::IRBuilder<> llvmBuilder; | ||
|
||
public: | ||
llvm::Module &module; | ||
llvm::LLVMContext &context; | ||
explicit Builder(Context &context): ctx(&context), llvmBuilder(ctx->context) {} | ||
|
||
/// @brief 获取不同位数的整数类型 | ||
/// @param bits 整数类型的位数 | ||
/// @return llvm:IntegerTy | ||
inline llvm::Type *getIntegerTy(const unsigned bits = 32) const { | ||
return llvm::Type::getIntNTy(ctx->context, bits); | ||
} | ||
|
||
inline llvm::Type *getFloatTy() const { | ||
return llvm::Type::getFloatTy(ctx->context); | ||
} | ||
|
||
inline llvm::Type *getDoubleTy() const { | ||
return llvm::Type::getDoubleTy(ctx->context); | ||
} | ||
|
||
inline llvm::Type *getVoidTy() const { | ||
return llvm::Type::getVoidTy(ctx->context); | ||
} | ||
|
||
inline llvm::Type *getBoolTy() const { | ||
return getIntegerTy(1); | ||
} | ||
|
||
llvm::Value *CreateVariable(llvm::Type *type, llvm::Value *value, const std::string &name = "", bool is_const = false); | ||
|
||
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,35 @@ | ||
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 |
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 |
---|---|---|
@@ -1,63 +1,79 @@ | ||
module; | ||
#include "llvm/IR/LLVMContext.h" | ||
|
||
#include "llvm/IR/Module.h" | ||
#include "llvm/Linker/Linker.h" | ||
#include <stack> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
export module Context; | ||
export module IR.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; | ||
std::stack<std::unordered_set<std::string>> Defined = {}; | ||
|
||
public: | ||
VarManager(): Vars({}) {} | ||
|
||
/// @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(); | ||
} | ||
/// @param name 变量名 | ||
/// @return 变量 | ||
Variable &getVariable(const std::string &name); | ||
|
||
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); | ||
} | ||
/// @brief 添加一个变量 | ||
/// @param var 变量 | ||
void addVariable(Variable &var); | ||
|
||
void push() { | ||
Defined.emplace(); | ||
} | ||
/// @brief 进入下一个生命周期 | ||
void push(); | ||
|
||
void pop() { | ||
for(auto i: Defined.top()) { | ||
Vars[i].pop(); | ||
if(Vars[i].empty()) { | ||
Vars.erase(i); | ||
} | ||
} | ||
Defined.pop(); | ||
} | ||
/// @brief 退出当前作用域 | ||
void pop(); | ||
}; | ||
}// namespace Riddle | ||
|
||
export namespace Riddle { | ||
class Context { | ||
int _deep = 0; | ||
|
||
public: | ||
llvm::LLVMContext &context; | ||
llvm::Module module; | ||
VarManager varManager; | ||
|
||
explicit Context(llvm::LLVMContext &context): context(context) {} | ||
explicit Context(llvm::LLVMContext &context): context(context), module("", context) {} | ||
|
||
void addVar(Variable var) { | ||
inline void addVariable(Variable var) { | ||
varManager.addVariable(var); | ||
} | ||
|
||
inline void push() { | ||
varManager.push(); | ||
} | ||
|
||
inline void pop() { | ||
varManager.pop(); | ||
} | ||
|
||
inline unsigned long long deep() const { | ||
return _deep; | ||
} | ||
|
||
void merge(const Context &ctx) { | ||
llvm::Linker linker(module); | ||
auto t = std::make_unique<llvm::Module>(ctx.module.getModuleIdentifier(), ctx.context); | ||
|
||
// 复制源模块的布局和目标配置 | ||
t->setDataLayout(ctx.module.getDataLayout()); | ||
t->setTargetTriple(ctx.module.getTargetTriple()); | ||
|
||
if(linker.linkInModule(std::move(t))) { | ||
llvm::errs() << "Error: Failed to merge modules.\n"; | ||
} else { | ||
llvm::outs() << "Modules merged successfully.\n"; | ||
} | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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
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