Common Intermediate Language

Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL) ., is the binary instruction set defined within the  (CLI) specification. CIL instructions are executed by a -compatible runtime environment such as the. Languages which target the CLI compile to CIL. CIL is,. Runtimes typically compile CIL instructions into.

CIL was originally known as Microsoft Intermediate Language (MSIL) during the beta releases of the .NET languages. Due to standardization of and the, the bytecode is now officially known as CIL.

General information
During compilation of, the is translated into CIL code rather than into platform- or processor-specific. CIL is a - and platform-independent instruction set that can be executed in any environment supporting the Common Language Infrastructure, such as the on, or the   runtime. In theory, this eliminates the need to distribute different executable files for different platforms and CPU types. CIL code is verified for safety during runtime, providing better security and reliability than natively compiled executable files.

The execution process looks like this:


 * 1) Source code is converted to CIL and a  is created.
 * 2) Upon execution of a CIL assembly, its code is passed through the runtime's to generate native code. Ahead-of-time compilation may also be used, which eliminates this step, but at the cost of executable-file portability.
 * 3) The computer's processor executes the native code.

Instructions
CIL bytecode has s for the following groups of tasks:
 * Load and store
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code
 * Data and function pointers manipulation needed for C++/CLI and unsafe C# code

Computational model
The Common Intermediate Language is object-oriented and. That means that data is pushed on a stack instead of pulled from registers as in most s.

In it might look like this:

The corresponding code in can be rendered as this, where 0 is eax and 1 is edx:

Here are two locals that are pushed on the stack. When the add-instruction is called the operands get popped and the result is pushed. The remaining value is then popped and stored in the first local.

Object-oriented concepts
This extends to object-oriented concepts as well. You may create objects, call methods and use other types of members such as fields.

CIL is designed to be object-oriented and every needs (with some exceptions) to reside in a class. So does this static method:

This method does not require any instance of Foo to be declared because it is static. That means it belongs to the class and it may then be used like this in C#:

In CIL:

Instance classes
An instance class contains at least one constructor and some instance members. This class has a set of methods representing actions of a Car-object.

Creating objects
In C# class instances are created like this:

And these statements are roughly the same as these instructions:

Invoking instance methods
Instance methods are invoked like the one that follows:

In CIL:

Metadata
CLI records information about compiled classes as. Like the type library in the, this enables applications to support and discover the interfaces, classes, types, methods, and fields in the assembly. The process of reading such metadata is called .

Metadata can be data in the form of  attributes. Attributes can be custom made by extending from the  class. This is a very powerful feature. It allows the creator of the class the ability to adorn it with extra information that consumers of the class can use in various meaningful ways depending on the application domain.

Example
Below is a basic program written in CIL. It will display the string "Hello, world!".

The following code is more complex in number of opcodes.

This code can also be compared with the corresponding code in the article about .

In CIL syntax it looks like this:

This is just a representation of how CIL looks like near VM-level. When compiled the methods are stored in tables and the instructions are stored as bytes inside the assembly, which is a (PE).

Generation
A CIL assembly and instructions are generated by either a compiler or a utility called the IL Assembler that is shipped with the execution environment.

Assembled CIL can also be disassembled into code again using the IL Disassembler (ILDASM). There are other tools such as that can decompile CIL into a high-level language (e. g. C# or ). This makes CIL a very easy target for reverse engineering. This trait is shared with. However, there are tools that can the code, and do it so that the code cannot be easily readable but still be runnable.

Just-in-time compilation
(JIT) involves turning the byte-code into code immediately executable by the CPU. The conversion is performed gradually during the program's execution. JIT compilation provides environment-specific optimization, runtime type safety, and assembly verification. To accomplish this, the JIT compiler examines the assembly metadata for any illegal accesses and handles violations appropriately.

Ahead-of-time compilation
-compatible execution environments also come with the option to do an (AOT) of an assembly to make it execute faster by removing the JIT process at runtime.

In the there is a special tool called the  (NGEN) that performs the AOT. In there is also an option to do an AOT.

Pointer instructions - C++/CLI
A notable difference from Java's bytecode is that CIL comes with ldind, stind, ldloca, and many call instructions which are enough for data/function pointers manipulation needed to compile C/C++ code into CIL.

The corresponding code in CIL can be rendered as this: