Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Common Intermediate Language
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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 language]]s. Code that adds two numbers in [[x86 assembly language]], where eax and edx specify two different [[X86#x86_registers|general-purpose registers]]: <syntaxhighlight lang="asm"> add eax, edx </syntaxhighlight> Code in an [[intermediate language]] (IL), where 0 is eax and 1 is edx: <syntaxhighlight lang="csharp"> 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 </syntaxhighlight> 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 (programming)|method]] needs (with some exceptions) to reside in a class. So does this static method: <syntaxhighlight lang="csharp"> .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; } } </syntaxhighlight> 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#: <syntaxhighlight lang="csharp"> int r = Foo.Add(2, 3); // 5 </syntaxhighlight> In CIL it would look like this: <syntaxhighlight lang="csharp"> ldc.i4.2 ldc.i4.3 call int32 Foo::Add(int32, int32) stloc.0 </syntaxhighlight> ====Instance classes==== An instance class contains at least one [[Constructor (object-oriented programming)|constructor]] and some [[Instance (computer science)|instance]] members. The following class has a set of methods representing actions of a Car-object. <syntaxhighlight lang="csharp"> .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 */ } } </syntaxhighlight> ====Creating objects==== In C# class instances are created like this: <syntaxhighlight lang="csharp"> Car myCar = new Car(1, 4); Car yourCar = new Car(1, 3); </syntaxhighlight> And those statements are roughly the same as these instructions in CIL: <syntaxhighlight lang="csharp"> 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); </syntaxhighlight> ====Invoking instance methods==== Instance methods are invoked in C# as the one that follows: <syntaxhighlight lang="csharp"> myCar.Move(3); </syntaxhighlight> As invoked in CIL: <syntaxhighlight lang="csharp"> ldloc.0 // Load the object "myCar" on the stack ldc.i4.3 call instance void Car::Move(int32) </syntaxhighlight> ===Metadata=== {{Main|Metadata (CLI)}} 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 (computer science)|reflection]]". Metadata can be data in the form of "attributes". Attributes can be customized by extending the <code>Attribute</code> 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)