CIL by Microsoft, flow chart by Scriptol

CIL - Common Intermediate Language

CIL (Common Intermediate Language) is a bytecode and the language of the .NET platform into which are compiled source code written in high level languages. Its operations are based on a stack and it is executed by a virtual machine.

It is also called IL or MSIL (MicroSoft Intermediate Language) which was its original name before the standardization of the CLI (Common Language Infrastructure) which he belongs and with which it should not be confused.

The source code from high-level language (C#, Basic or other language) is compiled in CIL and stored into an assembly.
The assembly is stored in a file in the PE (Portable Executable) format, which is also that of .dll and .exe, and includes a manifest file containing the medadata of the assembly, which is the interface of the code with other components of the software that uses it.

This code is compiled into bytecode to be interpreted by a JIT, or binary to be executed directly by the processor.

The CIL language can execute arithmetics operations, logics and has control structures (loop, if, etc.), calls functions and methods, manages a stack, loads and stores data, converts types (number into string...), has exceptions and supports concurrency.

Minimal Hello World! program in CIL assembly

.assembly Hello {}  
.method public static void Main() cil managed  
  .maxstack 1       
  ldstr "Hello, world!"       
  call void [mscorlib]System.Console::WriteLine(string)       

Sample of CIL bytecode

assembly              bytecode   purpose
ldarga argNum	 	FE 0A         fetch the adress given as argument
brtrue                   2D              branch when the previous result is true
break                    01              exit a loop

Reference: CIL is the partition 3 of the ECMA-335 standard that defines the .NET architecture.


See also LLVM, a even more portable alternative, which can produce JavaScript too from many languages, and the difference explained between bitcode and bytecode.