Sunday, January 27, 2008

IL Generator

From past few months I am constantly trying to increase my knowledge about IL i.e. intermediate language generated when we compile our code thought any .net runtime compliant compiler. There are few classes in mscorlib.dll under System.Reflection.Emit namespace that are used by these compilers to generate IL suitable for our code files. I'll try to give you some brief introduction about IL working and i'll use .Net Reflector and ILDasm.exe for demonstration.

Just to get you started let me tell you soemthing about these tools:

  1. .NET Reflector - This is a tool which allows you to browse thought constituents of your assemblies. You can reverse engineer all the classes that are part of your assembly in any .net compliant language like C# or VB.NET
  2. ILDasm - Let's you dis assemble your IL code. You can check out complete IL code with all opcodes. Apart from this IlDasm also let's you check assemblies manifest for metadata

Let us start with some IL generation:

  • Class ILGenerator
    [assembly : mscorlib.dll] [namespace: System.Reflection.Emit]
    This class generates Microsoft IL instruction. It contains a large number of methods that are used to emit appropriate IL instructions.
    There are methods to emit calli instructions, methods for exception handling stuff etc. Large portion of this class s occupied by Emit method overloads. There more than 10 overloads available for emit method.

Emit method takes a OpCode structure object. There is another class in System.Reflection.Emit namespace known as OpCodes. This has all the opcodes that you find in you IL. Almost all of these OpCodes are declared as readonly variables and initialized with some specific values.
This is OpCode structure and as you can see it has variables mostly of some enum type. Like StackBehaviour is an enum. OpCodes class is given below. Just look at the constuctor used for initializing readonly fields. After combining all these things together an IL instruction is pushed to a MSIL stream of instruction.

There is a method InternalEmit which almost all Emit methods in ILGenerator class calls. Let us look at this method: if opcode.m_size == 1 This checks weather opcode size is 1 bytes or 2 bytes long. If opCodesize is 1 then opCode byte is added to MSIL Stream byte array and its length is incremented by 1. But if we have 2 byte opcode then both bytes are added to MSIL byte stream array and its length is increased by 2 i.e. one after each addition. At last InternalEmit calls UpdateStackSize method which I'll leave for readers exploration.

Knowing about IL and metadata has helped me a lot in solving issues related to performance. I recommend knowing few things about IL to all .net developers.


  1. OpCodes :
  2. ILGenerator :

EnJoy Coding
Currently listening to: Who I am- Eminem


Mike Booker said...

Do we have same implementation for VB.NET?

~bLaCkHaWk~ said...

Hey Sorry Mike, I didn't check your question.

Yes, both VB.NET and C# works internally the same way. Try checking Reflector tool for more help. I am more comfortable with C# so I used C# code.

Anonymous said...

I found this site using [url=][/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...

50mg lariam Discount vasotec Low price levaquin Free pills ampicillin Online persantine 10mg cardizem

Anonymous said...

[url=]Buy Generic Cialis[/url]

Anonymous said...

Helo ! Forex - Работа на дому на компьютере чашка кофе получать удовольствие от работы стала независимой, просто зарегистрируйтесь forex [url=]forex[/url]