Skip to content

Instantly share code, notes, and snippets.

@comex
Created December 6, 2019 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comex/ee2479f24325823a1d4942e5a46868a0 to your computer and use it in GitHub Desktop.
Save comex/ee2479f24325823a1d4942e5a46868a0 to your computer and use it in GitHub Desktop.
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