Home > OS >  Is frame-pointer necessary for riscv assembly?
Is frame-pointer necessary for riscv assembly?

Time:12-02

I am working on a project which requires me to generate asm code from llvm ir.

When I using llc to generate code directly from .ll file, the assembly doesn't have frame pointer fp. However, when I use the riscv-unknow-elf-gcc to compile .cpp file, it does have a frame pointer.

Searching online, I found that there is -fomit-frame-pointer argument when compiling.

I just thought that the frame pointer is unnecessary for riscv asm, as we actually know the begin and the end of the function frame. And using a frame pointer to express a stack variable has no difference with using the stack pointer sp.

CodePudding user response:

No, a frame pointer is not strictly necessary in MIPS or RISC V for ordinary function calling and returning.

However, if you want to support stack unwind, it is helpful, though in some other environments, static data provides the information necessary for stack unwind without the use of the frame pointer at runtime.

Stack unwind is generally required if you want to be able to throw and catch exceptions, depending on the language.


Some instructors teaching MIPS or RISC V want you to learn the frame pointer concept, since it is commonly used on x86 — however, on x86 the frame pointer is sometimes used because of peculiarities of the instruction set.  Namely, that, frequent pushes and pops are done which make the stack harder to follow relative to the stack pointer that moves compared to the frame pointer that doesn't, and, also that the instruction set defaults two register to use the stack segment register, namely, the frame pointer and stack pointer registers, while the original x86 also did not have sp offset addressing, only bp (frame pointer) offset.


So for all the above reasons, some MIPS and RISC V instructional materials teach frame pointer concepts.

  • Related