Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL), is the intermediate language binary instruction set defined within the Common Language Infrastructure (CLI) specification. CIL instructions are executed by a CLI-compatible runtime environment such as the Common Language Runtime. Languages which target the CLI compile to CIL. CIL is object-oriented, stack-based bytecode. Runtimes typically just-in-time compile CIL instructions into native code.
CIL was originally known as Microsoft Intermediate Language (MSIL) during the beta releases of the .NET languages. Due to standardization of C# and the CLI, the bytecode is now officially known as CIL. Windows Defender virus definitions continue to refer to binaries compiled with it as MSIL.
General information
During compilation of CLI programming languages, the source code is translated into CIL code rather than into platform- or processor-specific object code. CIL is a CPU- and platform-independent instruction set that can be executed in any environment supporting the Common Language Infrastructure, such as the .NET runtime on Windows, or the cross-platform Mono 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:
Source code is converted to CIL bytecode and a CLI assembly is created. Upon execution of a CIL assembly, its code is passed through the runtime's JIT compiler to generate native code. Ahead-of-time compilation may also be used, which eliminates this step, but at the cost of executable-file portability. The computer's processor executes the native code.
Instructions
CIL bytecode has instructions for the following groups of tasks: Load and store Arithmetic Type conversion Object creation and manipulation Operand stack management (push / pop) Control transfer (branching) Method invocation and return Throwing exceptions Monitor-based concurrency Data and function pointers manipulation needed for C++/CLI and unsafe C# code
Computational model
The Common Intermediate Language is object-oriented and stack-based, which means that instruction parameters and results are kept on a single stack instead of in several registers or other memory locations, as in most programming languages.
Code that adds two numbers in x86 assembly language, where eax and edx specify two different general-purpose registers: add eax, edx
Code in an intermediate language (IL), where 0 is eax and 1 is edx: ldloc.0 // push local variable 0 onto stack ldloc.1 // push local variable 1 onto stack add // pop and add the top two stack items then push the result onto the stack stloc.0 // pop and store the top stack item to local variable 0
In the latter example, the values of the two registers, eax and edx, are first pushed on the stack. When the add-instruction is called the operands are "popped", or retrieved, and the result is "pushed", or stored, on the stack. The resulting value is then popped from the stack and stored in eax.
Object-oriented concepts
CIL is designed to be object-oriented. You may create objects, call methods, and use other types of members, such as fields.
Every method needs (with some exceptions) to reside in a class. So does this static method: .class public Foo { .method public static int32 Add(int32, int32) cil managed { .maxstack 2 ldarg.0 // load the first argument; ldarg.1 // load the second argument; add // add them; ret // return the result; } }
The method Add does not require any instance of Foo to be declared because it is declared as static, and it may then be used like this in C#: int r = Foo.Add(2, 3); // 5
In CIL it would look like this: ldc.i4.2 ldc.i4.3 call int32 Foo::Add(int32, int32) stloc.0
Instance classes
An instance class contains at least one constructor and some instance members. The following class has a set of methods representing actions of a Car-object. .class public Car { .method public specialname rtspecialname instance void .ctor(int32, int32) cil managed { /* Constructor */ }