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.
46 lines
964 B
46 lines
964 B
// IR 上下文:
|
|
// - 管理类型与常量的创建/复用
|
|
// - 保存字符串常量、符号等公共资源(按需要扩展)
|
|
#include "ir/IR.h"
|
|
|
|
#include <sstream>
|
|
|
|
namespace ir {
|
|
|
|
Context& DefaultContext() {
|
|
static Context ctx;
|
|
return ctx;
|
|
}
|
|
|
|
Context::~Context() = default;
|
|
|
|
const std::shared_ptr<Type>& Context::Void() {
|
|
if (!void_) {
|
|
void_ = std::make_shared<Type>(Type::Kind::Void);
|
|
}
|
|
return void_;
|
|
}
|
|
|
|
const std::shared_ptr<Type>& Context::Int32() {
|
|
if (!int32_) {
|
|
int32_ = std::make_shared<Type>(Type::Kind::Int32);
|
|
}
|
|
return int32_;
|
|
}
|
|
|
|
ConstantInt* Context::GetConstInt(int v) {
|
|
auto it = const_ints_.find(v);
|
|
if (it != const_ints_.end()) return it->second.get();
|
|
auto inserted =
|
|
const_ints_.emplace(v, std::make_unique<ConstantInt>(v)).first;
|
|
return inserted->second.get();
|
|
}
|
|
|
|
std::string Context::NextTemp() {
|
|
std::ostringstream oss;
|
|
oss << "%t" << temp_index_++;
|
|
return oss.str();
|
|
}
|
|
|
|
} // namespace ir
|