Source code generation using Python

Discussion in 'Python' started by ats, Dec 6, 2008.

  1. ats

    ats Guest

    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.
     
    ats, Dec 6, 2008
    #1
    1. Advertising

  2. 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
     
    Philip Semanchuk, Dec 6, 2008
    #2
    1. Advertising

  3. ats

    ats Guest

    On Dec 6, 11:19 pm, Philip Semanchuk <> 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.
     
    ats, Dec 6, 2008
    #3
  4. ats

    Alia Khouri Guest

    Alia Khouri, Dec 7, 2008
    #4
  5. ats

    Roger Binns Guest

    -----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)

    iEYEARECAAYFAkk8MkUACgkQmOOfHg372QRhsgCcCUzWHAHmjC1490yYba7c9Xrt
    DxMAnj/Ur2GoJkQgMrx65hYEqPwKLdVV
    =CvGB
    -----END PGP SIGNATURE-----
     
    Roger Binns, Dec 7, 2008
    #5
  6. ats

    Jorgen Grahn Guest

    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!
     
    Jorgen Grahn, Dec 8, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David M. Siegel
    Replies:
    0
    Views:
    357
    David M. Siegel
    Oct 20, 2003
  2. logistix at cathoderaymission.net

    Source Code Generation Tool

    logistix at cathoderaymission.net, Jun 10, 2004, in forum: Python
    Replies:
    1
    Views:
    375
    Miklós
    Jun 10, 2004
  3. Stefan Behnel
    Replies:
    3
    Views:
    488
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    399
    John W. Long
    Nov 24, 2003
  5. Laszlo Nagy
    Replies:
    1
    Views:
    101
    Ned Batchelder
    Nov 18, 2013
Loading...

Share This Page