[Solved] Indexing args as an array in the Windows x86-64 ABI [closed]


Jester’s suggestion to write it in C is probably a good one, esp. if it can be inlined into calls where some of the args are compile-time constants. Your example use-case passes mostly compile-time-constant args, including the function pointer. Any decent optimizing compiler will inline this and optimize away the indirection into just a normal function call with the right args. Make sure you put the definition somewhere it can be inlined.


However, if you can’t get the compiler to make nice code:

Indexing arguments as an array is the only piece of functionality that’s non-obvious how to implement in the 64bit ABI where some args are in regs.

The Windows 64bit calling convention provides space for storing the 4 register args right below the stack args (the shadow space), so you actually can create an array of args you can index with only at most 4 instructions (store the args into those slots). You don’t need to special-case the first 4 args.

Don’t forget to put the outgoing args into regs instead of the stack, too, and leave shadow-space for the function you call.

As Ross Ridge points out, make sure you include directives to create SEH unwind info in the stand-alone asm. This is another good reason to favour a pure C++ solution, esp. if the number of args is limited to a small number.

See the x86 tag wiki for links to calling conventions and so on.

I’m not a fan of the Windows calling convention in general, but it does make implementing var-args functions simple. I’m pretty sure that’s why the “shadow space” exists in the ABI.

8

solved Indexing args as an array in the Windows x86-64 ABI [closed]