How do C/C++ compilers work?

25,331

Solution 1

Start with the dragon book....(stress more on code optimization and code generation)

Go onto write a toy compiler for an educational programming language like Decaf or Cool.., you may use parser generators (lex and yacc) for your front end(to make life easier and focus on more imp stuff)....

Then read gcc internals book along with browsing gcc source code.

Solution 2

Compiler Text are good, but they are a bit heavy for teaching yourself. Jack Crenshaw has a "Book" that was a series of articles you can download and read call "Lets Build a Compiler." It follows a "Learn By Doing" methodology that is great if you didn't get anything out of taking formal classes on the subject, or it's been WAY too many years since took it (that's my case). It holds your hand and leads you through writting a compiler instead of smacking you around with Lambda Calculus and deep theoretical issues that only academia cares about. It was a good way to stir up those brain cells that only had a fuzzy memory of writting something on the Vax (YEAH, that right a VAX!) many many moons ago at school. It's written very conversationally and easy to just sit down and read, unlike most text books which require several pots of coffee just to get past the first chapter. Once you have a basis for understanding then more traditional text such as the Dragon book are great references to expand on your understanding. (And personal I like the Dead Tree versions, I printed out Jack's, it's much easier to read in a comfortable position than on a laptop. And the Ebook readers are too expensive for something that doesn't actually feel like you're reading a real book yet.)

What some might call a "downside" is that it's written in Pascal, but I thought that just made me think about it more than if someone had given me a working C program to start with. Appart from that it was written with the 68000 in mind, which is only being used in embedded systems at this point time. Again for me this wasn't a problem, I knew 68000 asm and 68000 asm is easier to read than some other asm.

Solution 3

Solution 4

If you want dead-tree edition, try The Art of Compiler Design: Theory and Practice.

Solution 5

As noted by Pete Eddy, Jack Crenshaw's tutorial is excellent for newbies. But if you want to see how to a real, production C compiler works—one which was designed by brilliant engineers instead of created by throwing code at the wall until something stuck—get yourself a copy of Fraser and Hanson's A Retargetable C Compiler: Design and Implementation, which contains the source code to the very clean lcc compiler. Explanations of the design and implementation are mixed in with the code. It is not a first book for a beginner, but it will repay careful study, and you can get a used copy for $35.

For a longer blurb about lcc, see Compile C Faster on Linux.

The lcc web page also has links to a number of good textbooks. I don't know of an intro text that I really like, however.

P.S. Sorry you got ripped off at Uni.

Share:
25,331
Женя Борісевіч
Author by

Женя Борісевіч

Uncertainty is underrated.

Updated on January 25, 2020

Comments

  • Женя Борісевіч
    Женя Борісевіч over 4 years

    After over a decade of C/C++ coding, I've noticed the following pattern - very good programmers tend to have detailed knowledge of the innards of the compiler.

    I'm a reasonably good programmer, and I have an ad-hoc collection of compiler "superstitions", so I'd like to reboot my knowledge and start from the basics.

    Can anyone recommend links to online resources or favorite books? I'm particularly interested in C/C++ compiling, optimization, GCC and LLVM.

  • Женя Борісевіч
    Женя Борісевіч almost 15 years
    Thanks, nice sequence. I take the dragon book is : en.wikipedia.org/wiki/index.html?curid=188976
  • RBerteig
    RBerteig almost 15 years
    Yes, that is the dragon book. I read the 1st edition. It had a much simpler dragon....
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten almost 15 years
    Gah. People keep recommending this. Not me. Start with a casual introduction---say "Let's build a compiler"---then look at a Computer Sciencey reference with all the math and theory.
  • Dietrich Epp
    Dietrich Epp almost 15 years
    I'd recommend against trying to understand GCC. It's fairly unusual as far as compilers go, and its architecture is poor by design (as in, the design is crippled on purpose. Yes, I'm serious. No, I'm not just making a joke at GCC's expense).
  • Женя Борісевіч
    Женя Борісевіч almost 15 years
    Thanks for the tip - I will check lcc out
  • NoMoreZealots
    NoMoreZealots almost 15 years
    Brillant Engineers? Jack Crenshaw designed parts of the space shuttle, and home made computers were a HOBBY of his. Not to dispute the intellect of folks who wrote lcc, but you don't have to be brilliant to design a compiler. It's really not that hard.
  • Norman Ramsey
    Norman Ramsey almost 15 years
    The reference was not to Crenshaw but to gcc. RMS is many things, but brilliant engineer is not one of them. Then add 1000 monkeys and stir well...
  • NoMoreZealots
    NoMoreZealots almost 15 years
    I was thumbing through the GCC internals manual, it doesn't seem useful for "Learning" how a compiler works. It's not a teaching document it assumes that you already have a knowlege of the subject.
  • sourabh jaiswal
    sourabh jaiswal almost 15 years
    If you are interested in compiler optimizations only then you can try SUIF