News for hackers. Stuff that matters.
Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by mahmud on Friday September 18, 2009 @10:29PM (#831568)
For the record, I think it's a terrible idea to keep prototyping compilers in C in this day and age. There is already a DSL for compiler construction and it's called Standard ML. SML/NJ, along with the New Jersey Machine Toolkit, Ramsey and Fernandez' excellent binary-frobbing-utility framework (yes! tools that generate profilers, debuggers, tracers, assemblers and disassemblers!) along with stuff from the SUIF project, you can start writing very sophisticated industrial compilers in fraction of the time it takes to debug just the front-end stuff in C. Even Perl, or your scripting language of choice, is better than C for compiler hacking.

http://suif.stanford.edu/papers/ http://www.cs.tufts.edu/~nr/toolkit/ (devour Norman Ramsey's site and read his joint papers with Mary Fernandez; him, along with Monica Lam at Stanford, the Rice people Linda Torczon, Keith Kooper et al. are producing some of the most accessible tools and papers and certainly most exciting. The Rice group is also responsible for the best introductory compiler hacking text in recent publication: Engineering a Compiler. GET IT! If even just for the carefully curated bibliography.

Also, recently, the ACM PLDI published a list of 20 most influential papers in programming languages design and implementation:

http://www.cs.utexas.edu/users/mckinley/20-years.html

I took the time to scrape as many of them as I could off of the internet, wherever they were freely available (i.e. author's websites) and I can say I have 18 of them. I would love to share them with hungry minds in one tarball, or they can google the papers individually:

A Data Locality Optimizing Algorithm.pdf

A Safe Approximate Algorithm for Interprocedural Pointer

Aliasing.pdf

An Evaluation of Staged Run-Time Optimizations in DyC.pdf

An Implementation of Lazy Code Motion for SUIF.pdf

Analysis of Pointers and Structures.pdf

Balanced Scheduling- Instruction Scheduling When Memory Latency is Uncertain.pdf

Complete Removal of Redundant Expressions.pdf

Global Register Allocation at Link Time.pdf

How To Read Floating Point Numbers Accurately.pdf

Improving Register Allocation for Subscripted Variables.pdf

Interprocedural Constant Propagation.pdf

Interprocedural Slicing Using Dependence Graphs.pdf

Lazy Code Motion.pdf

On-The-Fly Detection of Access Anomalies.pdf

Register Windows vs. Register Allocation.pdf

Soft Typing.pdf

Software Pipelining-- An Effective Scheduling Technique for VLIW Machines.pdf

The Design and Implementation of a Certifying Compiler.pdf

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by gizmo on Saturday September 19, 2009 @04:54AM (#831800)
I'm guessing many people are interested in the whole PDF package. Can you perhaps put it online somewhere?
Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by mahmud on Saturday September 19, 2009 @11:57AM (#832062)
So far no one has requested it, I am guessing it's because people have the list above and can google it themselves. I am gonna take a look at the papers and see if any of them have clauses about free redistribution, if not, you should have a tarball on the front page RSN.
Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by silentbicycle on Saturday September 19, 2009 @08:29PM (#832518)
If you do so, could you also post a link to it here? Thanks in advance.
Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by mahmud on Monday September 21, 2009 @03:58AM (#834180)
gizmo and silentbicyle, the papers are up:

http://news.ycombinator.com/item?id=834175

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by silentbicycle on Monday September 21, 2009 @07:48AM (#834398)
Awesome, thanks!
Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by drobilla on Saturday September 19, 2009 @10:13AM (#831978)
Maybe 'better' in some academic sense, but C is what all machines have. If you want to write a good native compiler userful to people without massive dependencies, you write it in C/C++.

Writing it in ML may be nice, but nobody wants to install ML just to get at another language. The kind of person that would do this is probably aspiring to a good language implementation of their own that could be considered a /peer/ of ML (or Perl, or Python, or Scheme, or Java, or....). High level languages written in yet another high level language are more of an academic curiosity than a tool anyone's likely to use.

While, yes, you're talking about prototyping, that assumes doing so in ML is much simpler. Maybe for you, but not for a C/C++ coder who doesn't know ML. There are a very, very large number of such coders.

Why write a prototype then have to rewrite the whole thing in C to have a "serious" language implementation? For small languages, the effort spent in prototyping then rewriting is going to be more effort than just writing a good stand-alone implementation from the outset. My hobby "prototype" compilers are small, portable, fast, depend on almost nothing, and integrate with the system and command line nicely. If the language was to become useful embedded in some program, say, I could do so immediately because I wrote the implementations to be good from the start. As a user, I'd take that over something I have to run in ML or some other massive high level language any day...

Would it be nice if the system's language was something better (somewhat) like ML? Sure. Is it? No.

Anyway, from the implementation point of view, when it comes time to write your GC, or any other low level details (parallelism?) writing in something like ML is a severe handicap. As far as performance goes, LLVM makes /fast/ code, and a lot of work is constantly being poured into it by a lot of people to make it even faster. This is a HUGE amount of work (i.e. far beyond what one person writing a language, or the people involved in the projects you have mentioned, can or will do), and it's already been done for you if you use LLVM (of course LLVM does have ML bindings).

Anyway, the point is, if you're trying to "prototype" or do PLT research, and you know ML well, and all you care about is the language itself you're implementing (and not the qualities/bloat/dependencies/performance of the implementation); sure, using ML probably makes sense. However these things are certainly not true of everyone writing a compiler.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by mahmud on Saturday September 19, 2009 @12:12PM (#832079)
I wrote a long reply then decided against it. I will ignore that "C is on all machines" fallacy, because it's not. People are accustomed to downloading viewers and codecs for even the most basic documents and multimedia files; witness the trouble people go through just to make an audio widget play on their MySpace page, or an MS Office version X template in version X++. Compiler users should be bloody well sophisticated and have no trouble installing the necessary runtimes; it couldn't be harder than installing Flash, Java or .NET, really.

Forget the word "compilers" for a second. Pick the most powerful language you can find for experimenting with graph algorithms.

That's it. That's all it boils down to. Compiler bloat, running speed, start up time and other stuff is just a programmer's wishful thinking. How many machines in the world run your software? how much of your user's time have you wasted by not implementing a piece of software as efficiently as you could? (would your users trade delivery time for startup or running time? i.e. do they want to start using your program today, even if it runs 300% slower than the machine allows, or do they want to use it next week and expect it to run at maximum speed? :-)

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by ekiru on Saturday September 19, 2009 @12:32PM (#832104)
I'm not entirely sure how machine code(or LLVM bytecode, if you'd prefer) produced by a program written in ML is at all inferior to the same machine code(actually, probably less optimized machine code, given that ML is a high level language in which it's easier to code optimizations than in C) produced by a program written in C.

Comparing ML to Perl or Python is ridiculous. There are a number of native optimizing compilers of ML. ML is closer to C++ or Java or CL performance-wise than to those languages.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by ilyak on Saturday September 19, 2009 @04:52AM (#831798)
Well, you can write your own guideline on how to prototype a language in SML, post it to NH, and profit.

Now we don't see your guideline but do see Flex/Bison/LLVM one.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by calambrac on Saturday September 19, 2009 @03:37PM (#832260)
WTF is wrong with you? That was one of the most informative comments I've ever seen on this site. There was a ton of useful information in there, way more than the short little guide in the OP (which I'm not knocking at all).
Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by ilyak on Saturday September 19, 2009 @03:44PM (#832263)
I'd like it to be a stand-alone article. This way it would be a first-class citizen on HN.

I mean, more meat, examples, and soon quite a few more people know how to prototype a language in SML.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by silentbicycle on Saturday September 19, 2009 @08:36PM (#832523)
Andrew Appel's _Modern Compiler Implementation in ML_ is probably what you want. (http://www.amazon.com/Modern-Compiler-Implementation-Andrew-...)

While it uses SML, it's not hard to follow if you use OCaml, and the bibliography / references are also quite helpful. The Ghuloum paper referenced elsewhere in the thread is also quite good (and uses Scheme, for better or worse).

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by ilyak on Sunday September 20, 2009 @04:16PM (#833485)
It's a damn book, not a quick article.

Sad.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by silentbicycle on Sunday September 20, 2009 @04:33PM (#833503)
Compilers are a big topic. Any "quick article" of substance will have references to underlying tools and concepts the author will expect you to know.

If you're completely lost, try starting with interpreters first. Compilers build on many of the same ideas, but interpreters are generally simpler. Read "The Art of the Interpreter" and/or SICP, and do the exercises. The former is a short paper, the latter is an excellent book expanding on it.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by RiderOfGiraffes on Monday September 21, 2009 @06:19AM (#834304)
I've written compilers, and even though I no longer have any reason to do so, I'd love to know more about writing better compilers in better languages.

There are dozens and dozens, if not hundreds of articles and "how to" style pages on the web showing a concrete implementation of a small compiler for a toy language using lex/flex and yacc/bison, yet when asked for something similar in ML, SML, Haskell, whatever, the response is to be pointed at books and scholarly articles.

Where are the popular accounts of better compiler building with ML? Are there any? If not, why not?

Yes, building an industrial-strength compiler for a real-world langauge is a big and complex undertaking, but I feel the field would advance more quickly if more hackers had more access to more articles of the "Build a compiler in an afternoon" variety.

And I'd appreciate learning from it too, despite, or perhaps because of, my existing knowledge.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by silentbicycle on Monday September 21, 2009 @08:06AM (#834416)
Part of it is that the culture surrounding ML is quite academic. Most of what I've found has been written for journals or books (the Appel compilers book above, _Purely Functional Data Structures_ by Okasaki, etc.), not blogs. Also, the ML community is rather small, though FWIW I'm only really familiar with the OCaml side of it.

Here's[1] a post about an OCaml / LLVM article by Jon Harrop, but it's just an intro for an article in his (pay) OCaml Journal.

   [1]: http://ocamlnews.blogspot.com/2008/09/writing-bytecode-compiler-using-llvm.html
Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by RiderOfGiraffes on Monday September 21, 2009 @03:22PM (#835302)
And there, to some extent, is the point. People will continue to write "popular press" articles about builing compilers using flex and bison, possibly targetting C as an intermediate language, until the ML community (or equivalent) becomes less clannish.

I'd love to see how clever they are, applaud them for it, and build on their work. They don't seem to care.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by silentbicycle on Monday September 21, 2009 @05:10PM (#835599)
Indeed. I think the ML languages have a lot of ideas that should see more widespread use, but the community (with exceptions, naturally) seems to be more motivated by reputation within academia than on blogs and other open discussions. That's their prerogative, I guess, but it does mean that little activity around ML is visible outside certain circles.

I'm curious about writing interpreters / compilers in Prolog (another language that seems particularly suited to prototyping them), as well, but I've already got plenty of projects.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by ilyak on Monday September 21, 2009 @05:31AM (#834268)
Please no books and no research papers.

Just a short article showing how to get things done.

Exactly as one we're discussing.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by jrockway on Saturday September 19, 2009 @05:16AM (#831811)
"Let's intentionally do things wrong because nobody posted details about the right way to HN."

Yeah, go for it...

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by ilyak on Saturday September 19, 2009 @06:24AM (#831843)
How would one do things right if there are no examples of comparable quality?

Revisit the recent drama with twisted vs tornado.

Re:Writing Your Own Toy Compiler Using Flex, Bison and LLVM by mahmud on Saturday September 19, 2009 @11:44AM (#832047)
How would one do things right if there are no examples of comparable quality?

By researching independently when given sufficient pointers. I posted a link to Abdul Azziz Ghoulum's paper which shows a full compiler in small baby steps, bootstrapped from the first line!

Research and conference papers are fairly good for learning, almost as good as blog posts.