Why is 0 moved to stack when using return value?

前端 未结 4 1293
逝去的感伤
逝去的感伤 2020-12-18 06:44

I\'m experimenting disassembling clang binaries of simple C programs (compiled with -O0), and I\'m confused about a certain instruction that gets g

4条回答
  •  鱼传尺愫
    2020-12-18 07:21

    movl   $0x0,-0x4(%rbp)
    

    This instruction stores 0 at %rbp - 4. It seems that clang allocates a hidden local variable for an implicit return value from main.

    From the clang mailing list:

    Yes. We allocate an implicit local variable to hold the return value; return statements then just initialize the return slot and jump to the epilogue, where the slot is loaded and returned. We don't use a phi because the control flow for getting to the epilogue is not necessarily as simple as a simple branch, due to cleanups in local scopes (like C++ destructors).

    Implicit return values like main's are handled with an implicit store in the prologue.

    Source: http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-February/019767.html

提交回复
热议问题