4

Possible Duplicate:
What are the stages of compilation of a C++ program?

I find that understanding how a given software language is compiled can be key to understanding best practices and getting the most out of that language. This seems to be doubly true with C++. Is there a good primer or document (for mortals) that describes C++ from the point of view of the compiler? (Obviously every compiler is a little different.)

I thought there may be something along those lines in the beginning of Stroustrup's book.

Community
  • 1
  • 1
Toaster
  • 1,911
  • 2
  • 23
  • 43
  • 2
    Not that I'm discouraging your curiosity about how compilers work, but I think you have the wrong idea about "best practices". Best practices is not about knowing the internals of a compiler. It's about programming for maintainability, reliability, and productivity. Best practices in C++ are applicable to all compilers, and often spills over to other programming languages. – Emile Cormier Jan 31 '12 at 12:46
  • @EmileCormier - I agree; however/for instance, I find that in some/many cases if one looks at a given best practice and asks 'why', one eventually reaches an aspect of the specification or compiler that dictates a certain behavior that one wants to either encourage or mitigate. – Toaster Jan 31 '12 at 13:06
  • @LuchianGrigore - thank you for the link. It certainly answers part of the question. I am looking for something a bit more descriptive. – Toaster Jan 31 '12 at 13:09
  • @Colin: I have every C++ "best practice" book written by Meyers and Sutter on my bookshelf, and not once did I see compiler implementation details as the rationale for a particular guideline. You say you found many cases where a best practice is explained by a compiler's implementation. Care to give an example? :) – Emile Cormier Jan 31 '12 at 13:15
  • @Colin: No wait, I might be wrong. :-) Passing large objects by reference would be an example of a best practice that is due to the internal behaviour of compilers. Another would be to avoid excessively deep and wide recursion that risks consuming all stack memory, where stack memory is a compiler implementation detail. – Emile Cormier Jan 31 '12 at 13:23

4 Answers4

3

Personally I like this one. Not a compiler's eye view exactly, but it tells you what's going on "under the hood" of a C++ program.

Inside the C++ Object Model

Paul Mitchell
  • 3,241
  • 1
  • 19
  • 22
1

It depends on what you want to obtain. I find that the Itaium ABI is a good document to understand some of the intricacies of the C++ object model. It will not deal with optimizations or the like, but I found it quite useful to understand how things like virtual inheritance can be implemented, or things that seemed much more simple as constructors and destructors (did you know that the compiler might generate up to 3 versions of each constructor that you provide? 2 destructors?)

Disclaimer: the document is quite dense, you will probably need to go over the sections more than once, at least I did. And you will need a good understanding of the semantics of the language to actually grasp why the solutions are so complex.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

I've head Compilers: Principles, Techniques, and Tools is solid.

http://www.amazon.com/Compilers-Principles-Techniques-Tools-2nd/dp/0321486811/ref=sr_1_1?s=books&ie=UTF8&qid=1328013630&sr=1-1

madmik3
  • 6,975
  • 3
  • 38
  • 60
0

I don't know of any such book, but if you wanted an understanding of how C++ is treated by the compiler the easiest way would be to write some code and get the compiler to spit out the annotated assembly listing and inspect that. This would give you an idea of how a particular compiler treats the code.

You could also get involved in a compiler project, perhaps something like the llvm's clang project?

Dominik Grabiec
  • 10,315
  • 5
  • 39
  • 45