[llvm] f1c21fa - [AArch64] Switch magic numbers to library functions in fixup

Daniel Hoekwater via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 27 14:30:02 PDT 2023


Author: Daniel Hoekwater
Date: 2023-07-27T21:27:11Z
New Revision: f1c21faeb2f66822895271c37de6d4b0ed6a5dce

URL: https://github.com/llvm/llvm-project/commit/f1c21faeb2f66822895271c37de6d4b0ed6a5dce
DIFF: https://github.com/llvm/llvm-project/commit/f1c21faeb2f66822895271c37de6d4b0ed6a5dce.diff

LOG: [AArch64] Switch magic numbers to library functions in fixup

Using masks and bounds that are magic numbers means that defects
in bounds checking and offset masking are subtle and hard to
detect. https://reviews.llvm.org/D152841 is an example of this
type of defect. Switching to clearly readable library functions
makes defects less obfuscated.

Differential Revision: https://reviews.llvm.org/D152843

Added: 
    

Modified: 
    llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
index a7933c00f5073d..59cf53e85c824a 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
@@ -181,14 +181,14 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
     if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
       Value &= 0xfff;
     // Unsigned 12-bit immediate
-    if (Value >= 0x1000)
+    if (!isUInt<12>(Value))
       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
     return Value;
   case AArch64::fixup_aarch64_ldst_imm12_scale2:
     if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
       Value &= 0xfff;
     // Unsigned 12-bit immediate which gets multiplied by 2
-    if (Value >= 0x2000)
+    if (!isUInt<13>(Value))
       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
     if (Value & 0x1)
       Ctx.reportError(Fixup.getLoc(), "fixup must be 2-byte aligned");
@@ -197,7 +197,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
     if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
       Value &= 0xfff;
     // Unsigned 12-bit immediate which gets multiplied by 4
-    if (Value >= 0x4000)
+    if (!isUInt<14>(Value))
       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
     if (Value & 0x3)
       Ctx.reportError(Fixup.getLoc(), "fixup must be 4-byte aligned");
@@ -206,7 +206,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
     if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
       Value &= 0xfff;
     // Unsigned 12-bit immediate which gets multiplied by 8
-    if (Value >= 0x8000)
+    if (!isUInt<15>(Value))
       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
     if (Value & 0x7)
       Ctx.reportError(Fixup.getLoc(), "fixup must be 8-byte aligned");
@@ -215,7 +215,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
     if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
       Value &= 0xfff;
     // Unsigned 12-bit immediate which gets multiplied by 16
-    if (Value >= 0x10000)
+    if (!isUInt<16>(Value))
       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
     if (Value & 0xf)
       Ctx.reportError(Fixup.getLoc(), "fixup must be 16-byte aligned");
@@ -306,7 +306,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
   }
   case AArch64::fixup_aarch64_pcrel_branch14:
     // Signed 16-bit immediate
-    if (SignedValue > 32767 || SignedValue < -32768)
+    if (!isInt<16>(SignedValue))
       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
     // Low two bits are not encoded (4-byte alignment assumed).
     if (Value & 0x3)
@@ -315,7 +315,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
   case AArch64::fixup_aarch64_pcrel_branch26:
   case AArch64::fixup_aarch64_pcrel_call26:
     // Signed 28-bit immediate
-    if (SignedValue > 134217727 || SignedValue < -134217728)
+    if (!isInt<28>(SignedValue))
       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
     // Low two bits are not encoded (4-byte alignment assumed).
     if (Value & 0x3)


        


More information about the llvm-commits mailing list