[midend]修改类型转换判断的逻辑

This commit is contained in:
rain2133
2025-07-29 20:23:25 +08:00
parent 63906d0648
commit f5922d0178

View File

@@ -527,12 +527,25 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
Type* RType = RValue->getType(); Type* RType = RValue->getType();
if (LType != RType) { if (LType != RType) {
ConstantValue * constValue = dynamic_cast<ConstantValue *>(RValue); ConstantValue *constValue = dynamic_cast<ConstantValue *>(RValue);
if (constValue != nullptr) { if (constValue != nullptr) {
if (LType == Type::getFloatType()) { if (LType == Type::getFloatType()) {
RValue = ConstantFloating::get(static_cast<float>(constValue->getFloat())); if(dynamic_cast<ConstantInteger *>(constValue)) {
// 如果是整型常量,转换为浮点型
RValue = ConstantFloating::get(static_cast<float>(constValue->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constValue)) {
// 如果是浮点型常量,直接使用
RValue = ConstantFloating::get(static_cast<float>(constValue->getFloat()));
}
} else { // 假设如果不是浮点型,就是整型 } else { // 假设如果不是浮点型,就是整型
if(dynamic_cast<ConstantFloating *>(constValue)) {
// 如果是浮点型常量,转换为整型
RValue = ConstantInteger::get(static_cast<int>(constValue->getFloat()));
} else if (dynamic_cast<ConstantInteger *>(constValue)) {
// 如果是整型常量,直接使用
RValue = ConstantInteger::get(static_cast<int>(constValue->getInt())); RValue = ConstantInteger::get(static_cast<int>(constValue->getInt()));
}
} }
} else { } else {
if (LType == Type::getFloatType()) { if (LType == Type::getFloatType()) {
@@ -721,22 +734,34 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) {
} }
Type* funcType = builder.getBasicBlock()->getParent()->getReturnType(); Type* funcType = builder.getBasicBlock()->getParent()->getReturnType();
if (returnValue != nullptr && funcType!= returnValue->getType()) { if (returnValue != nullptr && funcType!= returnValue->getType()) {
ConstantValue * constValue = dynamic_cast<ConstantValue *>(returnValue); ConstantValue * constValue = dynamic_cast<ConstantValue *>(returnValue);
if (constValue != nullptr) { if (constValue != nullptr) {
if (funcType == Type::getFloatType()) { if (funcType == Type::getFloatType()) {
returnValue = ConstantInteger::get(static_cast<float>(constValue->getInt())); if(dynamic_cast<ConstantInteger *>(constValue)) {
} else { // 如果是整型常量,转换为浮点型
returnValue = ConstantFloating::get(static_cast<int>(constValue->getFloat())); returnValue = ConstantFloating::get(static_cast<float>(constValue->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constValue)) {
// 如果是浮点型常量,直接使用
returnValue = ConstantFloating::get(static_cast<float>(constValue->getInt()));
} }
} else { } else {
if (funcType == Type::getFloatType()) { if(dynamic_cast<ConstantFloating *>(constValue)) {
returnValue = builder.createIToFInst(returnValue); // 如果是浮点型常量,转换为整型
} else { returnValue = ConstantInteger::get(static_cast<int>(constValue->getFloat()));
returnValue = builder.createFtoIInst(returnValue); } else if (dynamic_cast<ConstantInteger *>(constValue)) {
// 如果是整型常量,直接使用
returnValue = ConstantInteger::get(static_cast<int>(constValue->getFloat()));
} }
} }
} else {
if (funcType == Type::getFloatType()) {
returnValue = builder.createIToFInst(returnValue);
} else {
returnValue = builder.createFtoIInst(returnValue);
}
} }
}
builder.createReturnInst(returnValue); builder.createReturnInst(returnValue);
return std::any(); return std::any();
@@ -1054,20 +1079,34 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
// 如果有一个操作数是浮点数,则将两个操作数都转换为浮点数 // 如果有一个操作数是浮点数,则将两个操作数都转换为浮点数
if (operandType != floatType) { if (operandType != floatType) {
ConstantValue * constValue = dynamic_cast<ConstantValue *>(operand); ConstantValue * constValue = dynamic_cast<ConstantValue *>(operand);
if (constValue != nullptr) if (constValue != nullptr) {
operand = ConstantFloating::get(static_cast<float>(constValue->getInt())); if(dynamic_cast<ConstantInteger *>(constValue)) {
// 如果是整型常量,转换为浮点型
operand = ConstantFloating::get(static_cast<float>(constValue->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constValue)) {
// 如果是浮点型常量,直接使用
operand = ConstantFloating::get(static_cast<float>(constValue->getFloat()));
}
}
else else
operand = builder.createIToFInst(operand); operand = builder.createIToFInst(operand);
} else if (resultType != floatType) { } else if (resultType != floatType) {
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result); ConstantValue* constResult = dynamic_cast<ConstantValue *>(result);
if (constResult != nullptr) if (constResult != nullptr) {
result = ConstantFloating::get(static_cast<float>(constResult->getInt())); if(dynamic_cast<ConstantInteger *>(constResult)) {
else // 如果是整型常量,转换为浮点型
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constResult)) {
// 如果是浮点型常量,直接使用
result = ConstantFloating::get(static_cast<float>(constResult->getFloat()));
}
}
else
result = builder.createIToFInst(result); result = builder.createIToFInst(result);
} }
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result); ConstantFloating* constResult = dynamic_cast<ConstantFloating *>(result);
ConstantValue* constOperand = dynamic_cast<ConstantValue *>(operand); ConstantFloating* constOperand = dynamic_cast<ConstantFloating *>(operand);
if (opType == SysYParser::MUL) { if (opType == SysYParser::MUL) {
if ((constOperand != nullptr) && (constResult != nullptr)) { if ((constOperand != nullptr) && (constResult != nullptr)) {
result = ConstantFloating::get(constResult->getFloat() * result = ConstantFloating::get(constResult->getFloat() *
@@ -1088,8 +1127,8 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
assert(false); assert(false);
} }
} else { } else {
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result); ConstantInteger *constResult = dynamic_cast<ConstantInteger *>(result);
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand); ConstantInteger *constOperand = dynamic_cast<ConstantInteger *>(operand);
if (opType == SysYParser::MUL) { if (opType == SysYParser::MUL) {
if ((constOperand != nullptr) && (constResult != nullptr)) if ((constOperand != nullptr) && (constResult != nullptr))
result = ConstantInteger::get(constResult->getInt() * constOperand->getInt()); result = ConstantInteger::get(constResult->getInt() * constOperand->getInt());
@@ -1129,20 +1168,34 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
// 类型转换 // 类型转换
if (operandType != floatType) { if (operandType != floatType) {
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand); ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
if (constOperand != nullptr) if (constOperand != nullptr) {
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt())); if(dynamic_cast<ConstantInteger *>(constOperand)) {
// 如果是整型常量,转换为浮点型
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constOperand)) {
// 如果是浮点型常量,直接使用
operand = ConstantFloating::get(static_cast<float>(constOperand->getFloat()));
}
}
else else
operand = builder.createIToFInst(operand); operand = builder.createIToFInst(operand);
} else if (resultType != floatType) { } else if (resultType != floatType) {
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result); ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
if (constResult != nullptr) if (constResult != nullptr) {
result = ConstantFloating::get(static_cast<float>(constResult->getInt())); if(dynamic_cast<ConstantInteger *>(constResult)) {
// 如果是整型常量,转换为浮点型
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constResult)) {
// 如果是浮点型常量,直接使用
result = ConstantFloating::get(static_cast<float>(constResult->getFloat()));
}
}
else else
result = builder.createIToFInst(result); result = builder.createIToFInst(result);
} }
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result); ConstantFloating *constResult = dynamic_cast<ConstantFloating *>(result);
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand); ConstantFloating *constOperand = dynamic_cast<ConstantFloating *>(operand);
if (opType == SysYParser::ADD) { if (opType == SysYParser::ADD) {
if ((constResult != nullptr) && (constOperand != nullptr)) if ((constResult != nullptr) && (constOperand != nullptr))
result = ConstantFloating::get(constResult->getFloat() + constOperand->getFloat()); result = ConstantFloating::get(constResult->getFloat() + constOperand->getFloat());
@@ -1155,8 +1208,8 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
result = builder.createFSubInst(result, operand); result = builder.createFSubInst(result, operand);
} }
} else { } else {
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result); ConstantInteger *constResult = dynamic_cast<ConstantInteger *>(result);
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand); ConstantInteger *constOperand = dynamic_cast<ConstantInteger *>(operand);
if (opType == SysYParser::ADD) { if (opType == SysYParser::ADD) {
if ((constResult != nullptr) && (constOperand != nullptr)) if ((constResult != nullptr) && (constOperand != nullptr))
result = ConstantInteger::get(constResult->getInt() + constOperand->getInt()); result = ConstantInteger::get(constResult->getInt() + constOperand->getInt());
@@ -1210,15 +1263,29 @@ std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
// 浮点数处理 // 浮点数处理
if (resultType == floatType || operandType == floatType) { if (resultType == floatType || operandType == floatType) {
if (resultType != floatType) { if (resultType != floatType) {
if (constResult != nullptr) if (constResult != nullptr){
result = ConstantFloating::get(static_cast<float>(constResult->getInt())); if(dynamic_cast<ConstantInteger *>(constResult)) {
// 如果是整型常量,转换为浮点型
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constResult)) {
// 如果是浮点型常量,直接使用
result = ConstantFloating::get(static_cast<float>(constResult->getFloat()));
}
}
else else
result = builder.createIToFInst(result); result = builder.createIToFInst(result);
} }
if (operandType != floatType) { if (operandType != floatType) {
if (constOperand != nullptr) if (constOperand != nullptr) {
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt())); if(dynamic_cast<ConstantInteger *>(constOperand)) {
// 如果是整型常量,转换为浮点型
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constOperand)) {
// 如果是浮点型常量,直接使用
operand = ConstantFloating::get(static_cast<float>(constOperand->getFloat()));
}
}
else else
operand = builder.createIToFInst(operand); operand = builder.createIToFInst(operand);
@@ -1275,14 +1342,28 @@ std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext *ctx) {
if (resultType == floatType || operandType == floatType) { if (resultType == floatType || operandType == floatType) {
if (resultType != floatType) { if (resultType != floatType) {
if (constResult != nullptr) if (constResult != nullptr){
result = ConstantFloating::get(static_cast<float>(constResult->getInt())); if(dynamic_cast<ConstantInteger *>(constResult)) {
// 如果是整型常量,转换为浮点型
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constResult)) {
// 如果是浮点型常量,直接使用
result = ConstantFloating::get(static_cast<float>(constResult->getFloat()));
}
}
else else
result = builder.createIToFInst(result); result = builder.createIToFInst(result);
} }
if (operandType != floatType) { if (operandType != floatType) {
if (constOperand != nullptr) if (constOperand != nullptr) {
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt())); if(dynamic_cast<ConstantInteger *>(constOperand)) {
// 如果是整型常量,转换为浮点型
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
} else if (dynamic_cast<ConstantFloating *>(constOperand)) {
// 如果是浮点型常量,直接使用
operand = ConstantFloating::get(static_cast<float>(constOperand->getFloat()));
}
}
else else
operand = builder.createIToFInst(operand); operand = builder.createIToFInst(operand);
} }
@@ -1384,15 +1465,27 @@ void Utils::tree2Array(Type *type, ArrayValueTree *root,
} else { } else {
if (type == Type::getFloatType()) { if (type == Type::getFloatType()) {
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value); ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
if (constValue != nullptr) if (constValue != nullptr) {
result.push_back(ConstantFloating::get(static_cast<float>(constValue->getInt()))); if(dynamic_cast<ConstantInteger *>(constValue))
result.push_back(ConstantFloating::get(static_cast<float>(constValue->getInt())));
else if (dynamic_cast<ConstantFloating *>(constValue))
result.push_back(ConstantFloating::get(static_cast<float>(constValue->getFloat())));
else
assert(false && "Unknown constant type for float conversion.");
}
else else
result.push_back(builder->createIToFInst(value)); result.push_back(builder->createIToFInst(value));
} else { } else {
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value); ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
if (constValue != nullptr) if (constValue != nullptr){
result.push_back(ConstantInteger::get(static_cast<int>(constValue->getFloat()))); if(dynamic_cast<ConstantInteger *>(constValue))
result.push_back(ConstantInteger::get(constValue->getInt()));
else if (dynamic_cast<ConstantFloating *>(constValue))
result.push_back(ConstantInteger::get(static_cast<int>(constValue->getFloat())));
else
assert(false && "Unknown constant type for int conversion.");
}
else else
result.push_back(builder->createFtoIInst(value)); result.push_back(builder->createFtoIInst(value));