Source code generation using Python

Jorgen Grahn grahn+nntp at
Mon Dec 8 22:32:31 CET 2008

On Sat, 6 Dec 2008 13:47:26 -0800 (PST), ats <asteinarson at> wrote:
> Hello,
> This is my first posting to a Python group (and I'm starting with
> Python seriously only now) , so bear with me if I make some mistakes.
> I want to generate 3 different versions of a C++ source code,
> basically injecting different flavours of inline assembler depending
> on target compiler/CPU.
> Code generation should be integrated into a 'master source file' which
> is the processed and generates the right code for GCC / MSVC or other
> cases. Something like:
>   int FastAdd( int t1, int t2 ){
>     int r;
>     ##if USE_INLINE_ASM
>       #ARG( eax, "t1")
>       #ARG( ebx, "t2")
>       #ASM( "add", ebx, eax )
>       #RES( eax, "r" )
>     ##else
>       r = t1+t2;
>     ##endif
>     return r;
>   }

You didn't say explicitly, so I have to ask: is there a reason you
cannot use the C++ preprocessor? It does exactly what you describe,
and would be the least surprising solution to the readers.


  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/>          R'lyeh wgah'nagl fhtagn!

More information about the Python-list mailing list