Common Intermediate Language

Common Intermediate Language

Common Intermediate Language (CIL, pronounced either "sil" or "kil") (formerly called Microsoft Intermediate Language or MSIL) is the lowest-level human-readable programming language defined by the Common Language Infrastructure (CLI) specification and is used by the .NET Framework and Mono. Languages which target a CLS-compatible runtime environment compile to CIL, which is assembled into an object code that has a bytecode-style format. CIL is an object-oriented assembly language, and is entirely stack-based. Its bytecode is translated into native code or executed by a virtual machine.

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

Contents

General information

During compilation of .NET programming languages, the source code is translated into CIL code rather than 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,[2] 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:

  1. Source code is converted to Common Intermediate Language, CIL's equivalent to Assembly language for a CPU.
  2. CIL is then assembled into a form of so called bytecode and a .NET assembly is created.
  3. Upon execution of a .NET 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.
  4. The native code is executed by the computer's processor.

Instructions

CIL bytecode has instructions for the following groups of tasks:

Computational model

The Common Intermediate Language is object-oriented and stack-based. That means that data is pushed on a stack instead of pulled from registers like in most CPU architectures.

In x86 it might look like this:

add eax, edx

The corresponding code in IL can be rendered as this:

ldloc.0
ldloc.1
add
stloc.0    // a = a + b or a += b;

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 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;
    }
}

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#:

int r = Foo.Add(2, 3);    // 5

In CIL:

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. This 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 */
    }
 
    .method public void Move(int32) cil managed
    {
        /* Omitting implementation */
    }
 
    .method public void TurnRight() cil managed
    {
        /* Omitting implementation */
    }
 
    .method public void TurnLeft() cil managed
    {
        /* Omitting implementation */
    }
 
    .method public void Brake() cil managed
    {
        /* Omitting implementation */
    }
}

Creating objects

In C# class instances are created like this:

Car myCar = new Car(1, 4); 
Car yourCar = new Car(1, 3);

And these statements are roughly the same as these instructions:

ldc.i4.1
ldc.i4.4
newobj instance void Car::.ctor(int, int)
stloc.0    // myCar = new Car(1, 4);
ldc.i4.1
ldc.i4.3
newobj instance void Car::.ctor(int, int)
stloc.1    // yourCar = new Car(1, 3);

Invoking instance methods

Instance methods are invoked like the one that follows:

myCar.Move(3);

In CIL:

ldloc.0    // Load the object "myCar" on the stack
ldc.i4.3
call instance void Car::Move(int32)

Metadata

.NET records information about compiled classes as Metadata. Like the type library in the Component Object Model, 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 reflection.

Metadata can be data in the form of attributes. Attributes can be custom made by extending from the Attribute class. This is a very powerful feature.

Example

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

.assembly Hello {}
.assembly extern mscorlib {}
.method static void Main()
{
    .entrypoint
    .maxstack 1
    ldstr "Hello, world!"
    call void [mscorlib]System.Console::WriteLine(string)
    call string[mscorlib]System.Console::ReadLine()
    pop
    ret
}

The following code is more complex in number of opcodes.

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

static void Main(string[] args)
{
outer:
    for (int i = 2; i < 1000; i++)
    {
        for (int j = 2; j < i; j++)
        {
             if (i % j == 0)
                 goto outer;
        }
        Console.WriteLine(i);
    }
}

In CIL syntax it looks like this:

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack  2
    .locals init (int32 V_0,
                  int32 V_1)
 
    IL_0000:  ldc.i4.2
              stloc.0
              br.s       IL_001f
    IL_0004:  ldc.i4.2
              stloc.1
              br.s       IL_0011
    IL_0008:  ldloc.0
              ldloc.1
              rem
              brfalse.s  IL_0000
              ldloc.1
              ldc.i4.1
              add
              stloc.1
    IL_0011:  ldloc.1
              ldloc.0
              blt.s      IL_0008
              ldloc.0
              call       void [mscorlib]System.Console::WriteLine(int32)
              ldloc.0
              ldc.i4.1
              add
              stloc.0
    IL_001f:  ldloc.0
              ldc.i4     0x3e8
              blt.s      IL_0004
              ret
}

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 Portable Executable-file (PE).

