Source code generation using Python

ats asteinarson at gmail.com
Sat Dec 6 16:47:26 EST 2008


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;
  }

On processing, given constant USE_INLINE_ASM (or not) the right code
is generated to a target file, which goes into the build process.

I was looking for packages that can do this and came up with some
candidates:

 - "empy" - http://www.alcyone.com/pyos/empy/ - It looks like it could
do the job, but appears non-maintained since 2003.
- "Cheetah" - Looks like more of a tool to do fix replacements of code
snippets.

There is some logic going on in the "ARG", "ASM" and "RES" sections,
so I need to link code generation with true Python functions.

The situation is really quite similar to HTML/PHP except, here we
would have C++/Python.

Any suggestions?

Thanks,
//Arne S.



More information about the Python-list mailing list