Created
April 4, 2020 23:46
-
-
Save bgaff/742ac889b358050f3173311faa184b63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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