Common Intermediate Language (CIL) versus Microsoft Intermediate Language (MSIL)

A C# compiler converts C# source code into CIL, which is then translated into machine code by a hardware-specific JITer. On the other hand, a C++ compiler produces assembly code for a specific processor architecture using a machine language. This shows the difference in language/runtime independence between the two compilers.

The Microsoft Intermediate Language (MSIL), also referred to as the Common Intermediate Language (CIL), is a universal set of instructions generated by the
language-specific
compiler from source code. It is not specific to any platform and can be executed on various
common language infrastructure
environments, including the Windows .NET runtime. However, before the MSIL can be executed, it must be converted into a computer environment-specific
machine code
by the JIT compiler. This conversion is carried out on an as-needed basis, rather than compiling the entire MSIL at once.

The process of executing code within the Common Language Runtime (CLR) involves two main steps: first, the creation of MSIL code, and second, the conversion of that MSIL code into machine code using the JIT compiler.

MSIL or CIL

  • During CLR’s compile time, the language-specific compiler converts the source code to MSIL and generates metadata. The metadata includes details like runtime information, type definitions, and signatures of the code.
  • An assembly in the Common Language Infrastructure (CLI) is produced through the compilation of MSIL. It serves as a compiled code library utilized for security, deployment, and versioning. There are two types of assemblies, namely process assembly (EXE) and library assembly (DLL).
  • The Microsoft Intermediate Language (MSIL) is converted into machine code by the JIT compiler, which is specific to the computer environment it runs on. Rather than compiling the entire MSIL, the JIT compiler compiles it on an as-needed basis.
  • The processor of the computer executes the machine code produced by the JIT compiler.

The source code given below is compiled by the
language specific
compiler, resulting in the generation of MSIL. A simple C# source code with a class Demo that prints “GeeksforGeeks” is provided to gain a detailed understanding of MSIL.

The codes

using

and

System;

are listed, followed by


. Next,

public

,

class

, and

Demo {

are presented. After that,


,

public

,

static

,

void

, and

Main()

are shown. Then,


and

{

are listed. The codes


,

Console.WriteLine(

,

"GeeksforGeeks"

, and

);

are presented next. This is followed by


and

}

. Finally, the code

}

is listed.

The output provided by the C# compiler for the aforementioned code is the MSIL code presented below.

// =============== 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

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

The MSIL code contains opcodes of varying lengths, some being one or two bytes long. All classes inherit base class declarations from the mscorlib.dll. Within the Main() method, the ldstr instruction loads the string “GeeksforGeeks” onto the stack, followed by calling the static System.Console.Writeline function and popping the string from the stack. Finally, the function call is ended with the
ret instruction
signal. Additionally, the .ctor() statement denotes a default constructor with no parameters for the Demo class, which is automatically created by the compiler for non-static classes. The call instruction passes the base object constructor, and the ret instruction signals the end of the function call.

Frequently Asked Questions

Posted in Uncategorized