// 保存函数列表并提供模块级上下文访问。 #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 func_type) { functions_.push_back(std::make_unique(name, std::move(func_type))); return functions_.back().get(); } Function* Module::FindFunction(const std::string& name) const { for (const auto& func : functions_) { if (func->GetName() == name) { return func.get(); } } return nullptr; } const std::vector>& Module::GetFunctions() const { return functions_; } GlobalValue* Module::CreateGlobal(const std::string& name, std::shared_ptr ty) { // 对于标量类型,自动转换为指针类型 std::shared_ptr global_ty; if (ty->IsInt32()) { global_ty = Type::GetPtrInt32Type(); // i32 -> i32* } else if (ty->IsFloat()) { global_ty = Type::GetPtrFloatType(); // float -> float* } else if (ty->IsInt1()) { global_ty = Type::GetPtrInt1Type(); // i1 -> i1* } else { // 数组等类型保持不变 global_ty = ty; } globals_.push_back(std::make_unique(global_ty, name)); return globals_.back().get(); } const std::vector>& Module::GetGlobals() const { return globals_; } } // namespace ir