For many the compiler, bestowing animation upon lifeless source code, is a magic box. To me it's just another program. So, let's share the goodness with some writings on the subject. I'll add items to this as the fancy strikes me. While GCC is the compiler I know intimately, what's here is applicable to most compilers.
If you'd like to know about a bit of the compiler, email wiki.
Random discussion of compilers in general — they're not magic, but they have some rather magical properties.
- Compiler pipeline Compilers are a pipeline of transformations
- Exception handling Throwing an exception is complicated
- Compilers are magic Yeah, there is some magic ...
This is the bit of the compiler that slurps in your source code and turns it into some kind of internal representation. It's concerned with lexing and parsing, semantic analysis, template instantiation and giving you good diagnostics on why what you wrote is bad. The output of this is an Abstract Syntax Tree (AST)
- Abstract Syntax Tree The representation used by the Front End
- Parsing How AST is generated
- Vexing parse C++ has some complicated parsing requirements
This is the bit that's responsible for optimizing your code. Taking the FE stuff and making it go. Generally converting the AST to some lower-level internal representation (IR) in the process. There can be some fairly high level transforms here backed up by non-trivial maths. All of this is optional — your program will work without optimizing, just perform suckily.
- Intermediate Representation The representation used by the optimizers
- IR Cleanups Some core optimizations
- Code Reordering What sort of reordering might happen
- Loop Rotation A simple optimization applied to
And this is the bit that turns the IR into machine code the target CPU can actually execute. Some optimizations occur here too.
- Mary Shelley: Frankenstein
- It is trueish — there may be arbitrary hand-waving. Some errors may have crept in :(
- It's not a tree and it's not syntax. It's not particularly abstract either.