Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #91117

Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++
Subject Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler
Date 2023-07-17 17:27 +0200
Organization A noiseless patient Spider
Message-ID <u93ml4$19efq$1@dont-email.me> (permalink)
References <74f0d07f-b1cd-42df-bf6d-fb0c701b3165n@googlegroups.com>

Show all headers | View raw


Why not simply accept the ARM ABI for that ?
For me it doesn't matter where the return value pointer is stored.

Am 17.07.2023 um 16:24 schrieb Frederick Virchanza Gotham:
> I'm trying to write a very simple function in two or three aarch64 instructions as 'inline assembler' inside a C++ source file.
> 
> With the aarch64 calling convention on Linux, if a function returns a very large struct by value, then the address of where to store the return value is passed in the X8 register. This is out of the ordinary as far as calling conventions go. Every other calling convention, for example System V x86_64, Microsoft x64, cdecl, stdcall, arm32, pass the address of the return value in the first parameter. So for example with x86_64 on Linux, the RDI register contains the address of where to store the very large struct.
> 
> I want to try emulate this behaviour on aarch64 on Linux. When my assembler function is entered, I want it to do two things:
> (1) Put the address of the indirect return object into the first parameter register, i.e. move X8 to X0
> (2) Jump to a location specified by a global function pointer
> 
> So here's how I think my assembler function should look:
> 
>      __asm("Invoke:       \n"
>            " mov x0, x8   \n" // move return value address into 1st parameter
>            " mov x9, f    \n" // Load address of code into register
>            " br  x9       \n" // Jump to code
>      );
> 
> I don't know what's wrong here but it doesn't work. In the following complete C++ program, I use the class 'std::mutex' as it's a good example of a class that can't be copied or moved (I am relying on mandatory Return Value Optimisation).
> 
> Here is my entire program in one C++ file, could someone please help me write the assembler function properly? Am I supposed to be using the ADRP and LDR instructions instead of MOV?
> 
> #include <mutex>                  // mutex
> #include <iostream>               // cout, endl
> using std::cout, std::endl;
> 
> void (*f)(void) = nullptr;
> 
> extern "C" void Invoke(void);
> 
> __asm("Invoke:                    \n"
>        "    mov  x0, x8            \n"  // move return value address into 1st parameter
>        "    mov  x9, f             \n"  // Load address of code into register
>        "    br   x9                \n"  // Jump to code
> );
> 
> void Func(std::mutex *const p)
> {
>      cout << "Address of return value: " << p << endl;
> }
> 
> int main(void)
> {
>      f = (void(*)(void))Func;
> 
>      auto const p = reinterpret_cast<std::mutex (*)(void)>(Invoke);
> 
>      auto retval = p();
> 
>      cout << "Address of return value: " << &retval << endl;
> }

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-07-17 07:24 -0700
  Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Bonita Montero <Bonita.Montero@gmail.com> - 2023-07-17 17:27 +0200
    Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-07-17 09:13 -0700
      Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Bonita Montero <Bonita.Montero@gmail.com> - 2023-07-17 18:21 +0200
      Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-07-17 11:30 -0700
    Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler scott@slp53.sl.home (Scott Lurndal) - 2023-07-17 16:25 +0000
  Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-07-17 10:00 -0700
    Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler scott@slp53.sl.home (Scott Lurndal) - 2023-07-17 17:07 +0000
      Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-07-17 10:31 -0700
        Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler scott@slp53.sl.home (Scott Lurndal) - 2023-07-17 17:51 +0000
        Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Bonita Montero <Bonita.Montero@gmail.com> - 2023-07-17 19:55 +0200
    Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-07-17 11:34 -0700
      Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-07-18 01:34 -0700
        Re: aarch64 - 64-Bit ARM - return by value - GNU g++ inline assembler Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-07-18 03:55 -0700

csiph-web