Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use x8 register for result struct address on aarch64 #124

Open
tyfkda opened this issue Sep 14, 2023 · 0 comments
Open

Use x8 register for result struct address on aarch64 #124

tyfkda opened this issue Sep 14, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@tyfkda
Copy link
Owner

tyfkda commented Sep 14, 2023

Unlike x64 or RISCV64, aarch64 uses %x8 (instead of first argument register) to pass the function result struct address:

sub.h:

typedef struct {
    long long x, y, z;
} FooStruct;

extern FooStruct return_struct(long long x, long long y, long long z);

sub.c:

#include "sub.h"

FooStruct return_struct(long long x, long long y, long long z) {
    FooStruct s = {.x = x, .y = y, .z = z};
    return s;
}
$ gcc -S -o- -O2 -fno-asynchronous-unwind-tables sub.c
        .section        __TEXT,__text,regular,pure_instructions
        .build_version macos, 14, 0     sdk_version 15, 2
        .globl  _return_struct                  ; -- Begin function return_struct
        .p2align        2
_return_struct:                         ; @return_struct
        .cfi_startproc
; %bb.0:
        stp     x0, x1, [x8]
        str     x2, [x8, #16]
        ret
        .cfi_endproc
                                        ; -- End function
.subsections_via_symbols

main.c:

#include "sub.h"

FooStruct return_struct(long long x, long long y, long long z) {
    FooStruct s = {.x = x, .y = y, .z = z};
    return s;
}
$ gcc -S -o- -O2 -fno-asynchronous-unwind-tables main.c
        .section        __TEXT,__text,regular,pure_instructions
        .build_version macos, 14, 0     sdk_version 15, 2
        .globl  _main                           ; -- Begin function main
        .p2align        2
_main:                                  ; @main
        .cfi_startproc
; %bb.0:
        sub     sp, sp, #48
        stp     x29, x30, [sp, #32]             ; 16-byte Folded Spill
        add     x29, sp, #32
        .cfi_def_cfa w29, 16
        .cfi_offset w30, -8
        .cfi_offset w29, -16
        add     x8, sp, #8                           ;########## FooStruct s
        mov     w0, #1                          ; =0x1
        mov     w1, #2                          ; =0x2
        mov     w2, #3                          ; =0x3
        bl      _return_struct
        ldr     w8, [sp, #8]
        ldr     w9, [sp, #16]
        ldr     w10, [sp, #24]
        add     w8, w9, w8
        add     w0, w8, w10
        ldp     x29, x30, [sp, #32]             ; 16-byte Folded Reload
        add     sp, sp, #48
        ret
        .cfi_endproc
                                        ; -- End function
.subsections_via_symbols
@tyfkda tyfkda added the enhancement New feature or request label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant