GCC Macro Extension Arguments Inside a String

I have such a situation.

#define PRE 0xF1 #define SR0 0B0000 #define SR1 0B0001 #define SR2 0B0010 #define SR3 0B0011 #define VIOTA(A0) asm(".byte PRE, A0") int main() { VIOTA(SR1); return 0; } 

I have a top-level macro that expands, however the extension contains more macros. They do not expand and cause some problems.

The behavior that I desire is that the final extension

asm (".bot 0xF1, 0B0000")

Here, internal macros have been expanded. I'm really not sure what I'm doing wrong. Any tips?

+4
source share
3 answers
 #define S(x) #x #define SX(x) S(x) #define VIOTA(A0) asm(".byte " SX(PRE) ", " SX(A0)) 

See details here: C Preprocessor, Macro Result String

+8
source

Use the string operator # to convert tokens to strings. However, since the gating operator can only be applied to macro parameters, you need to add additional macro layers:

 #define PRE 0xF1 #define SR0 0B0000 #define SR1 0B0001 #define SR2 0B0010 #define SR3 0B0011 #define VIOTA(A0) VIOTA_HELPER1(PRE, A0) #define VIOTA_HELPER1(PRE, A0) VIOTA_HELPER2(PRE, A0) #define VIOTA_HELPER2(PRE, A0) asm(".byte" #PRE ", " #A0) int main(void) { VIOTA(SR1); return 0; } 

After pre-processing, this expands to this:

 int main(void) { asm(".byte " "0xF1" ", " "0B0001"); return 0; } 

String constants are concatenated at compile time, so this is equivalent to asm(".byte 0xF1, 0B0001"); .

+1
source

You do the PRE part of the string that is passed to asm (). Macros in rows do not expand.

This seems to work for me:

  #define PRE 0xF1 #define SR0 0B0000 #define SR1 0B0001 #define SR2 0B0010 #define SR3 0B0011 #define str(s) #s #define VIOTA(PRE, A0) asm( ".byte " str(PRE) ", " str(A0) ) int main() { VIOTA(PRE, SR1); return 0; } 
0
source

All Articles