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.

48 lines
1.1 KiB

#include "ir/IR.h"
#include <cstring>
#include <sstream>
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<ConstantInt>(Type::GetInt32Type(), v))
.first;
return inserted->second.get();
}
ConstantFloat* Context::GetConstFloat(float v) {
uint32_t bits = 0;
std::memcpy(&bits, &v, sizeof(bits));
auto it = const_floats_.find(bits);
if (it != const_floats_.end()) {
return it->second.get();
}
auto inserted = const_floats_
.emplace(bits, std::make_unique<ConstantFloat>(
Type::GetFloatType(), v))
.first;
return inserted->second.get();
}
std::string Context::NextTemp() {
std::ostringstream oss;
oss << "%t" << ++temp_index_;
return oss.str();
}
std::string Context::NextBlock(const std::string& prefix) {
std::ostringstream oss;
oss << prefix << "." << ++block_index_;
return oss.str();
}
} // namespace ir