Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Learn IL on the CLR

Tags:

.net

clr

il

Since these IL codes what I see more, I like to learn how to interpret them correctly.

I couldn't find a documentation like C# Compiler or any other so I think I can pretty much take care of the rest after I learn this common ones:

Below are some sample IL codes containing what I need to know :

Sample 1:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] class EnumReflection.DerivedClass derivedClass)
  IL_0000:  nop
  IL_0001:  newobj     instance void EnumReflection.DerivedClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance void EnumReflection.DerivedClass::WriteOutput()
  IL_000d:  nop
  IL_000e:  ret
} // end of method Program::Main

Sample 2:

.method public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
{
  // Code size       38 (0x26)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldstr      "Hello"
  IL_0006:  stfld      string EnumReflection.DerivedClass::hello
  IL_000b:  ldarg.0
  IL_000c:  ldstr      "World"
  IL_0011:  stfld      string EnumReflection.DerivedClass::world
  IL_0016:  ldarg.0
  IL_0017:  ldc.i4.s   123
  IL_0019:  stfld      int32 EnumReflection.DerivedClass::age
  IL_001e:  ldarg.0
  IL_001f:  call       instance void EnumReflection.BaseClass::.ctor()
  IL_0024:  nop
  IL_0025:  ret
} // end of method DerivedClass::.ctor

I know what these codes do since I produced them :-) however I'd like to learn more about corresponding IL code.

These samples contain IL codes like, and could you please explain command with question marks? and also what do those command stand for? So we can memorize them easily.

  • nop (for debugging - no operation)
  • newobj (it seems it is creating new object in heap)
  • stloc.0 ?
  • ldloc.0 ?
  • ret ?
  • ldarg.0 ?
  • ldstr ?
  • stfld ?
  • ldc.i4.s ?
  • .ctor - constructor

Understanding IL is important as it exposes how particular compiler produces codes and act in specific cases.

However, I couldn't find a nice docs which contain examples as well about IL. CLR with C# 3.0 is a good book however eventually it isn't a IL book so it doesn't explain everything about IL.

EDIT:

I've found the specs and they tell these: Thanks to @usr.

  • nop (for debugging - no operation)
  • newobj - create a new object
  • stloc.0 - pop value from stack to local variable
  • ldloc.0 ? - load local variable onto the stack
  • ret - return from method
  • ldarg.0 - Load argument 0 onto the stack.
  • ldstr - load a literal string
  • stfld - store into a field of an object
  • ldc.i4.s - Push num onto the stack as int32 , short form.
  • .ctor - constructor
like image 765
Tarik Avatar asked Apr 21 '12 15:04

Tarik


People also ask

What is Il in CLR?

This article introduces the Cross-Language capabilities of CLR environment (Common Language Runtime Environment) and its constituent language IL (Intermediate Language).

What is IL code CLR CLS & JIT?

In this post we will discuss about CLR (Common Language Runtime), CLS (Common Language Specification) , CTS (Common Type System), JIT (Just in Time Compiler), IL (microsoft intermediate language), Managed Code and Unmanaged Code.

What is IL intermediate language code?

Intermediate language (IL) is an object-oriented programming language designed to be used by compilers for the . NET Framework before static or dynamic compilation to machine code. The IL is used by the . NET Framework to generate machine-independent code as the output of compilation of the source code written in any .


2 Answers

There are a few books that do cover IL:

  • Inside Microsoft .NET IL Assembler
  • Expert .NET 2.0 IL Assembler
  • Compiling for the .NET Common Language Runtime (CLR)
  • Feel free to edit this answer and add some books.

Also some books on reverse engineering have sections on IL.

See also:

  • IL / CLR / DLR References?
  • What is your recommendation for a good book on the .NET CLR and CIL?
like image 92
Richard Anthony Freeman-Hein Avatar answered Oct 17 '22 03:10

Richard Anthony Freeman-Hein


Microsoft standardized the CLR and published those standards. Partition III contains a wealth of information about IL/CIL and is suitable for learning. It is an excellent document.

You can also learn IL by example. Compile a few simple methods in C# and look at the IL in reflector (it has an IL-mode).

like image 10
usr Avatar answered Oct 17 '22 03:10

usr