From d1ba140657eb7500d9cb35bb7978bd4c0c9a7565 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Sun, 10 Aug 2025 16:12:09 +0800 Subject: [PATCH] =?UTF-8?q?[midend-llvnirprint]=E4=BF=AE=E6=94=B9=E6=B5=AE?= =?UTF-8?q?=E7=82=B9=E4=B8=BA=E5=AD=97=E9=9D=A2=E5=80=BC=E6=89=93=E5=8D=B0?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8Dassign=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=8E=A8=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/midend/IR.cpp | 34 +++++++--- src/midend/SysYIRGenerator.cpp | 114 +++++++++++++++++++++------------ 2 files changed, 98 insertions(+), 50 deletions(-) diff --git a/src/midend/IR.cpp b/src/midend/IR.cpp index c6a2ce5..b301d82 100644 --- a/src/midend/IR.cpp +++ b/src/midend/IR.cpp @@ -13,16 +13,15 @@ using namespace std; inline std::string getMachineCode(float fval) { - // LLVM IR要求float也使用64位十六进制表示 - // 将float扩展为double精度格式 - double dval = static_cast(fval); - uint64_t bits = *reinterpret_cast(&dval); - // 如果是0,直接返回 - if (bits == 0 || fval == 0.0f) { + if (fval == 0.0f) { return "0x0000000000000000"; } + // 正确的float到double扩展:先转换值,再获取double的位表示 + double dval = static_cast(fval); + uint64_t bits = *reinterpret_cast(&dval); + std::stringstream ss; ss << "0x" << std::hex << std::uppercase << std::setfill('0') << std::setw(16) << bits; return ss.str(); @@ -90,11 +89,18 @@ static inline ostream &printFunctionName(ostream &os, const Function *fn) { static inline ostream &printOperand(ostream &os, const Value *value) { auto constValue = dynamic_cast(value); if (constValue != nullptr) { - // 对于常量,只打印值,不打印类型(类型已经在指令中单独打印了) if (auto constInt = dynamic_cast(constValue)) { os << constInt->getInt(); } else if (auto constFloat = dynamic_cast(constValue)) { - os << getMachineCode(constFloat->getFloat()); + float f = constFloat->getFloat(); + char buffer[64]; + snprintf(buffer, sizeof(buffer), "%.17g", f); + std::string str(buffer); + if (str.find('.') == std::string::npos && str.find('e') == std::string::npos) { + str += ".0"; + } + os << str; + } else if (auto undefVal = dynamic_cast(constValue)) { os << "undef"; } @@ -586,10 +592,18 @@ void ConstantValue::print(std::ostream &os) const { } void ConstantInteger::print(std::ostream &os) const { - os << "i32 " << this->getInt(); + os << this->getInt(); } void ConstantFloating::print(std::ostream &os) const { - os << "float " << getMachineCode(this->getFloat()); + float f = this->getFloat(); + + char buffer[64]; + snprintf(buffer, sizeof(buffer), "%.17g", f); + std::string str(buffer); + if (str.find('.') == std::string::npos && str.find('e') == std::string::npos) { + str += ".0"; + } + os << str; } void UndefinedValue::print(std::ostream &os) const { os << this->getType() << " undef"; diff --git a/src/midend/SysYIRGenerator.cpp b/src/midend/SysYIRGenerator.cpp index a0cd6af..7ff316a 100644 --- a/src/midend/SysYIRGenerator.cpp +++ b/src/midend/SysYIRGenerator.cpp @@ -1297,6 +1297,45 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) { if (dynamic_cast(variable) || dynamic_cast(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(RValue); + if (constValue != nullptr) { + if (LType == Type::getFloatType()) { + if(dynamic_cast(constValue)) { + // 如果是整型常量,转换为浮点型 + RValue = ConstantFloating::get(static_cast(constValue->getInt())); + } else if (dynamic_cast(constValue)) { + // 如果是浮点型常量,直接使用 + RValue = ConstantFloating::get(static_cast(constValue->getFloat())); + } + } else { // 假设如果不是浮点型,就是整型 + if(dynamic_cast(constValue)) { + // 如果是浮点型常量,转换为整型 + RValue = ConstantInteger::get(static_cast(constValue->getFloat())); + } else if (dynamic_cast(constValue)) { + // 如果是整型常量,直接使用 + RValue = ConstantInteger::get(static_cast(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(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(RValue); - if (constValue != nullptr) { - if (LType == Type::getFloatType()) { - if(dynamic_cast(constValue)) { - // 如果是整型常量,转换为浮点型 - RValue = ConstantFloating::get(static_cast(constValue->getInt())); - } else if (dynamic_cast(constValue)) { - // 如果是浮点型常量,直接使用 - RValue = ConstantFloating::get(static_cast(constValue->getFloat())); + // TODO:computeExp处理了类型转换,可以考虑删除判断逻辑 + if (LType != RType) { + ConstantValue *constValue = dynamic_cast(RValue); + if (constValue != nullptr) { + if (LType == Type::getFloatType()) { + if(dynamic_cast(constValue)) { + // 如果是整型常量,转换为浮点型 + RValue = ConstantFloating::get(static_cast(constValue->getInt())); + } else if (dynamic_cast(constValue)) { + // 如果是浮点型常量,直接使用 + RValue = ConstantFloating::get(static_cast(constValue->getFloat())); + } + } else { // 假设如果不是浮点型,就是整型 + if(dynamic_cast(constValue)) { + // 如果是浮点型常量,转换为整型 + RValue = ConstantInteger::get(static_cast(constValue->getFloat())); + } else if (dynamic_cast(constValue)) { + // 如果是整型常量,直接使用 + RValue = ConstantInteger::get(static_cast(constValue->getInt())); + } } - } else { // 假设如果不是浮点型,就是整型 - if(dynamic_cast(constValue)) { - // 如果是浮点型常量,转换为整型 - RValue = ConstantInteger::get(static_cast(constValue->getFloat())); - } else if (dynamic_cast(constValue)) { - // 如果是整型常量,直接使用 - RValue = ConstantInteger::get(static_cast(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(); }