문제

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]

다른 팁

I have a problem using the XCode development environment the following code compiled correctly. Switching to my makefile I received the following error message Expected '(' after 'asm'

#define DebugBreak()  { __asm { int 3 }; }
int main(int argc, const char *argv[])
{
    DebugBreak();
}

Note that the definition for DebugBreak() came from my code that compiled under Visual Studio.

The way that I fix this in my make file was to added the argument -fasm-blocks

CFLAGS += -std=c++11 -stdlib=libc++ -O2 -fasm-blocks

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top