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
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.
- 8,517
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.
HTH!
- 437
- 3
- 7
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.
- 2,206