[midend]修改类型转换判断的逻辑
This commit is contained in:
@@ -527,12 +527,25 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
|
||||
Type* RType = RValue->getType();
|
||||
|
||||
if (LType != RType) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(RValue);
|
||||
ConstantValue *constValue = dynamic_cast<ConstantValue *>(RValue);
|
||||
if (constValue != nullptr) {
|
||||
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 { // 假设如果不是浮点型,就是整型
|
||||
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()));
|
||||
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (LType == Type::getFloatType()) {
|
||||
@@ -721,22 +734,34 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) {
|
||||
}
|
||||
|
||||
Type* funcType = builder.getBasicBlock()->getParent()->getReturnType();
|
||||
if (returnValue != nullptr && funcType!= returnValue->getType()) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(returnValue);
|
||||
if (constValue != nullptr) {
|
||||
if (funcType == Type::getFloatType()) {
|
||||
returnValue = ConstantInteger::get(static_cast<float>(constValue->getInt()));
|
||||
} else {
|
||||
returnValue = ConstantFloating::get(static_cast<int>(constValue->getFloat()));
|
||||
if (returnValue != nullptr && funcType!= returnValue->getType()) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(returnValue);
|
||||
if (constValue != nullptr) {
|
||||
if (funcType == Type::getFloatType()) {
|
||||
if(dynamic_cast<ConstantInteger *>(constValue)) {
|
||||
// 如果是整型常量,转换为浮点型
|
||||
returnValue = ConstantFloating::get(static_cast<float>(constValue->getInt()));
|
||||
} else if (dynamic_cast<ConstantFloating *>(constValue)) {
|
||||
// 如果是浮点型常量,直接使用
|
||||
returnValue = ConstantFloating::get(static_cast<float>(constValue->getInt()));
|
||||
}
|
||||
} else {
|
||||
if (funcType == Type::getFloatType()) {
|
||||
returnValue = builder.createIToFInst(returnValue);
|
||||
} else {
|
||||
returnValue = builder.createFtoIInst(returnValue);
|
||||
if(dynamic_cast<ConstantFloating *>(constValue)) {
|
||||
// 如果是浮点型常量,转换为整型
|
||||
returnValue = ConstantInteger::get(static_cast<int>(constValue->getFloat()));
|
||||
} 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);
|
||||
|
||||
return std::any();
|
||||
@@ -1054,20 +1079,34 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
// 如果有一个操作数是浮点数,则将两个操作数都转换为浮点数
|
||||
if (operandType != floatType) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(operand);
|
||||
if (constValue != nullptr)
|
||||
operand = ConstantFloating::get(static_cast<float>(constValue->getInt()));
|
||||
if (constValue != nullptr) {
|
||||
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
|
||||
operand = builder.createIToFInst(operand);
|
||||
} else if (resultType != floatType) {
|
||||
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result);
|
||||
if (constResult != nullptr)
|
||||
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
|
||||
else
|
||||
if (constResult != nullptr) {
|
||||
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
|
||||
result = builder.createIToFInst(result);
|
||||
}
|
||||
|
||||
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result);
|
||||
ConstantValue* constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
ConstantFloating* constResult = dynamic_cast<ConstantFloating *>(result);
|
||||
ConstantFloating* constOperand = dynamic_cast<ConstantFloating *>(operand);
|
||||
if (opType == SysYParser::MUL) {
|
||||
if ((constOperand != nullptr) && (constResult != nullptr)) {
|
||||
result = ConstantFloating::get(constResult->getFloat() *
|
||||
@@ -1088,8 +1127,8 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
assert(false);
|
||||
}
|
||||
} else {
|
||||
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
ConstantInteger *constResult = dynamic_cast<ConstantInteger *>(result);
|
||||
ConstantInteger *constOperand = dynamic_cast<ConstantInteger *>(operand);
|
||||
if (opType == SysYParser::MUL) {
|
||||
if ((constOperand != nullptr) && (constResult != nullptr))
|
||||
result = ConstantInteger::get(constResult->getInt() * constOperand->getInt());
|
||||
@@ -1129,20 +1168,34 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
// 类型转换
|
||||
if (operandType != floatType) {
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
if (constOperand != nullptr)
|
||||
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
|
||||
if (constOperand != nullptr) {
|
||||
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
|
||||
operand = builder.createIToFInst(operand);
|
||||
} else if (resultType != floatType) {
|
||||
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
|
||||
if (constResult != nullptr)
|
||||
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
|
||||
if (constResult != nullptr) {
|
||||
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
|
||||
result = builder.createIToFInst(result);
|
||||
}
|
||||
|
||||
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
ConstantFloating *constResult = dynamic_cast<ConstantFloating *>(result);
|
||||
ConstantFloating *constOperand = dynamic_cast<ConstantFloating *>(operand);
|
||||
if (opType == SysYParser::ADD) {
|
||||
if ((constResult != nullptr) && (constOperand != nullptr))
|
||||
result = ConstantFloating::get(constResult->getFloat() + constOperand->getFloat());
|
||||
@@ -1155,8 +1208,8 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
result = builder.createFSubInst(result, operand);
|
||||
}
|
||||
} else {
|
||||
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
ConstantInteger *constResult = dynamic_cast<ConstantInteger *>(result);
|
||||
ConstantInteger *constOperand = dynamic_cast<ConstantInteger *>(operand);
|
||||
if (opType == SysYParser::ADD) {
|
||||
if ((constResult != nullptr) && (constOperand != nullptr))
|
||||
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) {
|
||||
if (constResult != nullptr)
|
||||
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
|
||||
if (constResult != nullptr){
|
||||
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
|
||||
result = builder.createIToFInst(result);
|
||||
|
||||
}
|
||||
if (operandType != floatType) {
|
||||
if (constOperand != nullptr)
|
||||
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
|
||||
if (constOperand != nullptr) {
|
||||
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
|
||||
operand = builder.createIToFInst(operand);
|
||||
|
||||
@@ -1275,14 +1342,28 @@ std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext *ctx) {
|
||||
|
||||
if (resultType == floatType || operandType == floatType) {
|
||||
if (resultType != floatType) {
|
||||
if (constResult != nullptr)
|
||||
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
|
||||
if (constResult != nullptr){
|
||||
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
|
||||
result = builder.createIToFInst(result);
|
||||
}
|
||||
if (operandType != floatType) {
|
||||
if (constOperand != nullptr)
|
||||
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
|
||||
if (constOperand != nullptr) {
|
||||
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
|
||||
operand = builder.createIToFInst(operand);
|
||||
}
|
||||
@@ -1384,15 +1465,27 @@ void Utils::tree2Array(Type *type, ArrayValueTree *root,
|
||||
} else {
|
||||
if (type == Type::getFloatType()) {
|
||||
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr)
|
||||
result.push_back(ConstantFloating::get(static_cast<float>(constValue->getInt())));
|
||||
if (constValue != nullptr) {
|
||||
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
|
||||
result.push_back(builder->createIToFInst(value));
|
||||
|
||||
} else {
|
||||
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr)
|
||||
result.push_back(ConstantInteger::get(static_cast<int>(constValue->getFloat())));
|
||||
if (constValue != nullptr){
|
||||
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
|
||||
result.push_back(builder->createFtoIInst(value));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user