Click to See Complete Forum and Search --> : Preprocessor metaprogramming?


dude_1967
July 24th, 2007, 05:57 PM
Hi there,

I got stuck on this one today while reviewing some deeply embedded operating system code.

The developer was clearing the instruction pipeline in a deeply embedded microcontroller operating system and he used assembler nop's to do it. This is certainly legitimate, assuming that the underlying CPU architecture has no serializing instructions, which it did not. He was supporting several architectures, each one of which had different pipeline stages and it was ugly:


#ifdef(Architecture_Something)
_asm("nop")
_asm("nop")
#elif defined(Architecture_Something_Else)
_asm("nop")
_asm("nop")
_asm("nop")
_asm("nop")
#endif


So after this long lead-in...

Is it possible to create a macro 'nopN' which generates a sequence of N instances of a repeated 'nop' line? I know that C++ template metaprogramming would do it with no problem. But can the C-preprocessor do this?

Maybe I did it once and forgot how.

Sincerely, Chris.

zennehoy
July 25th, 2007, 02:57 AM
It's possible, check out Boost.Preprocessor... how they do it is something I don't plan on understanding though :) (I think it relies on repeated inclusion of the same file)
Zen