What stack alignment does the RISC-V standard ABI require?
The standard ABI requires sp to be aligned to a 128-bit boundary on procedure entry and remain aligned during execution.
sp is the stack pointer used by the RISC-V standard calling convention. In the standard ABI, the stack grows toward lower addresses; sp must be aligned to a 128-bit boundary on procedure entry and remain aligned throughout execution.
The psABI marks sp as preserved across calls: its entry value must be restored on return. Adjusting sp to build a stack frame must still satisfy the standard ABI requirement for 128-bit alignment on procedure entry and throughout execution.
The psABI names x2 as sp and marks it preserved across calls. A function may move sp while building its frame, but must restore it before returning.
sp / x2 is the stack pointer in the standard calling convention. The psABI marks it preserved across calls; a function may move sp to build a frame, but must restore it and maintain the standard ABI's 128-bit alignment rule.
The standard ABI requires the stack pointer to be aligned to a 128-bit boundary on procedure entry and remain aligned during execution.
A callee may move sp to build a frame, but must restore it before return.
Before calling another function, the current function must leave the stack in an ABI-compliant state.
addi sp, sp, -16
sd ra, 8(sp)
sd s0, 0(sp)
addi s0, sp, 16
...
ld s0, 0(sp)
ld ra, 8(sp)
addi sp, sp, 16
retsp is the x2 stack pointer; in the standard ABI the stack grows downward and sp is 128-bit aligned on procedure entry. s0/fp is x8; the psABI says a frame pointer is optional, but if one exists it must reside in x8/s0. Both are preserved ABI state, but sp tracks the stack position while s0/fp has a frame-pointer role only when that convention is used.
Do not use sp as an ordinary scratch register. The standard ABI requires 128-bit alignment on procedure entry and continued alignment during execution; handwritten code that adjusts sp must maintain those conditions.
The standard ABI requires sp to be aligned to a 128-bit boundary on procedure entry and remain aligned during execution.