C compiler written in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Freeman

    C compiler written in Python

    Doing an independent study with Dave Beazley this quarter, Atul Varma
    has written a C compiler on his own in Python. It generates annotated
    x86 assembly code from arbitrary C files.

    Quoting Atul's website:

    * The different stages of compilation are encapsulated in visitor
    classes, which (in my opinion) makes the code quite readable, and also
    made writing the compiler a lot easier. The yacc rules merely generate
    the abstract syntax tree and visitors do the rest.

    * The code generator is also a visitor, which makes the process very
    modular; for instance, although this compiler doesn't generate
    intermediate code (which is what most compilers that compile for
    different architectures use), one could simply write, say, a SPARC code
    generation visitor and run the AST through it to generate assembly for
    that architecture. This separation also means that the rest of the
    compiler is independent of machine architecture.

    * Writing the compiler in Python allowed me to focus entirely on the
    task at hand (compilation), without being distracted by issues of memory
    management and low-level data structure creation. Using such a
    high-level language also made reading and refactoring the code a lot
    easier.


    Have a look:


    And this is the annotated assembly output:


  • Thomas Guettler

    #2
    Re: C compiler written in Python

    Am Wed, 02 Jun 2004 23:32:07 -0500 schrieb Tim Freeman:
    [color=blue]
    > Doing an independent study with Dave Beazley this quarter, Atul Varma
    > has written a C compiler on his own in Python. It generates annotated
    > x86 assembly code from arbitrary C files.[/color]

    [cut]

    Cool. Next goal could be to
    compile python with it.

    Thomas


    Comment

    • Leif K-Brooks

      #3
      Re: C compiler written in Python

      Thomas Guettler wrote:[color=blue]
      > Am Wed, 02 Jun 2004 23:32:07 -0500 schrieb Tim Freeman:
      >
      >[color=green]
      >>Doing an independent study with Dave Beazley this quarter, Atul Varma
      >>has written a C compiler on his own in Python. It generates annotated
      >>x86 assembly code from arbitrary C files.[/color]
      >
      > Cool. Next goal could be to
      > compile python with it.[/color]

      Then run Mini-C with that so you can compile Wine and run Cygwin under
      it. :-)

      Comment

      • Günter Jantzen

        #4
        Re: C compiler written in Python

        From: "Thomas Guettler" <guettli@thom as-guettler.de> writes[color=blue]
        >
        > Cool. Next goal could be to
        > compile python with it.
        >
        > Thomas
        >
        >[/color]
        And the goal after could be to get rid of C :-)

        Yes I really think about this:
        Let us start from the bottom

        Let us start with Assembler. Maybe High Level Assembler with clever macros.
        Add higher datatypes if possible (I wish list and dict). Build an
        environment that allows you to generate and execute assembler at runtime

        This could be the basic building block for clever JIT virtual machines
        (something like parrot - but small, lightweight please - if possible)

        Higher level languages on top of this machine will go the Psyco/JIT way-
        taking the best of Compiler and Interpreter technology. This means: not all
        can be compiled at compile-time, so compile again at hot-spots in the
        beginning of loops when type-information is available, save the compiled
        stuff and use it as long the loop repeats ...


        regards
        Günter


        Comment

        Working...