views:

555

answers:

4

I'm interested in writing a compiler for a intermediate byte-code language like C# (a subset of). I'm trying to accumulate all the resources and information I can before the project begins.

I'll be writing the compiler in C# as well, so hopefully down the line my compiler will be able to dogfood itself.

The best resource I have found so far is, Blue - The C# Compiler Written in Pure C#, which is open-source. I've already flipped through its source code and it will be a great resource.

One thing about this project is my CLR knowledge isn't up to par yet and it is one of the things I'm looking to get a deeper insight into by writing this term project.

I'm not looking for general compiler-construction resources, I have plenty of those. I'm looking for resources that will aid me specifically in writing a C# compiler.

  1. What are some good resources for learning the nitty gritty details of .NET/CLR internals?
  2. What is the best resource on MSIL?
  3. Should I use the ANTLR Parser Generator since it lists C# as a target code-gen language or would you consider this "cheating" in terms of this project?
+2  A: 

I just answer 3: I think it's perfectly valid and not considered cheating to use a parser generator. Writing a parser (in code) is a highly error prone and mechanical task without much benefits (in terms of learning stuff).

Mehrdad Afshari
+2  A: 

This would seem to be what you need:

Compiling for the .NET Common Language Runtime (CLR)

Product Description

The definitive book for serious software engineers, compiler developers, and language implementers. Examples included are drawn from C#. Softcover.

From the Back Cover

  • Go "under the hood" with .NET's Common Language Runtime!
  • The definitive book for serious software engineers, compiler
    developers, and language implementers
  • Mapping any language's key features onto .NET's common object model
  • Comprehensive coverage of Microsoft Intermediate Language (MSIL)
  • Includes extensive examples drawn from C#

I haven't read it (only skimmed through it) but it looked pretty comprehensive.

And, no, I don't think that using the ANTLR Parser Generator is in any way "cheating." I have used Lex/yacc on many project and I have never felt like I was cheating or somehow taking away from the learning experience of writing compilers.

Enjoy,

Robert C. Cartaino

Robert Cartaino
I would just like to add some more information regarding the book. The "extensive examples" in C# are few and far between. Most of the code samples in the book are in a Pascal variant language, which is also the language the compiler is designed for. The book also is .NET 1 specific, in fact the book was written before .NET 1 shipped so there's no mention of IL generics, lambdas, yield return, or any of the new 4.0 features. I would recommend the book only as a guide to .NET 1 IL.
Wesley Wiser
+2  A: 

What are some good resources for learning the nitty gritty details of .NET/CLR internals?

I'd suggest Richter would be a good place to start.

And of course the C# Langauage Specification. Although this is online I think the v3.0 book also contains annotations by Anders and others, so it may be worth borrwoing the actual dead-tree version from your library.

Steve Haigh
+1  A: 
David Silva Smith