Generation

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

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

Execution

Just-in-time compilation

Just-in-time compilation 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

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

In the .NET Framework there is a special tool called the Native Image Generator (NGEN) that performs the AOT. In Mono there is also an option to do an AOT.

See also

  • List of CIL instructions

External links

References

  1. ^ "What is Intermediate Language(IL)/MSIL/CIL in .NET". http://www.interviewcity.com/2010/04/what-is-intermediate-languageilmsilcil.html. Retrieved 2011-02-17. "CIL: ... When we compile [a] .NET project, it [is] not directly converted to binary code but to the intermediate language. When a project is run, every language of .NET programming is converted is converted into binary code into CIL. Only some part of CIL that is required at run time is converted into binary code. DLL and EXE of .NET are also in CIL form." 
  2. ^ Benefits of CIL. http://books.google.at/books?id=VGT1_UJzjM0C&pg=PA15&lpg=PA15&dq=CIL+is+platform-independent#v=onepage&q=CIL%20is%20platform-independent&f=false. Retrieved 2011-02-17. "Furthermore, given that CIL is platform-agnostic, .NET itself is platform-agnostic..." 

Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Common Intermediate Language — (сокращённо CIL)  промежуточный язык, разработанный фирмой Microsoft для платформы .NET Framework. JIT компилятор CIL является частью так называемой CLR (англ. Common Language Runtime)  общей среды выполнения языков .NET. Ранее… …   Википедия

  • Common Intermediate Language — (CIL, pronunciado sil o kil ) (anteriormente llamado Microsoft Intermediate Language o MSIL) es el lenguaje de programación legible por humanos de más bajo nivel en el Common Language Infrastructure y en el .NET Framework. Los lenguajes del .NET… …   Wikipedia Español

  • Common Intermediate Language — (CIL) (teilweise auch nur Intermediate Language (IL)) ist eine Zwischensprache, in die alle Programme der Common Language Infrastructure übersetzt werden. CIL ist eine objektorientierte Assemblersprache und ist vollständig stackbasiert. Auf dem… …   Deutsch Wikipedia

  • Common Intermediate Language — Dans l environnement de programmation Microsoft, le Common Intermediate Language (CIL) est le langage de programmation de plus bas niveau qui peut être lu par un humain. Le code de plus haut niveau dans l environnement .NET Framework est compilé… …   Wikipédia en Français

  • Intermediate language — Common Intermediate Language (CIL) (teilweise auch nur Intermediate Language (IL)) ist eine Zwischensprache, in die alle Programme der Common Language Infrastructure übersetzt werden. CIL ist eine objektorientierte Assemblersprache und ist… …   Deutsch Wikipedia

  • Intermediate language — In computer science, an intermediate language is the language of an abstract machine designed to aid in the analysis of computer programs. The term comes from their use in compilers, where a compiler first translates the source code of a program… …   Wikipedia

  • Microsoft Intermediate Language — Common Intermediate Language (CIL) (teilweise auch nur Intermediate Language (IL)) ist eine Zwischensprache, in die alle Programme der Common Language Infrastructure übersetzt werden. CIL ist eine objektorientierte Assemblersprache und ist… …   Deutsch Wikipedia

  • Microsoft Intermediate Language — Microsoft Intermediate Language, сокращенно MSIL промежуточный язык, разработанный фирмой Microsoft для платформы .NET Framework. JIT компилятор MSIL является частью так называемой CLR (англ. Common Language Runtime) общей среды выполнения языков …   Википедия

  • C Intermediate Language — This article is about the simplified subset of C. For the Common Intermediate Language (also known as MSIL), see CIL. CIL (C Intermediate Language) is a simplified subset of the C programming language, as well as a set of tools for transforming C …   Wikipedia

  • Common Language Runtime — El Common Language Runtime o CLR ( entorno en tiempo de ejecución de lenguaje común ) es un entorno de ejecución para los códigos de los programas que corren sobre la plataforma Microsoft .NET. El CLR es el encargado de compilar una forma de… …   Wikipedia Español

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”