Skip to content

Commit b05dece

Browse files
kito-chengjrtc27
andcommitted
Clarify the range of medlow code model on rv64
Range of medlow code model is highest 2GiB and lowest 2GiB, but the spec only say lowest 2GiB. And also put the detail to show how those range come from. Ref: [1] https://www.sifive.com/blog/all-aboard-part-4-risc-v-code-models Co-authored-by: Jessica Clarke <[email protected]>
1 parent 8ad4cdd commit b05dece

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

riscv-elf.adoc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ in the current code model.
2121
=== Small code model
2222

2323
The small code model, or `medlow`, allows the code to address the whole RV32
24-
address space or the lower 2 GiB of the RV64 address space.
24+
address space or the lower 2 GiB and highest 2GiB of the RV64 address space
25+
(`0xFFFFFFFF7FFFF800` ~ `0xFFFFFFFFFFFFFFFF` and `0x0` ~ `0x000000007FFFF7FF`).
2526
By using the instructions `lui` and `ld` or `st`, when referring to an object, or
2627
`addi`, when calculating an address literal, for example,
2728
a 32-bit address literal can be produced.
@@ -44,6 +45,20 @@ an address in the `medlow` code model.
4445
addi a0, a0, %lo(symbol)
4546
----
4647

48+
NOTE: The ranges on RV64 are not `0x0` ~ `0x000000007FFFFFFF` and
49+
`0xFFFFFFFF80000000` ~ `0xFFFFFFFFFFFFFFFF` due to RISC-V's sign-extension of
50+
immediates; the following code fragments show where the ranges come from:
51+
[,asm]
52+
----
53+
# Largest postive number:
54+
lui a0, 0x7ffff # a0 = 0x7ffff000
55+
addi a0, 0x7ff # a0 = a0 + 2047 = 0x000000007FFFF7FF
56+
57+
# Smallest negative number:
58+
lui a0, 0x80000 # a0 = 0xffffffff80000000
59+
addi a0, a0, -0x800 # a0 = a0 + -2048 = 0xFFFFFFFF7FFFF800
60+
----
61+
4762
=== Medium code model
4863

4964
The medium code model, or `medany`, allows the code to address the range

0 commit comments

Comments
 (0)