Source code generation using Python

A

ats

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.
 
P

Philip Semanchuk

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.

Hi Arne,
There are *lots* of packages for Python that replace chunks of
predefined templates. Most are HTML-focused, some more so than others.
I've used Mako (http://www.makotemplates.org/) to generate both HTML
and Apache config files. It could certainly do C++. Some alternatives
to Mako are mentioned in the documentation -- Kid, Genshi and Cheetah.

Rather than invite a flame war as to which is a better templating
engine, I'll just say that I'm happy with how Mako addresses *my*
needs. =) Good luck finding something that addresses yours.

Cheers
Philip
 
A

ats

Hi Arne,
There are *lots* of packages for Python that replace chunks of  
predefined templates. Most are HTML-focused, some more so than others.  
I've used Mako (http://www.makotemplates.org/) to generate both HTML  
and Apache config files. It could certainly do C++. Some alternatives  
to Mako are mentioned in the documentation -- Kid, Genshi and Cheetah.

Rather than invite a flame war as to which is a better templating  
engine, I'll just say that I'm happy with how Mako addresses *my*  
needs. =) Good luck finding something that addresses yours.

Cheers
Philip

Thanks, Mako looks neat.

Regards
// Arne S.
 
R

Roger Binns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I want to generate 3 different versions of a C++ source code,
basically injecting different flavours of inline assembler depending
on target compiler/CPU.

Are you aware that there are also packages that let you generate and
call C code from Python on the fly? I find it most productive to write
my code in all Python first and to also develop a comprehensive test
suite. Then profile and replace selected portions with lower level C
code with the tests being able to confirm your code is correct.

Here are some packages that take an alternate approach:

http://www.cs.tut.fi/~ask/cinpy/
http://code.google.com/p/shedskin/
http://pyinline.sourceforge.net/
http://scipy.org/Weave
http://mdevan.nfshost.com/llvm-py/

I like LLVM the most.

Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkk8MkUACgkQmOOfHg372QRhsgCcCUzWHAHmjC1490yYba7c9Xrt
DxMAnj/Ur2GoJkQgMrx65hYEqPwKLdVV
=CvGB
-----END PGP SIGNATURE-----
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top