[midend-llvnirprint]修改浮点为字面值打印,修复assign的类型推断
This commit is contained in:
@@ -1297,6 +1297,45 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
|
||||
if (dynamic_cast<AllocaInst*>(variable) || dynamic_cast<GlobalValue*>(variable)) {
|
||||
LValue = variable;
|
||||
}
|
||||
|
||||
// 标量变量的类型推断
|
||||
Type* LType = builder.getIndexedType(variable->getType(), indices);
|
||||
|
||||
Value* RValue = computeExp(ctx->exp(), LType); // 右值计算
|
||||
Type* RType = RValue->getType();
|
||||
|
||||
// TODO:computeExp处理了类型转换,可以考虑删除判断逻辑
|
||||
if (LType != RType) {
|
||||
ConstantValue *constValue = dynamic_cast<ConstantValue *>(RValue);
|
||||
if (constValue != nullptr) {
|
||||
if (LType == Type::getFloatType()) {
|
||||
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() && RType != Type::getFloatType()) {
|
||||
RValue = builder.createItoFInst(RValue);
|
||||
} else if (LType != Type::getFloatType() && RType == Type::getFloatType()) {
|
||||
RValue = builder.createFtoIInst(RValue);
|
||||
}
|
||||
// 如果两者都是同一类型,就不需要转换
|
||||
}
|
||||
}
|
||||
|
||||
builder.createStoreInst(RValue, LValue);
|
||||
}
|
||||
else {
|
||||
// 对于数组或多维数组的左值处理
|
||||
@@ -1334,52 +1373,47 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
|
||||
}
|
||||
// 左值为地址
|
||||
LValue = getGEPAddressInst(gepBasePointer, gepIndices);
|
||||
}
|
||||
|
||||
// 数组变量的类型推断,使用gepIndices和gepBasePointer的类型
|
||||
Type* LType = builder.getIndexedType(gepBasePointer->getType(), gepIndices);
|
||||
|
||||
Value* RValue = computeExp(ctx->exp(), LType); // 右值计算
|
||||
Type* RType = RValue->getType();
|
||||
|
||||
// Value* RValue = std::any_cast<Value *>(visitExp(ctx->exp())); // 右值
|
||||
|
||||
// 先推断 LValue 的类型
|
||||
// 如果 LValue 是指向数组的指针,则需要根据 indices 获取正确的类型
|
||||
// 如果 LValue 是标量,则直接使用其类型
|
||||
// 注意:LValue 的类型可能是指向数组的指针 (e.g., int(*)[3]) 或者指向标量的指针 (e.g., int*) 也能推断
|
||||
Type* LType = builder.getIndexedType(variable->getType(), indices);
|
||||
|
||||
Value* RValue = computeExp(ctx->exp(), LType); // 右值计算
|
||||
Type* RType = RValue->getType();
|
||||
|
||||
// TODO:computeExp处理了类型转换,可以考虑删除判断逻辑
|
||||
if (LType != RType) {
|
||||
ConstantValue *constValue = dynamic_cast<ConstantValue *>(RValue);
|
||||
if (constValue != nullptr) {
|
||||
if (LType == Type::getFloatType()) {
|
||||
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()));
|
||||
// TODO:computeExp处理了类型转换,可以考虑删除判断逻辑
|
||||
if (LType != RType) {
|
||||
ConstantValue *constValue = dynamic_cast<ConstantValue *>(RValue);
|
||||
if (constValue != nullptr) {
|
||||
if (LType == Type::getFloatType()) {
|
||||
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(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() && RType != Type::getFloatType()) {
|
||||
RValue = builder.createItoFInst(RValue);
|
||||
} else if (LType != Type::getFloatType() && RType == Type::getFloatType()) {
|
||||
RValue = builder.createFtoIInst(RValue);
|
||||
}
|
||||
// 如果两者都是同一类型,就不需要转换
|
||||
}
|
||||
} else {
|
||||
if (LType == Type::getFloatType() && RType != Type::getFloatType()) {
|
||||
RValue = builder.createItoFInst(RValue);
|
||||
} else if (LType != Type::getFloatType() && RType == Type::getFloatType()) {
|
||||
RValue = builder.createFtoIInst(RValue);
|
||||
}
|
||||
// 如果两者都是同一类型,就不需要转换
|
||||
}
|
||||
|
||||
builder.createStoreInst(RValue, LValue);
|
||||
}
|
||||
|
||||
builder.createStoreInst(RValue, LValue);
|
||||
|
||||
invalidateExpressionsOnStore(LValue);
|
||||
return std::any();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user