views:

879

answers:

13

I've heard of the idea of bootstrapping a language, that is, writing a compiler/interpreter for the language in itself. I was wondering how this could be accomplished and looked around a bit, and saw someone say that it could only be done by either

  • writing an initial compiler in a different language.
  • handcoding an initial compiler in Assembly, which seems like a special case of the first

To me, neither of these seem to actually be bootstrapping a language in the sense that they both require outside support. Is there a way to actually write a compiler in it's own language?

A: 

I'm not very experienced with such things, but I would assume that the initial compiler would have to be written in another language. I'm fairly certain that "bootstrapping", in reference to compilers, simply refers to writing a compiler for a language in the language it's meant to compile, not writing the first compiler for the language in the language it's meant to compile.

jeremiahd
+3  A: 

The way I've heard of is to write an extremely limited compiler in another language, then use that to compile a more complicated version, written in the new language. This second version can then be used to compile itself, and the next version. Each time it is compiled the last version is used.

This is the definition of bootstrapping:

the process of a simple system activating a more complicated system that serves the same purpose.

EDIT: The Wikipedia article on compiler bootstrapping covers the concept better than me.

Eric Haskins
+8  A: 

The explanation you've read is correct. There's a discussion of this in the Dragon Book:

  • write a compiler C1 for language X in language Y
  • use the compiler C1 to write compiler C2 for language X in language X
  • Now C2 is a fully self hosting environment.
Mark Harrison
A: 

Every example of bootstrapping a language I can think of (C, PyPy) was done after there was a working compiler. You have to start somewhere, and reimplementing a language in itself requires writing a compiler in another language first.

How else would it work? I don't think it's even conceptually possible to do otherwise.

Adam Lassek
The first Lisp compiler, at least, was bootstrapped using an existing Lisp *interpreter*. So not another language semantically, but another language implementation.
Ken
A: 

It's the Comp-Sci version of the chicken-and-egg paradox. I can't think of a way not to write the initial compiler in assembler or some other language. If it could have been done, I should Lisp could have done it.

Actually, I think Lisp almost qualifies. Check out the Wikipedia entry on it: http://en.wikipedia.org/wiki/Lisp_(programming_language)

According to the article, the Lisp eval function could be implemented on an IBM 704 in machine code, with a complete compiler (written in Lisp itself) coming into being in 1962 at MIT.

Wing
+4  A: 

A super interesting discussion of this in unix co-creator Ken Thompson's Turing Award lecture.

He starts off with

What I am about to describe is one of many "chicken and egg" problems that arise when compilers are written in their own language. In this ease, I will use a specific example from the C compiler.

and proceeds to show how he wrote a version of the unix C compiler that would always allow him to log in without a password, because the C compiler would recognize the login program and add in special code.

The second pattern is aimed at the C compiler. The replacement code is a Stage I self-reproducing program that inserts both Trojan horses into the compiler. This requires a learning phase as in the Stage II example. First we compile the modified source with the normal C compiler to produce a bugged binary. We install this binary as the official C. We can now remove the bugs from the source of the compiler and the new binary will reinsert the bugs whenever it is compiled. Of course, the login command will remain bugged with no trace in source anywhere.

http://cm.bell-labs.com/who/ken/trust.html

Mark Harrison
+30  A: 

Is there a way to actually write a compiler in it's own language?

You have to have some existing language to write your new compiler in. If you were writing a new, say, C++ compiler, you would just write it in C++ and compile it with an existing compiler first. On the other hand, if you were creating a compiler for a new language, let's call it Yazzleof, you would need to write the new compiler in another language first. Generally, this would be another programming language, but it doesn't have to be. It can be assembly, or if necessary, machine code.

If you were going to bootstrap a compiler for Yazzleof, you generally wouldn't write a compiler for the full language initially. Instead you would write a compiler for Yazzle-lite, the smallest possible subset of the Yazzleof (well, a pretty small subset at least). Then in Yazzle-lite, you would write a compiler for the full language. (Obviously this can occur iteratively instead of in one jump.) Because Yazzle-lite is a proper subset of Yazzleof, you now have a compiler which can compile itself.

There is a really good writeup about bootstrapping a compiler from the lowest possible level (which on a modern machine is basically a hex editor), titled Bootstrapping a simple compiler from nothing. It used to be hosted at http://www.rano.org/bcompiler.html, but that link is dead now. If you run across it, it's a great read. (And if anyone knows where I could get a working copy, I'd really appreciate a link.)

Derek Park
I found a copy of the bootstrapping article at http://homepage.ntlworld.com/edmund.grimley-evans/bcompiler.html
Bruce Alderman
Sufficiently simple languages can be bootstrapped by (1) specifying the language (2) designing and writing the compiler (possibly on paper) (3) "running" the compiler on its own code *by hand*. No pre-existing compiler needed. Why would you *want* to do this? I have absolutely no idea.
dmckee
+7  A: 

There is a really good writeup about bootstrapping a compiler from the lowest possible level (which on a modern machine is basically a hex editor), titled Bootstrapping a simple compiler from nothing. It used to be hosted at http://www.rano.org/bcompiler.html, but that link is dead now. If you run across it, it's a great read. (And if anyone knows where I could get a working copy, I'd really appreciate a link.)

Derek, I don't know if or where this is still available online but I've copied it up onto shorttext.com.

John Richardson
+1  A: 

This might be a little OT, but it's also possible that you are writing a compiler of existing language for a new CPU. In this case you are in a very comfortable situation as you can first compile for and existing architecture and then use your compiler to cross-compile itself for the new architecture.

Michał Piaskowski
+1  A: 

Another alternative is to create a bytecode machine for your language (or use an existing one if it's features aren't very unusual) and write a compiler to bytecode, either in the bytecode, or in your desired language using another intermediate - such as a parser toolkit which outputs the AST as XML, then compile the XML to bytecode using XSLT (or another pattern matching language and tree-based representation). It doesn't remove the dependency on another language, but could mean that more of the bootstrapping work ends up in the final system.

Pete Kirkham
A: 

@John

Derek, I don't know if or where this is still available online but I've copied it up onto shorttext.com.

Thanks! I really appreciate it. Bookmarked and saved this time. Thanks again.

Derek Park
+1  A: 

Thanks for the info, everyone. When explained with the idea of initially writing a limited compiler, then building up on top of that, then the idea of bootstrapping makes more sense.

I'm taking a Compilers class this semester, a decision largely influenced by Steve Yegge's post on how important a class in Compilers is, and I just bought a copy of the Dragon book from the Amazon link that got so downmodded on SO earlier.

Thanks again everyone!

pbh101
A: 

Some of you may want to check out this podcast which discusses gcc compiler internals, as well as the gcc bootstrapping process.

none