// 管理基础类型、整型常量池和临时名生成。 #include "ir/IR.h" #include #include namespace ir { Context::~Context() = default; 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(Type::GetInt32Type(), v)).first; return inserted->second.get(); } ConstantFloat* Context::GetConstFloat(double v) { std::ostringstream key_builder; key_builder << std::setprecision(17) << v; const std::string key = key_builder.str(); auto it = const_floats_.find(key); if (it != const_floats_.end()) return it->second.get(); auto inserted = const_floats_ .emplace(key, std::make_unique(Type::GetFloat32Type(), v)) .first; return inserted->second.get(); } ConstantInt* Context::GetConstBool(int v) { auto it = const_bools_.find(v ? 1 : 0); if (it != const_bools_.end()) return it->second.get(); auto inserted = const_bools_.emplace(v ? 1 : 0, std::make_unique(Type::GetInt1Type(), v ? 1 : 0)).first; return inserted->second.get(); } std::string Context::NextTemp() { std::ostringstream oss; oss << "%t" << ++temp_index_; return oss.str(); } } // namespace ir