C # Basics of IL

Source: Internet
Author: User

1. Instance parsing IL

As a C # programmer, IL's role is self-evident, first of all, a very simple program and its IL interpretation diagram, through the program's IL instructions to simply understand what the common IL directives mean.

  class  Span style= "color: #000000;" > program { static  void  Main (string  [] args) { int  I            = 2  ;  string  str=  " c#   ; Console.WriteLine (  hello   " +str); }    }
View Code

The next step is to define a concept. NET runtime any meaningful operation is done on the stack, rather than directly manipulating the registers. That's why. NET cross-platform, by designing different compilers to compile the same IL code to achieve cross-platform. For the stack our operation is nothing more than the stack and the stack, in Il the stack usually begins with the LD, and the stack starts with St. Know this and then look at the instructions above the feeling suddenly enlightened, then continue to learn the pace, the following table is for some common LD instructions. The ST instruction is to change the LD instruction into St, the function has the pressure stack to go out the stack, sometimes sees in the St or the LD adds. s This means that only one byte is taken. Then take a look at the process control, know that the pressure out stack and flow control, basically see the approximate meaning of il that will be bored kick. Process control is mainly loops and branches, and below I wrote a small program with loops and branches. Where we use addition and comparison operations, here we have to introduce the most basic three kinds of operations: arithmetic operations (add, Sub, mul multiplication, div, rem redundancy), comparison operation (CGT greater than, CLT less than, ceq equals); bitwise operations (not, and, or, XOR-XOR, Move left SHL, right shift shr). It is important to note that in the comparison operation, the result 1 or 0 is pushed directly after the execution of the instruction, and the process is done automatically. For the process control, mainly BR, Brture and brfalse These 3 instructions, wherein BR is directly to jump, Brture and brture is to judge and then jump.

Ldarg The parameters of the loaded member, such as the ldarg.0 above
Ldarga Load the address of the parameter, note that the general addition of a to take the address
Ldc Press the number constant to stack, as above the ldc.i4.2
ldstr Press the string reference to the stack
Ldloc/ldloca Ldloc a local variable to the stack, plus a to stack the address of the local variable
Ldelem Indicates that the array element is pressed against the stack
Ldlen Stack the array length
Ldind To stack addresses to access or manipulate data within an address.
 classProgram {Static voidMain (string[] args) {            intCount =2; stringStrname="C #"; if(StrName = ="C #")            {                 for(intI=0; i<count;i++) Console.WriteLine ("Hello C #"); }            ElseConsole.WriteLine ("ha ha"); }    }
View Code

2. Object-oriented IL

with the previous foundation, Basically look at the general Il code is not so square. If we declare a class in the program and create the object, we can see the keywords newobj, class, instance, and static in the IL. Look at the IL command will find outside is the class, the class inside the method, although the method is a directive but this and C # code structure is very similar. From the above phenomena, it is obvious that IL is not a simple instruction, it is object-oriented. When we use new to create an object in C #, it corresponds to newobj in IL, and the value type can also be created by new, but in Il it corresponds to initobj. Newobj is used to create an object that allocates the required memory for the object, initializes the object with an additional member synchronization index block and type object pointer, and then executes the constructor to initialize and return the object reference. Initobj is the initialization of the allocated memory on the stack, and the value type is set to 0 reference type to NULL. Another string is a reference type, which can be seen from the above example by using ldstr to load a string reference in the metadata into the stack instead of newobj. But if you create a string variable in your code that is not directly assigned but use the New keyword to get a string object, you will see the newobj directive in IL. When you create a one-dimensional 0 cardinality group, you also see the newarr directive, which creates an array and pushes the first address. However, if the array is not a one-dimensional 0 cardinality group, you will still see our familiar newobj.

Since it is object-oriented, the virtual method or abstract method in the inheritance must have the corresponding instruction in the IL to complete the invocation of the method. The calling method is primarily called call, callvirt, Calli,call, which is used primarily to invoke a static method, and callvirt is used to invoke a normal method and a method that requires runtime binding (that is, an instance method marked with instance), and Calli is called by a function pointer. There is a special case, however, that call calls virtual methods, such as virtual methods in sealed classes, because it is not likely to be rewritten, so using invoke can improve performance. Why does it improve performance? I don't know if you still remember when you create an object to invoke the method of this object, we often judge if the object is null, and if the object is null, it will be an error to call the method. This occurs because the callvirt will be type-detected when invoking the method, in addition to determining whether a subclass method overrides the situation to dynamically bind the method, and call is called directly. In addition, when calling the virtual method of the base class, such as calling object. The ToString method is to use the call method, if callvirt is used, because it is possible to see if there is an overriding method for the subclass (always viewing the child class that inherits the parent class), thereby reducing performance. However, in the final analysis call is used to invoke static methods, and the core idea of the dynamic method that callvirt calls to associate with the object is to be sure that the special case of call is because in this case there is no need for a dynamic binding method but can be used directly. Calli means to get a reference to a function, through this reference to call the function, but in my study did not use this, this specific how to get the citation I also do not know, interested parties please Baidu.

Role of 3.IL

We all know that C # code compiles will produce Narimoto data and IL, but our common exe such an assembly is how to generate, it and IL is what relationship? One thing you can be sure of is that the Assembly will definitely contain metadata and IL, because these 2 things are at the heart of the assembly. Here is a description of the assembly and the internal composition diagram, you can see that an assembly is composed of multiple managed modules, a module can be understood as a class or more than one class compiled after the generated assembly. The assembly manifest refers to information that describes the assembly, and the PE file header describes the file type, creation time, and so on of the PE file. The CLR header describes the CLR version, CPU information, and so on, which tells the system that this is a. NET assembly. Then the main thing is the metadata and IL in each managed module. Metadata is used to describe the data such as classes, methods, parameters, attributes, and so on. NET contains 44 metadata tables, mainly including definition tables, reference tables, pointer tables, and heaps. A definition table includes a class definition table, a method table, and so on, and a reference table describes a mapping record that refers to a type or method, and the pointer table holds a method pointer, a parameter pointer, and so on. You can see that the metadata table is equivalent to a database, and there is a relationship between the tables that resembles the primary foreign key.

By the preceding knowledge you can conclude that IL is a CPU-independent, object-oriented instruction set. NET platform compiles all the languages above it into a CLS (Common Language Specification) IL instruction set, which is then translated by different compilers into native code, such as our common JIT compiler, if you run C # on a Mac You can translate IL into machine code that MAC systems can execute by using a specific compiler on your Mac. That is, IL, like its name, executes a dynamic program as an intermediate language, for example, we invoke a method in a method table that points to an IL address that triggers the JIT compiler address and method, and the JIT compiler then compiles the cost code to the IL that the method points to. After generating the local code, this method will have a reference to the local code head address so that the next time the method is called, the local code will be executed directly.

C # Basics of IL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.