fuhsnn / slimcc

C11 compiler with GNU / C23 extensions for x86-64 Linux, able to build Python and PostgreSQL
MIT License
24 stars 3 forks source link

ABI requires returned-by-stack aggregate's pointer in `rax` #48

Closed fuhsnn closed 6 months ago

fuhsnn commented 6 months ago

Assembly is needed to test reliably, since the compiler is free to not use the rax value.

main.c

struct S { char t[600]; };
extern struct S fn(void);

#include <stdio.h>
int main(void) {
    struct S s;
    void* ptr = &s;
    asm (
        "mov %0, %%rdi;"
        "call fn;"
        "mov %%rax, %0;"
        : "+r"(ptr)
        :
        : "rax", "rdi", "rsi", "rdx", "rcx", "r8", "r9"
    );
    printf("expect 1, got %d\n", ((void*)&s == ptr));
}

fn.c

struct S { char t[600]; };
struct S fn(void) { return (struct S){0}; }

test

cc main.c -c 
chibicc main.o fn.c