· Il Code Analysis Method
· Il Command Parsing
·. Net learning methodology
1. Introduction
Since the beginning of the "You must know. Net" series, anytao has received a lot of attention and support from everyone, providing great encouragement and motivation. In the past, I found that many garden friends focused on understanding the issue of IL code. For me, this is really good news, because it is obvious that our thinking is slowly changing from the application to the bottom layer, and the skill is an accumulation of one aspect, in my opinion, the underlying exploration is also an essential practice. If we choose to focus on this practice, we should choose how to start it. First, we should pay attention to anytao's "what you must know. net series can provide you with a shortcut, less effort; secondly, you should have a deeper understanding of master-level works, such as applied Microsoft. net Framework programming,. net essence; again, it is growing from the blog and msdn knowledge base like me. Well, in addition to making an advertisement for myself, I think we have a deeper understanding of the Il code in whatever way. net and. the nature of the net application is definitely different, which is the reason for this study and sharing.
So, if we want to understand the Il code, we need to know the benefits of Il. Time is precious for every programmer. You must know the value of your investment and then decide the invested capital. For. Net programmers, the Il code means:
· The general language foundation is. when we disagree with the program running results, how to look at the surface through the essence requires us to start from the essence, then Il is the foundation you must know;
· Metadata and IL language are the basis of CLR. Understanding the necessary intermediate language is a shortcut to gain a deeper understanding of CLR;
· A large number of case analyses are based on IL. Therefore, understanding Il is an essential foundation for reading others' code and can give you more insights.
Obviously, these advantages are enough to entice us to spend time and energy exploring them. However, understanding the benefits of IL does not mean that we should pay too much attention to Il. Some people can even write a bunch of IL code to implement a simple hello World Program, but as we know, programming has gone through decades of development. If you are immersed in history, there is no other explanation except your mind. Otherwise, we will see that any code is analyzed from the perspective of Il and will go into another misunderstanding. Our aim is to pursue but not to mention it.
Therefore, with the above reasons and baseline that should be understood and should not be excessively baseline, this article begins to understand the Il code in the way I think, the author hopes that through the elaboration and analysis in this article, everyone can have an overview of Il, and use this method to understand their own code in normal project practices.. net. I think this method should be the best practice worth advocating and exerting. Do you believe it or not? Haha.
2. Use tools
As the saying goes, to do a good job, you must first sharpen it. The main sources of ilare iladsm.exeand reflector.exe. These two tools are the basis for understanding Il, And the principle is to view il code through reflection mechanism.
· Iladsm.exe
Open the. NET Framework SKD command prompt line, enter ildasm and press enter to open it ,:
It is an example of the familiar article "13th back: Getting to know Il from Hello, world". The meaning of the tree symbol can be explained in a classic help example of msdn, as shown in:
Chart Source: msdn)
·
Reflector is an exciting decompilation tool developed by Lutz roeder. The current version is version 5.0.35.0, which supports. net3.0 and has powerful functions. It is more flexible than ildasm in use ,:
It can be easily decompiled into multiple languages, such as Il, C #, VB, and Delphi. It is the best tool to gain an in-depth understanding of Il.
In this document, we use the simplest iladsm.exe as the instruction tool.
3. Analysis Structure
For more information about the Il structure, see "13th back: Getting to know Il From hello and world". You do not need to write too much ink here, in fact, the structure of IL itself is not very complex, just understand the general system.
4. parse Common commands
On the basis of understanding the structure of the Il file, we can learn the commonly used il commands, so that we can basically understand the standard of Il, therefore, the analysis of Common commands in Il is the focus and key points of this article. Through Explanations, examples, and Analysis of Common commands, we can gradually understand the world of your unfamiliar language.
The IL instruction set includes about 200 basic instruction sets and Object Model instruction sets. It is wise for us to digest so many unfamiliar instructions, just like keywords in advanced languages, we only use one of them for standalone drinks. The revolutionary tradition of grasping the large and small scales is also an effective learning method. For detailed instruction set explanations, please download the [msil instruction speed query manual].
4.1 newobj and initobj
The newobj and intiobj commands are like two brothers. They often confuse us, but we don't know why. Although we know each other, we don't know much about them. This feeling is depressing. Let's look at them:
Code Introduction
Instructions
In-depth analysis
From the above Code, what conclusions can we draw that are worth further consideration?
Msdn explains that newobj is used to allocate and initialize objects, while initobj is used to initialize value types.
So how does newobj allocate memory and initialize the object? How does initobj initialize the value type?
Obviously, the newobj command has already been introduced in "back 5: Let's get down to the keyword-let's talk about newobj". To put it simply, we can draw conclusions about newobj:
· Allocate all the memory space required for the specified type from the managed heap.
· Before calling and executing the constructor initialization, first initialize the object appended members: one is the pointer to the method table of this type; the other is syncblockindex, which is used for thread synchronization. All objects contain the two additional members for object management.
· The last step is to call the constructor for initialization. Return the reference address of the new object.
The role of initobj can be summarized as follows:
· Construct a new value type to complete value type initialization. It is worth noting that such constructor does not need to call the value type. What is the specific execution process? In the preceding example, the execution result of initobj mystruct is to convert the reference type in mystruct to null at the beginning, and set the primitive type to 0.
Therefore, the value type initialization can be:
// Initobj method initialization value type
Initobj anytao.net. my_must_net.il.mystruct
You can also directly call the constructor to complete initialization.
Mystruct MS = new mystruct (123 );
Corresponding to Il is the call to the CTO of the constructor.
// Call the constructor method to initialize the Value Type
Call instance void anytao.net. my_must_net.il.mystruct:. ctor (int32)
· Initobj is also used to set NULL for the pointer of the specified storage unit ). Although this operation is not common, it should be noted.
It can be seen that both newobj and initobj have the function of completing instance initialization, but the execution process varies depending on different types. The main differences include:
· Newobj is used to allocate and initialize objects, while initobj is used to initialize value types. Therefore, it can be said that newobj allocates memory in the heap and completes initialization. initobj initializes the memory allocated on the stack, therefore, the value type has allocated memory on the stack during compilation.
· Newobj calls constructor during initialization. initobj does not call constructor, but directly sets NULL for the instance.
· Newobj has memory allocation, while initobj only initializes data.
There are other situations worth noting about object creation, such:
· The newarr command is used to create a one-dimensional array starting from zero. The multi-dimensional or non-starting one-dimensional array is still created by the newobj command.
· The creation of the string type is completed by the ldstr command. The specific discussion will be discussed below.
4.2 call, callvirt, and calli
The call, callvirt, and calli commands are used to call methods. These are just a few friends we are not familiar with in Il. So what are the differences between these digits when they are also called as methods? We first make a general description of it, and then go to the in-depth analysis layer through code and instances.
· Call uses static scheduling, that is, the Scheduling Method Based on the static type of the reference type.
· Callvirt uses virtual scheduling, that is, scheduling methods based on dynamic types of reference types;
· Calli, also known as indirect call, is used to execute method calls through function pointers. Direct calls are of course the previous ones: Call and callvirt.
However, despite the above general conclusions, call and callvirt cannot be generalized. In some cases, call can call virtual methods, while callvirt can also call non-virtual methods. The specific analysis will be carried out in future articles, so we will not do too much analysis for the time being.
5. Conclusion
Starting from several key commands of Il, this article tries to gradually uncover the mysteries and confusions of IL through comparative analysis and in-depth analysis, as we have emphasized at the beginning, this article is just a beginning or a stage, and the exploration of Il is just like in my own step, and I am continuing to do so. NET technology world can have more insights. I hope that through continuous efforts, I will gradually explore the. Net world from the Il world. In the future discussions, we will continue to grow on this topic.