Created
December 6, 2019 03:41
-
-
Save comex/b4184e616ab06dfc0acf204cd3aa1025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyType my_function(int arg1, int arg2); | |
int add_example(int a, int b); | |
template <> struct std::call_abi<add_example> { | |
// caller_stub is force-inlined into everyone who calls add_example: | |
int &caller_stub(int a, int b) { | |
regs.rdi = (uint64_t)a; | |
regs.rsi = (uint64_t)b; | |
callee_stub(); | |
return regs.rax; | |
} | |
void callee_stub() { | |
int a = (int)regs.rdi; | |
int b = (int)regs.rsi; | |
// this calls the actual body of my_function (also inlined): | |
int ret = impl(a, b); | |
regs.rax = ret; | |
} | |
}; | |
MyBigType struct_return_example(int arg); | |
template <> struct std::call_abi<struct_return_example> { | |
MyType &caller_stub(int arg) { | |
MyType *tmp = alloca_in_caller_frame<MyType>(); | |
regs.rdi = (uint64_t)tmp; | |
regs.rsi = (uint64_t)arg; | |
callee_stub(); | |
return *tmp; | |
} | |
void callee_stub() { | |
MyType *tmp = (MyType *)regs.rdi; | |
int arg = (int)regs.rsi; | |
// add guaranteed copy elision for this | |
new (tmp) MyType(impl(arg)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment