CIL or MSIL | Microsoft Intermediate Language or Common Intermediate Language

The Microsoft Intermediate Language (MSIL), also known as the Common Intermediate Language (CIL) is a set of instructions that are platform independent and are generated by the language-specific compiler from the source code. The MSIL is platform independent and consequently, it can be executed on any of the Common Language Infrastructure supported environments such as the Windows .NET runtime.
The MSIL is converted into a particular computer environment specific machine code by the JIT compiler. This is done before the MSIL can be executed. Also, the MSIL is converted into the machine code on a requirement basis i.e. the JIT compiler compiles the MSIL as required rather than the whole of it.

Execution process in Common Language Runtime (CLR): The execution process that includes the creation of the MSIL and the conversion of the MSIL into machine code by the JIT compiler is given as follows:

MSIL or CIL

  • The source code is converted into the MSIL by a language-specific compiler in the compile time of the CLR. Also, along with the MSIL, metadata is also produced in the compilation. The metadata contains information such as the definition and signature of the types in the code, runtime information, etc.
  • A Common Language Infrastructure (CLI) assembly is created by assembling the MSIL. This assembly is basically a compiled code library that is used for security, deployment, versioning, etc. and it is of two types i.e. process assembly (EXE) and library assembly (DLL).
  • The JIT compiler then converts the Microsoft Intermediate Language(MSIL) into the machine code that is specific to the computer environment that the JIT compiler runs on. The MSIL is converted into the machine code on a requirement basis i.e. the JIT compiler compiles the MSIL as required rather than the whole of it.
  • The machine code obtained using the JIT compiler is then executed by the processor of the computer.

Example: The MSIL is generated by the language specific compiler from the source code given below. To understand the MSIL in detail, simple C# source code with the class Demo that prints “GeeksforGeeks is given as follows:

filter_none

edit
close

play_arrow

link
brightness_4
code

using System;
  
public class Demo {
    public static void Main()
    {
        Console.WriteLine("GeeksforGeeks");
    }
}

chevron_right


The MSIL that is created by the C# compiler for the code provided above is given as follows:

// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit Demo
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      "GeeksforGeeks"
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method Demo::Main

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method Demo::.ctor

} // end of class Demo


// =============================================================

In the above MSIL, there are opcodes that are one or two bytes long. The base class declarations from which all other classes are inherited are contained in the mscorlib.dll. In the method Main(), the instruction ldstr loads the string “GeeksforGeeks” on the stack. Then the static System.Console.Writeline function is called and the string is popped from the stack. Finally, the ret instruction signals the end of the function call.
Then the .ctor() statement implies a default constructor without parameters for the class Demo. This constructor is automatically created by the compiler for the non-static class Demo. The call instruction passes the base object constructor and the ret instruction signals the end of the function call.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.