Search code examples
cgccavravr-gcc

Why is this code generated to dereference a float pointer?


I have this file float_deref.c that generates strange machine code:

float float_deref(float *ptr)
{
        return *ptr;
}

Here is the assembly generated by avr-gcc -mmcu=atmega328p -O3 -S -o - float_deref.c:

    .file   "float_deref.c"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
    .text
.global float_deref
    .type   float_deref, @function
float_deref:
    push r28
    push r29
    rcall .
    rcall .
    in r28,__SP_L__
    in r29,__SP_H__
/* prologue: function */
/* frame size = 4 */
/* stack size = 6 */
.L__stack_usage = 6
    movw r30,r24
    ld r18,Z
    ldd r19,Z+1
    ldd r20,Z+2
    ldd r21,Z+3
    movw r24,r20
    movw r22,r18
/* epilogue start */
    pop __tmp_reg__
    pop __tmp_reg__
    pop __tmp_reg__
    pop __tmp_reg__
    pop r29
    pop r28
    ret
    .size   float_deref, .-float_deref
    .ident  "GCC: (Homebrew AVR GCC 9.3.0) 9.3.0"

I don't get why the code pushes four extra bytes onto the stack and loads the stack pointer into Y. Y is never used in the function as far as I can tell.


Solution

  • It's a known problem in v9+, see https://gcc.gnu.org/PR90706