views:

279

answers:

3

If I wanted to modify or add my own extensions to C, and add them to the GCC C compiler, what would I need to do? I do not want to propose changes to the language, I want to know how the C compiler actually works.

I've had a look at the source code to GCC and it looks as if Objective-C is implemented as a simple parser that generates code that makes references to a modified runtime library and then for everything that is C-based it uses the regular C compiler. I haven't studied it intensely so I'm probably not on the right track.

Say for example, I wanted to add my own keyword to Objective-C, just for fun. I want to add something that seems easy to implement, like maybe @assert. All I want is for @assert to call a function that I will add to the Objective-C runtime:

objc_runtime_assert (__FILE__, __LINE__, expression, "string version of expression");

Obviously I could just implement this as function call and macro anyway, but I want to gain an understanding of the parsing and compilation stages of C source code, and maybe so I could add my own extensions to a language for my own use.

The source code I have looked at consistently refers to "trees" and there are many, many macros that perform actions or obtain information from these trees.

Is there any documentation around that explains this sort of thing in-depth?

+2  A: 

The trees are most likely abstract syntax trees (AST) and any compiler book will teach that. The dragon book is a very classic one, at the class I took we used The essence of compilers by Robin Hunter, ISBN 0-13-727835-7. You should also be able to find tons of material from different colleges and universities with some good search terms (compiler design, AST, lexical analysis, top-down parsing, bottom-up parsing, semantic analysis, LL, LR).

hlovdal
+1  A: 

It depends on how you're doing it - but you might like to look for the GCC implementation of the D programming language - GCD. This is a work in progress, but is a full language implemented in the GCC framework without (as yet) much if any recognition from within the GCC team. The GCD experiences might be salutary.

Jonathan Leffler
+2  A: 

It depends on if you are already familiar with compilers in general or not. If not, start with the Dragon Book. If yes, then GCC already has some documentation for you:

  1. The GCC Internals Manual
  2. The GCC Wiki, in particular, the Getting Started page contains information about many things, GCC trees included.
Laurynas Biveinis