1

From my understanding, the local parameters, return address among other things of a procedure are pushed on to the stack in memory only to be popped later into the respective registers to be accessed by the CPU because there are limited number of registers which cannot accommodate leaf procedures with a large number of parameters or functions that call other functions.

If there is a tiny leaf function which takes in a few parameters that can be stored in the local registers, will modern compilers still generate code to create a stack frame for the function?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    Depends on the architecture and how you configure the compiler. Note that on some architectures (like x86), the return address is generally never kept in a register as the `call` instruction places it on the stack directly. – fuz Jul 17 '20 at 20:15
  • 1
    Good calling conventions always pass the first few args in registers, e.g. most RISC, and all x86-64 calling conventions. (Assuming they fit, e.g. not large structs by value.) Also, functions don't normally *pop* their stack args (if any), they just load them with normal random access to the callstack. – Peter Cordes Jul 17 '20 at 20:17
  • Questions like this can often be answered by doing your own experiments... – Nate Eldredge Jul 17 '20 at 20:25
  • try godbolt.org – Erik Eidt Jul 17 '20 at 20:42
  • 1
    Short answer: without optimization (-O0), most compilers generate codes for a stack frame. With optimization (-O3), they will not unless it is necessary. – W. Chang Jul 18 '20 at 01:25
  • 1
    Ideally, a tiny leaf function is inlined. – Erik Eidt Jul 18 '20 at 15:07

0 Answers0