C compiler written in Python

T

Tim Freeman

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:
http://people.cs.uchicago.edu/~varmaa/mini_c/

And this is the annotated assembly output:
http://people.cs.uchicago.edu/~varmaa/mini_c/foo.s
 
T

Thomas Guettler

Am Wed, 02 Jun 2004 23:32:07 -0500 schrieb Tim Freeman:
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.

[cut]

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

Thomas
 
L

Leif K-Brooks

Thomas said:
Am Wed, 02 Jun 2004 23:32:07 -0500 schrieb Tim Freeman:



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

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

Günter Jantzen

From: "Thomas Guettler said:
Cool. Next goal could be to
compile python with it.

Thomas
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
 

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

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top