Skip to content

Instantly share code, notes, and snippets.

@bgaff
Created April 4, 2020 23:46
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 bgaff/742ac889b358050f3173311faa184b63 to your computer and use it in GitHub Desktop.
Save bgaff/742ac889b358050f3173311faa184b63 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <functional>
#include <memory>
#define GET_RETURN_VALUE64(VAR) \
asm ("nop\n nop\n movq %%rax,%0 \n nop\n" \
: "=r" (VAR) \
: \
: "rax" \
);
#define GET_RETURN_VALUE32(VAR) \
asm ("movl %0,%%rax" \
: "=r" (VAR) \
);
#define CONCAT1(X,Y) X##Y
#define CONCAT(X,Y) CONCAT1(X,Y)
#define DEFER(BODY) \
std::unique_ptr<int, std::function<void(int*)>> \
CONCAT(_defer,__LINE__)(reinterpret_cast<int*>(1), \
[&](int*) BODY );
int foo () {
int x = 31;
std::cout << "Hello From Foo." << std::endl;
DEFER({
int64_t returned_code = 0;
GET_RETURN_VALUE64(returned_code);
std::cout << "\tDefer foo x = " << x
<< " returned " << returned_code << std::endl;
});
std::cout << "Foo is returning..." << std::endl;
return 1234;
}
int main ( void ) {
int ret = foo();
std::cout << "foo() = " << ret;
return 55;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment