Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JALR encode the LSB of the offset?

We know that jal specifies a 21-bit offset. However, it does not encode a 21-bit offset but a 20-bit one. The reason is that the least significant bit of an address is always zero because the smallest possible RISC-V instruction is 2 bytes, so this bit is not encoded in the instruction.

By encoding the offset this way it can provide a jumping range of ±1MiB. If jal did encode the LSB, it would offer just a ±512KiB jumping range.

However, the jalr instruction, which specifies a 12-bit offset, does encode the LSB. This reduces the jumping range to ±2kiB (instead of ±4kiB). I know that jalr uses the I-type format, which is the same as addi and the LSB of the immediate has to be encoded for this kind of instructions. However, I see no reason why the least significant bit has to be encoded for jalr.

like image 714
Lui Avatar asked Dec 25 '19 18:12

Lui


People also ask

What is the JALR instruction used for?

The jump-and-link-register instruction ( JALR ) is the union of JAL and JR , meaning that it transfers control to the address in a specified register, and stores the return address in the register file. However, unlike JAL , JALR allows the programmer to specify the destination register of the return address.

What is the least significant bit in a RISC v word?

Save this answer. Show activity on this post. The smallest instruction in RISC-V is 2 bytes. No valid RISC-V instruction starts at an odd instruction, so there would be no purpose in allowing the least significant bit to be 1.

What is JAL in RISC V?

As per definition in riscv-spec-2.2 : The indirect jump instruction JALR (jump and link register) uses the I-type encoding. The target. address is obtained by adding the 12-bit signed I-immediate to the register rs1, then setting the. least-signi cant bit of the result to zero.

What range of addresses can be reached using the JAL instruction in RISC V?

What range of addresses can be reached using the RISC-Vjump-and-link(jal) instruction?(In other words, what is the set of possible values for the PC after the jump instruction executes?)injalinstruction, the immediate field has 20 bits.


2 Answers

JALR is used for two relatively distinct purposes:

  • indirect branches, e.g.
    • function return
    • indirect function calls (e.g. function pointers; vtables/virtual dispatch), and,
  • mid-far branches (in a two instruction sequence, with 32-bit pc-relative range).

For the former, indirect branches, the immediate value is always 0, which is to say that effectively no immediate is used at all!

For the latter, this instruction is used in conjunction with AUIPC, which forms the upper 20 bits of pc-relative addressing — and JALR is used then in conjunction to form the lower 12-bits, for a total pc-relative offset of 32-bits.

However, AUIPC is used both for far branches, as well as for pc-relative data access.  Thus, they both share the 12-bit offset — the load/store's using their 12-bit immediate, and the JALR following suit by also using a 12-bit immediate field just like loads & stores.  The designers chose to share AUIPC rather than to have a two different AUIPC for these two uses (reference from code-to-code vs. reference from code-to-data).

In summary, the range of JALR is mostly not important, as long as it can supply the remaining 12-bits to complement AUIPC's 20 bits.  Sure there are other approaches, but this does have the advantage of reusing and requiring only one AUIPC instruction.

like image 89
Erik Eidt Avatar answered Oct 09 '22 10:10

Erik Eidt


The rationale has been stated in RISC-V spec:

Note that the JALR instruction does not treat the 12-bit immediate as multiples of 2 bytes, unlike the conditional branch instructions. This avoids one more immediate format in hardware. In practice, most uses of JALR will have either a zero immediate or be paired with a LUI or AUIPC, so the slight reduction in range is not significant.

Clearing the least-significant bit when calculating the JALR target address both simplifies the hardware slightly and allows the low bit of function pointers to be used to store auxiliary information. Although there is potentially a slight loss of error checking in this case, in practice jumps to an incorrect instruction address will usually quickly raise an exception.

like image 22
phuclv Avatar answered Oct 09 '22 11:10

phuclv