I have earlier been able to call Swift methods using a function pointer from C, and providing the appropriate arguments because the calling convention was the same.
Unfortunately this no longer works. This is because Swift has added a "call context" argument that is passed in a separate register (r13) from the normal arguments. For regular method calls, this context is a pointer to self. See here for more info: https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md#calling-convention
So in order to call these methods, I need to be able to reliably set r13 to the appropriate value from C.
How can I do this in clang/llvm?
I do not find a way to declare a register variable to a specific register (gcc syntax for that does not work). So I have code like this:
void (*my_fp)(void *) = find_pointer();
void *instance = find_instance();
void *arg1 = whatever();
asm {
mov r13, [instance]
}
my_fp(arg1);
It might or might not work - sometimes the compiler clobbers r13 immediately by using it for the function pointer. Also I guess it's not particularly safe to just write to that register without the compiler's consent.
Is there a reliable way to do this?
(By the way, I am not able to include the Swift runtime in my code, so I must be able to call from C.)