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. One 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 */ } .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 those statements are roughly the same as these instructions in CIL: ldc.i4.1 ldc.i4.4 newobj instance void Car::.ctor(int32, int32) stloc.0 // myCar = new Car(1, 4); ldc.i4.1 ldc.i4.3 newobj instance void Car::.ctor(int32, int32) stloc.1 // yourCar = new Car(1, 3);
Invoking instance methods Instance methods are invoked in C# as the one that follows: myCar.Move(3); As invoked in CIL: ldloc.0 // Load the object "myCar" on the stack ldc.i4.3 call instance void Car::Move(int32)
Metadata The
Common Language Infrastructure (CLI) 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 customized by extending the Attribute class. This is a 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==