引入了常量池优化,修改constvalue类并对IR生成修复,能够编译通过

This commit is contained in:
rain2133
2025-06-19 00:18:58 +08:00
parent 1aa785efc3
commit 1de8c0e7d7
4 changed files with 194 additions and 64 deletions

View File

@@ -13,6 +13,8 @@
#include <string>
#include <utility>
#include <vector>
#include <variant>
#include <iomanip>
using namespace std;
namespace sysy {
@@ -80,6 +82,15 @@ Type *Type::getFunctionType(Type *returnType,
return FunctionType::get(returnType, paramTypes);
}
Type *Type::getArrayType(Type *elementType, const vector<int> &dims) {
// forward to ArrayType
return ArrayType::get(elementType, dims);
}
ArrayType* Type::asArrayType() const {
return isArray() ? dynamic_cast<ArrayType*>(const_cast<Type*>(this)) : nullptr;
}
int Type::getSize() const {
switch (kind) {
case kInt:
@@ -177,33 +188,75 @@ bool Value::isConstant() const {
return false;
}
ConstantValue *ConstantValue::get(int value) {
static std::map<int, std::unique_ptr<ConstantValue>> intConstants;
auto iter = intConstants.find(value);
if (iter != intConstants.end())
return iter->second.get();
auto constant = new ConstantValue(value);
assert(constant);
auto result = intConstants.emplace(value, constant);
return result.first->second.get();
// 定义静态常量池
std::unordered_map<ConstantValueKey, ConstantValue*, ConstantValue::ConstantValueHash> ConstantValue::constantPool;
// 常量池实现
ConstantValue* ConstantValue::get(Type* type, int32_t value) {
ConstantValueKey key = {type, ConstantValVariant(value)};
if (auto it = constantPool.find(key); it != constantPool.end()) {
return it->second;
}
ConstantValue* constant = new ConstantInt(type, value);
constantPool[key] = constant;
return constant;
}
ConstantValue *ConstantValue::get(float value) {
static std::map<float, std::unique_ptr<ConstantValue>> floatConstants;
auto iter = floatConstants.find(value);
if (iter != floatConstants.end())
return iter->second.get();
auto constant = new ConstantValue(value);
assert(constant);
auto result = floatConstants.emplace(value, constant);
return result.first->second.get();
ConstantValue* ConstantValue::get(Type* type, float value) {
ConstantValueKey key = {type, ConstantValVariant(value)};
if (auto it = constantPool.find(key); it != constantPool.end()) {
return it->second;
}
ConstantValue* constant = new ConstantFloat(type, value);
constantPool[key] = constant;
return constant;
}
void ConstantValue::print(ostream &os) const {
if (isInt())
os << getInt();
else
os << getFloat();
ConstantValue* ConstantValue::getInt32(int32_t value) {
return get(Type::getIntType(), value);
}
ConstantValue* ConstantValue::getFloat32(float value) {
return get(Type::getFloatType(), value);
}
ConstantValue* ConstantValue::getTrue() {
return get(Type::getIntType(), 1);
}
ConstantValue* ConstantValue::getFalse() {
return get(Type::getIntType(), 0);
}
void ConstantValue::print(std::ostream &os) const {
// 根据类型调用相应的打印实现
if (auto intConst = dynamic_cast<const ConstantInt*>(this)) {
intConst->print(os);
}
else if (auto floatConst = dynamic_cast<const ConstantFloat*>(this)) {
floatConst->print(os);
}
else {
os << "???"; // 未知常量类型
}
}
void ConstantInt::print(std::ostream &os) const {
os << value;
}
void ConstantFloat::print(std::ostream &os) const {
if (value == static_cast<int>(value)) {
os << value << ".0"; // 确保输出带小数点
} else {
os << std::fixed << std::setprecision(6) << value;
}
}
Argument::Argument(Type *type, BasicBlock *block, int index,