[JITLink][RISCV] Homogenize immediate handling
authorJonas Hahnfeld <hahnjo@hahnjo.de>
Sun, 1 Jan 2023 18:07:32 +0000 (19:07 +0100)
committerJonas Hahnfeld <hahnjo@hahnjo.de>
Tue, 3 Jan 2023 16:18:39 +0000 (17:18 +0100)
Name the variables based on which part of the immediate value is
extracted, as it was already done for R_RISCV_JAL. This makes it
much easier to compare the logic with the spec.

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

llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp

index 3fcc45e..79e3be6 100644 (file)
@@ -205,12 +205,13 @@ private:
         return makeTargetOutOfRangeError(G, B, E);
       if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2)))
         return makeAlignmentError(FixupAddress, Value, 2, E);
-      uint32_t Imm31_25 =
-          extractBits(Value, 5, 6) << 25 | extractBits(Value, 12, 1) << 31;
-      uint32_t Imm11_7 =
-          extractBits(Value, 1, 4) << 8 | extractBits(Value, 11, 1) << 7;
+      uint32_t Imm12 = extractBits(Value, 12, 1) << 31;
+      uint32_t Imm10_5 = extractBits(Value, 5, 6) << 25;
+      uint32_t Imm4_1 = extractBits(Value, 1, 4) << 8;
+      uint32_t Imm11 = extractBits(Value, 11, 1) << 7;
       uint32_t RawInstr = *(little32_t *)FixupPtr;
-      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
+      *(little32_t *)FixupPtr =
+          (RawInstr & 0x1FFF07F) | Imm12 | Imm10_5 | Imm4_1 | Imm11;
       break;
     }
     case R_RISCV_JAL: {
@@ -280,11 +281,11 @@ private:
       int64_t Value = RelHI20->getTarget().getAddress() +
                       RelHI20->getAddend() - E.getTarget().getAddress();
       int64_t Lo = Value & 0xFFF;
-      uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
-      uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
+      uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25;
+      uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7;
       uint32_t RawInstr = *(little32_t *)FixupPtr;
 
-      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
+      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0;
       break;
     }
     case R_RISCV_HI20: {
@@ -312,10 +313,10 @@ private:
       // with current relocation R_RISCV_LO12_S. So here may need a check.
       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
       int64_t Lo = Value & 0xFFF;
-      uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
-      uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
+      uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25;
+      uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7;
       uint32_t RawInstr = *(little32_t *)FixupPtr;
-      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
+      *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0;
       break;
     }
     case R_RISCV_ADD8: {