From 769861d3da5fb910708a0c1d726b49fbee7142c0 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Tue, 3 Dec 2024 07:42:13 -0500 Subject: [PATCH] Added proper uint64->float conversion --- IDEHelper/Backend/BeMCContext.cpp | 28 +++++++++++++++++++++++++++- IDEHelper/Backend/BeModule.cpp | 1 + IDEHelper/Backend/BeModule.h | 1 + 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/IDEHelper/Backend/BeMCContext.cpp b/IDEHelper/Backend/BeMCContext.cpp index a343bd9e..5b482520 100644 --- a/IDEHelper/Backend/BeMCContext.cpp +++ b/IDEHelper/Backend/BeMCContext.cpp @@ -6187,7 +6187,9 @@ uint8 BeMCContext::GetJumpOpCode(BeCmpKind cmpKind, bool isLong) case BeCmpKind_NB: // JNB return 0x83; case BeCmpKind_NO: // JNO - return 0x81; + return 0x81; + case BeCmpKind_Sign: // JS + return 0x88; } } else @@ -6228,6 +6230,8 @@ uint8 BeMCContext::GetJumpOpCode(BeCmpKind cmpKind, bool isLong) return 0x73; case BeCmpKind_NO: // JNO return 0x71; + case BeCmpKind_Sign: // JS + return 0x78; } } @@ -16585,6 +16589,28 @@ void BeMCContext::Generate(BeFunction* function) vregInfo->mIsExpr = true; vregInfo->mRelTo = mcValue; } + else if ((toType->IsFloat()) && (fromType->IsIntable()) && (fromType->mSize == 8)) + { + // uint64 to float - basically, when we are signed then we shift down one bit (so it's unsigned) and then double the result. There's a 1-bit correction factor. + AllocInst(BeMCInstKind_Test, mcValue, mcValue); + AllocInst(BeMCInstKind_CondBr, BeMCOperand::FromLabel(mCurLabelIdx), BeMCOperand::FromCmpKind(BeCmpKind_Sign)); + AllocInst(BeMCInstKind_MovSX, toValue, mcValue); + AllocInst(BeMCInstKind_Br, BeMCOperand::FromLabel(mCurLabelIdx + 1)); + CreateLabel(); + + auto temp0 = AllocVirtualReg(GetType(mcValue)); + CreateDefineVReg(temp0); + auto temp1 = AllocVirtualReg(GetType(mcValue)); + CreateDefineVReg(temp1); + AllocInst(BeMCInstKind_Mov, temp0, mcValue); + AllocInst(BeMCInstKind_Shr, temp0, BeMCOperand::FromImmediate(1)); + AllocInst(BeMCInstKind_Mov, temp1, mcValue); + AllocInst(BeMCInstKind_And, temp1, BeMCOperand::FromImmediate(1)); + AllocInst(BeMCInstKind_Or, temp0, temp1); + AllocInst(BeMCInstKind_MovSX, toValue, temp0); + AllocInst(BeMCInstKind_Add, toValue, toValue); + CreateLabel(); + } else { bool doSignExtension = (toType->IsIntable()) && (fromType->IsIntable()) && (toType->mSize > fromType->mSize) && (castedInst->mToSigned) && (castedInst->mValSigned); diff --git a/IDEHelper/Backend/BeModule.cpp b/IDEHelper/Backend/BeModule.cpp index 488176ca..39101d4a 100644 --- a/IDEHelper/Backend/BeModule.cpp +++ b/IDEHelper/Backend/BeModule.cpp @@ -1740,6 +1740,7 @@ void BeDumpContext::ToString(StringImpl& str, BeCmpKind cmpKind) case BeCmpKind_OGE: str += "oge"; return; case BeCmpKind_NB: str += "nb"; return; case BeCmpKind_NO: str += "no"; return; + case BeCmpKind_Sign: str += "sign"; return; default: str += "???"; } diff --git a/IDEHelper/Backend/BeModule.h b/IDEHelper/Backend/BeModule.h index 2778a862..5aa1e43d 100644 --- a/IDEHelper/Backend/BeModule.h +++ b/IDEHelper/Backend/BeModule.h @@ -874,6 +874,7 @@ enum BeCmpKind BeCmpKind_OGE, BeCmpKind_NB, BeCmpKind_NO, + BeCmpKind_Sign, }; class BeCmpInst : public BeInst