Skip to content

[PowerPC] Parameters are not stored in stack as per ABI #120700

Open
@IshwaraK

Description

@IshwaraK

For the below example, we can observe that the function parameters are not stored in stack slot as per ABI, instead being stored in other area.
The example is available here https://godbolt.org/z/Gfevrzh93

#include <stdio.h>

int __attribute__((noinline))  foo(long* y) {
  long **x = &y - 6; // needs to be &y+2 in clang, but as per ABI that is not recorded 
  printf("return address: %p\n", *(x+2));
  return **x;
}

/*
int __attribute__((noinline))  foo(long* y) {
  long **x = __builtin_frame_address(1);
  printf("return address: %p\n", *(x+2));
  return **x;
}
*/

int __attribute__((noinline))  bar() {
  long y = 200;
  return 1 + foo(&y);
}

int __attribute__((noinline))  xyz() {
  return 2 + bar();
}

int main() {
  return xyz();
}

The below is the explanation of the discrepancies.


// PowerPC gcc compiler at -O0
.L.foo(long*):
    mflr 0
    std 0,16(1)
    std 31,-8(1)
    stdu 1,-144(1)
    mr 31,1
    std 3,192(31) <--- Right: Stored at parameter area of the caller
    addi 9,31,144 <--- Got the frame pointer
    std 9,112(31)
    ld 9,112(31)
    addi 9,9,16 
    ld 9,0(9) <--- got the return address
    mr 4,9
    addis 3,2,.LC0@toc@ha
    addi 3,3,.LC0@toc@l
    bl printf

// PowerPc clang Compiler  at -O0
.Lfunc_begin0:
    mflr 0
    std 31, -8(1)
    stdu 1, -144(1)
    std 0, 160(1)
    mr      31, 1
    std 3, 128(31) <--- Wrong: Stored in SAVE reg area instead of caller parameter area
    addi 3, 31, 128
    addi 3, 3, -48
    std 3, 120(31)
    ld 3, 120(31)
    ld 4, 16(3)
    addis 3, 2, .L.str@toc@ha
    addi 3, 3, .L.str@toc@l
    bl printf

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions