Assume one wants to create a simple .NET language, or slightly more difficult, a compiler for an existing .NET language. Do you absolutely need to be familiar with the CIL (Common Intermediate Language) to implement a compiler? And do you have to translate the syntax of your language to CIL by hand? Or is there another (more preferable) way? If not, what is the best way to learn CIL and especially to learn how to translate High-Level code to CIL?

3 Answers 3


Of course you can generate C# or any other higher level language code instead of targeting CIL directly.

It is common to implement compilers as eDSLs on top of meta-languages. You can take, say, a decent Scheme implementation for .NET, or a more heavyweight language, like Nemerle, and build up your compiler as a set of macros on top of a host language, with a syntax front-end at the very top of this hierarchy.

As you've mentioned "translating the syntax to CIL", you're likely having a somewhat distorted view on how compilers should work. Normally there are many more steps in between parser and code generation, and it does not make any sense to do everything in a single pass.

And, of course, if your language semantics is significantly different from the host language, for an efficient implementation you'd still be much better off with generating CIL directly (or via a sequence of intermediate languages).

Learning CIL is not a big deal at all: everything you have to know is available on MSDN.

And here is a trivial example of building a compiler on top of a meta-language - you can easily do it with any Lisp.

  • Please explain "Via a sequence of intermediate languages". Sep 27, 2011 at 12:55
  • 2
    @IntermediateHacker: In large modularized compilers, the parser produces a abstract syntax tree, that AST may be - after lots of analysis - lowered to a sequence of simple instructions in three address code, which may in converted into static single assignment form, which may then be lowered again into the output language (in this case, CIL). Many more options exist, and they can be combined in almost abritary ways (except that you'll usually want to go from high-level to low-level). In a sense, the ast and, before that, the tokens are also an intermediate representations.
    – user7043
    Sep 27, 2011 at 13:03
  • 1
    @IntermediateHacker, adding to what delnan said, it often worth trying to avoid complex transforms between intermediate representations. It is much easier to handle a sequence of trivial transforms, each dealing with only one aspect: e.g., replacing high level for, while and do loops with label, if and goto in one pass, then introducing explicit basic blocks in a next pass, then flattening expression trees into either stack pushes/pops or 3-address expressions, and so on.
    – SK-logic
    Sep 27, 2011 at 14:08

Do you have to know CIL? No, but it can help.

Do you have to translate your language to CIL? It's not at all necessary. (more in a moment)

What's the best way to learn CIL? Well, I've found that the best way to do it is to:
1. Read up on the CIL instruction set, the PE file format, and always have these documents nearby as a handy reference.
2. Write a small program in a .net language you're already familiar with, this small program should only try to do one thing that you don't know how to do with CIL. Compile it.
3. Use ILDASM to look at the program you've compiled as pure CIL. Now you understand how to do that with CIL!
4. Return to 2. until you grok the CIL.

Is there another way? Oh yes. First, look into The CodeDOM. This is a generic .net framework for 1) translating raw code into an object model, 2) translating an object model into code, or 3) Creating an executable. If you implement a CodeDOM parser to generate an object model, you could then use the generic MSIL CodeProvider to write the CIL for you! I haven't used this specific method, but I have created a compiler that generated CIL and used ILASM to create an executable. I've also done code compilation using CodeDOM. I would always choose to use as much CodeDOM as possible.


  • CodeDom does not cover all of the IL features. And why using ilasm, if there is System.Reflection.Emit available?
    – SK-logic
    Sep 27, 2011 at 16:51
  • @SK: CodeDom is definitely missing built-in support for some specific things, but it's extendable, so I was able to add them in myself. I've definitely used Emit, but I found that while I was learning CIL being able to look at the .IL file my tool had generated and see any ILASM output on the command line was useful. Obviously, either way works.
    – Task
    Sep 27, 2011 at 17:42
  • 2
    Microsoft is now going to make CodeDOM obsolete, since .NET 4. Using Expression tree is more preferred than CodeDOM, because it's closer to AST syntax than using CodeDOM. It's also powerful that since .NET 4 it supports statement as expression tree, not just simple math and lambda.
    – eriawan
    Oct 8, 2011 at 8:47

In this case, it depends on your language. I've seen several programming languages that target .NET or Java, and the developers did a "MyLanguage To Java translator" or "MyLanguage To C#/VB.NET translator", and later turn it into "MyLanguage To Java Bytecode" or "MyLanguage To MSIL/CIL Bytecode".

Its your language its similar to C# or VB.NET, perhaps it will be easier, in this case, to make a translator first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.