Skip to content

Commit

Permalink
[JITLink][RISCV] Homogenize immediate handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hahnjo committed Jan 3, 2023
1 parent e7d432f commit 850adc1
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,13 @@ class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
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: {
Expand Down Expand Up @@ -280,11 +281,11 @@ class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
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: {
Expand Down Expand Up @@ -312,10 +313,10 @@ class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
// 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: {
Expand Down

0 comments on commit 850adc1

Please sign in to comment.