Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top