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:
- 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:
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
.method public hidebysig static void Main() cil managed
IL_0001: ldstr "GeeksforGeeks"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
} // end of method Demo::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
IL_0001: call instance void [mscorlib]System.Object::.ctor()
} // 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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!