Mike Kwan
Mike Kwan

Reputation: 24447

How to duplicate a C macro?

I want to make a C macro (TRAMPOLINE_BLOCK) which takes a predefined macro constant (TRAMPOLINE_LENGTH) and writes that many asm volatile nop instructions. For example:

#ifdef __x86_64__
    #define TRAMPOLINE_LENGTH 41
    #define TRAMPOLINE_BLOCK  \
        ({ \
            asm volatile ( \
                    "nop\n" \
                    ...
            );\
        })
#else
    #define TRAMPOLINE_LENGTH ...
    #define TRAMPOLINE_BLOCK ...
#endif

In this case, TRAMPOLINE_BLOCK should have 41 nop instructions. I've been playing around with macro arguments and the such but haven't got it working yet. How would I go about writing such a macro?

Upvotes: 1

Views: 991

Answers (1)

Jens Gustedt
Jens Gustedt

Reputation: 78903

Doing that yourself is relatively complicated.

P99 has a macro P99_DUPL that allows you to do something like you want

 P99_DUPL(41, TRAMPOLINE_BLOCK)

should do the trick. You need a C99 compliant compiler for this.

Upvotes: 1

Related Questions