How to interpret IL code and IL code

Source: Internet
Author: User
Tags mscorlib

How to interpret IL code and IL code
How to interpret IL code

For the IL code, I will unveil it in three ways. What is the IL code? Why should we read the IL code? How can we read the IL code? The answer to these three questions will be my overall idea of interpreting the IL code.

What is the IL code? IL (Intermediate Language). It is also called the pencil or MSIL. translating it into Chinese is "Intermediate Language ". The JIT compiler of csung can compile the csung source program into a. EXE or. dll file. However, the compiled program code is not the binary code that the CPU can directly execute, but the legendary IL code. For this reason, the. exe or. dll files can be opened by the ILDASM installed by VS to view the IL code.

Why should we read the IL code? No matter which technology we are proficient in, we should start from its essence, find the most core operating mechanism, and finally understand and master it effectively. For the C # language, mastering the IL language is like grasping the essence of the C # language. It can effectively help us understand the operating mechanism of the compiler. For some C # experts, after in-depth research on the IL language, the IL language can be directly modified and compiled. However, at my current level, understanding the IL code is to better understand the various features of C #, such as some of the most important basic features of C #, delegation, and events.

How can we read the IL code? Before reading the IL code, I must introduce the memory structure in the C # Code compiling environment. As far as I know, it is divided into five major memory areas. These are managed stacks, thread stacks, computing stacks, call stacks, and code zones.

Managed Heap: stores referenced data. GC (Garbage Collection) manages referenced data.

Thread Heap Stack: stores data of the value type and the address of the reference type. The operating system directly manages the data of the value type.

Evaluation Stack: A Stack that temporarily stores data of the value type and the address of the reference type. It complies with the Basic stack rules of the advanced post-output (FILO.

Call Stack: The Record Frame is used for storage. the parameter value of the locals init (int32 V_0) command is a local variable table that does not comply with the basic post-output (FILO) stack rules.

Code: stores various program instruction sets.

There are four data memory areas, which can be divided into three levels by level. The hosting area is the highest level. Only the reference type data is stored. The thread stack is the second level, and the value type data and reference type address are stored. The computing stack and call Stack are the third level. When a method is called in a thread, it mainly operates data in these two stacks. To understand the IL code, the first step is to clearly understand the relationship between the computing stack and the call stack operation data. Next I will introduce an IL code instance to illustrate how several memory areas operate data on each other.

C # source code

Using System;

Namespace ILDemo
{
Class Program
{
Static void Main (string [] args)
{
Int I = 1;
Int j = 2;
Int k = 3;
Int answer = I + j + k;
Console. WriteLine ("I + j + k =" + answer );
Console. ReadKey ();
}
}
}

IL code

. Method private hidebysig static void Main (string [] args) cel managed
{
. Entrypoint // program entry
// Code size 42 (0x2a)
. Maxstack 2 // calculate the number of values that can be stored in the computing Stack

. Locals init ([0] int32 I,
[1] int32 j,
[2] int32 k,
[3] int32 answer) // defines the int32 type of I, j, k, and answer into the call stack

IL_0000: nop // No operation

IL_0001: ldc. i4.1 // place the I value from the thread stack to the computing stack.
IL_0002: stloc.0 // place the value (I value) at the top of the computing stack to 0 of the Call Stack index.
IL_0003: ldc. i4.2 // place the j value from the thread stack to the computing stack.
IL_0004: stloc.1 // place the value (j value) at the top of the computing stack to call stack Index 1.
IL_0005: ldc. i4.3 // place the value of k from the thread stack to the computing stack.
IL_0006: stloc.2 // place the value (k value) at the top of the computing stack to call stack index 2

IL_0007: ldloc.0 // copy the value at zero of the Call Stack index to the computing Stack
IL_0008: ldloc.1 // copy the value at 1 of the Call Stack index to the computing Stack
IL_0009: add // add
IL_000a: ldloc.2 // copy the value at 2 of the Call Stack index to the computing Stack
IL_000b: add // add
IL_000c: stloc.3 // place the value at the top of the computing stack (the value of add) at the call stack index.
IL_000d: ldstr "I + j + k =" // push new object references to strings stored in metadata.
IL_0012: ldloc.3 // copy the value at 3 of the Call Stack index to the computing Stack

IL_0013: box [mscorlib] System. Int32 // boxed
IL_0018: call string [mscorlib] System. String: Concat (object, object) // call internal methods
IL_001d: call void [mscorlib] System. Console: WriteLine (string) // call WriteLine
IL_0022: nop // No operation
IL_0023: call valuetype [mscorlib] System. ConsoleKeyInfo [mscorlib] System. Console: ReadKey () // call lelekey
IL_0028: pop // No operation
IL_0029: ret // return
} // End of method Program: Main

Conclusion: 1. ldloc.0 prefix ld, load indicates loading, that is, pushing into the computing stack; loc, locals indicates the call stack, that is, the value is set to The 0th-bit parameter of the call stack.

2. Other ld prefixes are valid for the thread stack. For example, ldc. i4.0, ldstr, ld1_, ld1.

3. The st prefix, store meaning storage, that is, the computing stack is popped up. The pop-up values are stored in the call stack.

Based on the above three conclusions, you can basically understand the data interaction between the calculation stack and the call stack in most of the Methods of IL code. In the subsequent blog, I will describe how to use the IL code to gain a deeper understanding of delegation and events. I hope you can support it.

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.