I got the 2 following definition that compile (and work) just fine using XCode LLVM-GCC compiler:
#define SAVE_STACK(v)__asm { mov v, ESP }
#define RESTORE_STACK __asm {sub ESP, s }
However when I change the compiler to Apple LLVM I got the following error:
Expected '(' after 'asm'
I replace the {} with () but that doesn't do the trick, I google on that error couldn't find anything useful... anyone?
The __asm {...}
style of inline assembly is non-standard and not supported by clang. Instead C++ specifies inline assembly syntax as asm("...")
, note the quotes. Also clang uses AT&T assembly syntax so the macros would need to be rewritten to be safe.
However, some work has been going on to improve support for Microsoft's non-standard assembly syntax, and Intel style assembly along with it. There's an option -fenable-experimental-ms-inline-asm
that enables what's been done so far, although I'm not sure when it was introduced or how good the support is in the version of clang you're using. A simple attempt with the code you show seems to work with a recent version of clang from the SVN trunk.
#define SAVE_STACK(v)__asm { mov v, ESP }
#define RESTORE_STACK __asm {sub ESP, s }
int main() {
int i;
int s;
SAVE_STACK(i);
RESTORE_STACK;
}
clang++ tmp.cpp -fms-extensions -fenable-experimental-ms-inline-asm -S -o -
.def main;
.scl 2;
.type 32;
.endef
.text
.globl main
.align 16, 0x90
main: # @main
# BB#0: # %entry
pushq %rax
#APP
.intel_syntax
mov dword ptr [rsp + 4], ESP
.att_syntax
#NO_APP
#APP
.intel_syntax
sub ESP, dword ptr [rsp]
.att_syntax
#NO_APP
xorl %eax, %eax
popq %rdx
ret
And the command clang++ tmp.cpp -fms-extensions -fenable-experimental-ms-inline-asm produces an executable that runs.
It does still produce warnings like the following though.
warning: MS-style inline assembly is not supported [-Wmicrosoft]