Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating IL for .Net Platform

I’m writing a small compiler in C# and planning to generate IL instructions for .Net platform using System.Reflection.Emit. My question is, it is advisable to use System.Reflection.Emit for generating IL for production compilers.

If it is not advisable to use System.Reflection.Emit for generating IL for production compilers, do we have alternative libraries/tools for that purpose?

like image 745
Upul Bandara Avatar asked Jun 01 '11 05:06

Upul Bandara


4 Answers

System.Reflection.Emit is fine for production compilers, though you may want to take a look at mono-cecil.

like image 128
Oded Avatar answered Oct 23 '22 16:10

Oded


Here is a blog article describing some problems with Reflection.Emit that probably aren't serious limitations for your project but you can be the judge:

  • The limitations of Reflection.Emit

If those issues don't bother you, then you can use this SO question for tips on the generation using Reflection.Emit and writing the assembly to disk:

  • Writing a Compiler for .net - IL or Bytecode?
like image 43
Rick Sladkey Avatar answered Oct 23 '22 16:10

Rick Sladkey


While System.Reflection.Emit let's you do code gen it's the oldest of the code gen APIs in the .NET framework and it requires understanding of IL. The expression trees introduced in .NET 3.5 and extended in .NET 4.0 (can't create new types) but they can be used to assemble complete method bodies.

like image 29
John Leidegren Avatar answered Oct 23 '22 16:10

John Leidegren


Reflection.Emit is generally fine, but IKVM.Reflection.Emit (available from Mono) may have more options if the assembly you are generating is not for the same platform (i.e. you want to build a silverlight dll from your compiler, which is written in "full" .NET).

like image 1
Marc Gravell Avatar answered Oct 23 '22 16:10

Marc Gravell