문제

I have this C macro:

#define syscall(number) \
({ \
    asm volatile ( \
        ".set noreorder\n" \
        "nop\n" \
        "syscall "#number"\n" \
    );\
})

It works great when I call it with integer:

 syscall(5);

However when I do something like this:

 #define SYSCALL_PUTC 5

 syscall(SYSCALL_PUTC);

I get this error:

Error: Instruction syscall requires absolute expression

How do I work around this? I don't want to have my code littered with magic numbers.

도움이 되었습니까?

해결책

Use stringification like this:

#define xstr(s) str(s)
#define str(s) #s
#define syscall(number) \
({ \
    asm volatile ( \
        ".set noreorder\n" \
        "nop\n" \
        "syscall "xstr(number)"\n" \
    );\
})

For more info on stringification, see this gcc page:

http://gcc.gnu.org/onlinedocs/cpp/Stringification.html

다른 팁

A bit of a hack, but not THAT much: make a copy of your original syscall macro for each syscall number, naming each for that syscall. Thus, you'd have a macro named SYSCALL_PUTC which would expand into the same as syscall(5) would have.

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