Open In App

CIL or MSIL - Microsoft Intermediate Language or Common Intermediate Language

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MSIL (Microsoft Intermediate Language) also called CIL (Common Intermediate Language) is a set of instructions generated by a compiler from source code. MSIL is platform-independent means it can run on any environment that supports the .NET runtime.

Before MSIL code can be executed, it must be converted into machine code specific to the system it's running on. This is done by the JIT compiler (Just-In-Time compiler), which compiles the MSIL as needed during runtime.

Execution Process in Common Language Runtime (CLR)

The execution process includes the creation of the MSIL and the conversion of the MSIL into machine code by the JIT compiler. The process includes the following steps:

  1. MSIL Creation: The source code is compiled into MSIL by a language-specific compiler. Along with MSIL, metadata is generated, which contains information such as type definitions, signatures, and runtime information.
  2. Assembly Creation: The MSIL is packaged into a Common Language Infrastructure (CLI) assembly, which is a compiled code library used for security, deployment, and versioning. There are two types of assemblies: process assemblies (EXE) and library assemblies (DLL).
  3. JIT Compilation: The JIT compiler converts MSIL into machine code specific to the system. It compiles the MSIL on-demand, meaning only the necessary parts are compiled at runtime.
  4. Execution: The machine code generated by the JIT compiler is executed by the processor.
MSIL or CIL

Example: Simple C# Code and Its MSIL

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 below:

C#
using System;

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


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


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

Explanation:

  • 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.

Next Article
Article Tags :

Similar Reads