Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Source code generation using Python

Reply
Thread Tools

Source code generation using Python

 
 
ats
Guest
Posts: n/a
 
      12-06-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.
 
Reply With Quote
 
 
 
 
Philip Semanchuk
Guest
Posts: n/a
 
      12-06-2008

On Dec 6, 2008, at 4:47 PM, ats 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;
> }
>
> 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


>
>
> The situation is really quite similar to HTML/PHP except, here we
> would have C++/Python.
>
> Any suggestions?
>
> Thanks,
> //Arne S.
> --
> http://mail.python.org/mailman/listinfo/python-list


 
Reply With Quote
 
 
 
 
ats
Guest
Posts: n/a
 
      12-06-2008
On Dec 6, 11:19*pm, Philip Semanchuk <phi...@semanchuk.com> wrote:
> On Dec 6, 2008, at 4:47 PM, ats 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;
> > *}

>
> > 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
>
>
>
> > The situation is really quite similar to HTML/PHP except, here we
> > would have C++/Python.

>
> > Any suggestions?

>
> > Thanks,
> > //Arne S.
> > --
> >http://mail.python.org/mailman/listinfo/python-list

>
>


Thanks, Mako looks neat.

Regards
// Arne S.
 
Reply With Quote
 
Alia Khouri
Guest
Posts: n/a
 
      12-07-2008
> Any suggestions?


I've happily used Cheetah with Leo (http://webpages.charter.net/
edreamleo/front.html) to organise and script my code generation needs,
but you may also be happy with cog (http://nedbatchelder.com/code/
cog/).

AK

 
Reply With Quote
 
Roger Binns
Guest
Posts: n/a
 
      12-07-2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ats wrote:
> 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)

iEYEARECAAYFAkk8MkUACgkQmOOfHg372QRhsgCcCUzWHAHmjC 1490yYba7c9Xrt
DxMAnj/Ur2GoJkQgMrx65hYEqPwKLdVV
=CvGB
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-08-2008
On Sat, 6 Dec 2008 13:47:26 -0800 (PST), ats <> 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

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template language with XPath support for source code generation? Stefan Behnel Python 3 01-13-2006 02:01 PM
Source Code Generation Tool logistix at cathoderaymission.net Python 1 06-10-2004 09:28 PM
HTML Generation (Next Generation CGI) John W. Long Ruby 4 11-24-2003 04:24 AM
Recommendation Wanted: Java source code generation David M. Siegel Java 0 10-20-2003 10:21 AM
run time code generation in python Carlo v. Dango Python 6 10-12-2003 12:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57