forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.8 KiB
86 lines
2.8 KiB
// 保存函数列表并提供模块级上下文访问。
|
|
|
|
#include "include/ir/IR.h"
|
|
|
|
namespace ir {
|
|
|
|
Context& Module::GetContext() { return context_; }
|
|
|
|
const Context& Module::GetContext() const { return context_; }
|
|
|
|
Function* Module::CreateFunction(const std::string& name,
|
|
std::shared_ptr<Type> ret_type,
|
|
bool is_external) {
|
|
functions_.push_back(
|
|
std::make_unique<Function>(name, std::move(ret_type), is_external));
|
|
return functions_.back().get();
|
|
}
|
|
|
|
Function* Module::GetFunction(const std::string& name) const {
|
|
for (const auto& func : functions_) {
|
|
if (func && func->GetName() == name) {
|
|
return func.get();
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
GlobalVariable* Module::CreateGlobalI32(const std::string& name,
|
|
int init_value) {
|
|
globals_.push_back(std::make_unique<GlobalVariable>(name, init_value));
|
|
return globals_.back().get();
|
|
}
|
|
|
|
GlobalVariable* Module::CreateGlobalF32(const std::string& name,
|
|
double init_value) {
|
|
globals_.push_back(std::make_unique<GlobalVariable>(name, init_value));
|
|
return globals_.back().get();
|
|
}
|
|
|
|
GlobalVariable* Module::CreateGlobalArrayI32(const std::string& name,
|
|
size_t array_size) {
|
|
globals_.push_back(std::make_unique<GlobalVariable>(name, array_size));
|
|
return globals_.back().get();
|
|
}
|
|
|
|
GlobalVariable* Module::CreateGlobalArrayF32(const std::string& name,
|
|
size_t array_size) {
|
|
globals_.push_back(std::make_unique<GlobalVariable>(
|
|
name, array_size, GlobalVariable::ElemKind::Float32));
|
|
return globals_.back().get();
|
|
}
|
|
|
|
GlobalVariable* Module::CreateGlobalArrayI32(const std::string& name,
|
|
size_t array_size,
|
|
const std::vector<int>& init_values) {
|
|
globals_.push_back(std::make_unique<GlobalVariable>(name, array_size, init_values));
|
|
return globals_.back().get();
|
|
}
|
|
|
|
GlobalVariable* Module::CreateGlobalArrayF32(const std::string& name,
|
|
size_t array_size,
|
|
const std::vector<double>& init_values) {
|
|
globals_.push_back(std::make_unique<GlobalVariable>(name, array_size, init_values));
|
|
return globals_.back().get();
|
|
}
|
|
|
|
GlobalVariable* Module::GetGlobal(const std::string& name) const {
|
|
const std::string ir_name = "@" + name;
|
|
for (const auto& global : globals_) {
|
|
if (global && global->GetName() == ir_name) {
|
|
return global.get();
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const std::vector<std::unique_ptr<GlobalVariable>>& Module::GetGlobals() const {
|
|
return globals_;
|
|
}
|
|
|
|
const std::vector<std::unique_ptr<Function>>& Module::GetFunctions() const {
|
|
return functions_;
|
|
}
|
|
|
|
} // namespace ir
|