views:

361

answers:

2

I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies.

The problem is, I can't figure out how to actually build these assemblies. The MethodBuilder class only accepts raw MSIL method bodies, so I have to get the raw MSIL of my expression tree. Calling Expression.Compile() returns a working delegate but I'm not able to get its underlying MSIL. Calling MethodInfo.GetMethodBody() throws an InvalidOperationException since it's not implemented in that specific child class.

How can I link that delegate into a new assembly?

+1  A: 

In order to emit raw IL you need to define your own AST. You need to Get AssemblyBuilder then ModuleBuilder and then you can define module-level method or get new TypeBuilder and now MethodBuilder to define class-level method.

You said that you already have lexer and parser. that means you able to build AST. So just walk through the parsed expressions and emit your IL.

Even if you get generated(by Compile) code you'll not be able to do something useful with it since generated code depends on infrastructure. For example if you need to compile closures then you should create class or other store fo lexical variables and so on(like non lexical control transfer which require to use Exceptions in .net)

Trickster
Yeah that's what I'm trying to avoid. The LINQ Expression Tree model does all that for me so I'd like to use that instead of writing my own MSIL emitter.
DrJokepu
ok, you trying to learn compiler theory but parser and lexer is the least parts of compiler. All fun we have inside AST and optimisation sections and in emitter of couse - just things you try to avoid
Trickster
There is a really good book out there called 'Expert .NET 2.0 IL Assembler' by Serge Lidin, that will give you an understanding about the structure of assemblies in MSIL. It's very easy to read as long as you understand basic assembler concepts. I would also suggest using Mono.Cecil as the library to Emit your IL. I think you'll find it much easier to work with then than the library under Emit namespace. Otherwise, i agree with the above poster. If you have the AST built, you should be walking through each statement and emit your IL.
Sergey
Thanks guys but I'm not sure if you understand my quesiton. I already have the abstract syntax tree. I know how to write the IL emitter, I know MSIL, but I don't want to, given that there's an API for that already.
DrJokepu
You "...in order to get a better understanding of how compilers work". Well those Expressions(they come from Linq and DLR) are not intended for compiler writer. And you have to write compiler by yourself. They intended for dynamic code generation so actually you have dynamic compiler not just interpreter.
Trickster
+1  A: 

Just found it. The DLR version of LambdaExpression exposes a CompileToMethod method which does exactly what I need.

lambdaExpression.CompileToMethod(myMethodBuilder);
DrJokepu
Be aware that this method has some limitations, such as the inability to compile non-static methods.
280Z28
@280Z28: Fortunately, My Toy Language is not object-oriented so that won't be a problem.
DrJokepu
Can you explain how to get the appropriate `MethodBuilder` - I realise this was a long time ago :)
Callum Rogers