Understanding metadata and IL (ii) <Article 5>

Source: Internet
Author: User

Book connection:

24th back: Understanding metadata and IL (I), 25th back: Understanding metadata and IL (medium)

Let's continue.

Finally, let's talk about the role of metadata and IL in JIT compilation. Although the two rounds are not paved, it is not too much, because only adequate cognition can give enough experience, and so can technology. Then, let's start following the path of method calling and the power contributed by metadata and IL in that mysterious moment.

5. Metadata and Il during JIT compilation

CLR only executes the local machine code, so the JIT compilation function is to parse the Il code into the machine code for execution at runtime. For JIT compilation, we will take a dedicated space for a comprehensive understanding. This article only focuses on the role of metadata and IL in program execution and participation details. First, Il is executed based on the stack. When calling a method, method parameters, local variables, and return values are allocated to the stack and the call process is executed, since we focus on JIT compilation, we will naturally focus on method execution, because JIT compilation is triggered by executing method calls.

First, add new materials to the code at the beginning of this article:

// Release : code04, 2009/02/24
// Author : Anytao, http://www.anytao.com
// List  : Base.cs
public class Base
{
   public void M()
   {
     Console.WriteLine("M in Base");
   }

   public virtual void N()
   {
     Console.WriteLine("N in Base");
   }
}

Also:

// Release : code05, 2009/02/24
// Author : Anytao, http://www.anytao.com
// List  : Three.cs
public class Three : Base
{
   private static int ID { get; set; }

   public override void N()
   {
     //Something new in Three
     Console.WriteLine("N in Three");
   }

   public void M()
   {
     Console.WriteLine("M in Three");

     M1();
   }

   public void M1()
   {
     Console.WriteLine("M1 in Three");
   }
}

Also execute the Code:

static void Main(string[] args)
{
   Base three = new Three();
   three.M();
   three.N();
}

 

Small View method table

In this example, when calling the main method, it is accompanied by the creation of the three instance and the loading of the corresponding type information. We will first put the secret created by the type information in future content, so that we can leave some suspense to play in the future, haha. However, type loading must be completed before the instance is created, that is, the method table that we often mention is created. Type loading is executed by Class Loader. In short, it obtains the corresponding type information from the metadata table and creates a method table (including the corinfo_class_struct structure ), its structure mainly includes non-virtual method tables and virtual method tables, which are arranged in the sequence of inherited virtual methods, newly introduced virtual methods, instance methods, and static methods, taking the three type as an example, the corinfo_class_struct structure can be expressed:

Note: In this example, three does not define any static method. In its method table, the parent class method N has been overwritten by a subclass, and because of the existence of static members, CLR automatically creates a Type constructor. For details, see what you must know. net section 1.2 "What Is Inheritance ".

We can understand the corresponding method table information through loading SOS debugging:

Break a breakpoint at the three. N () call to view the dump information at this time point, just like a memory snapshot. The photographer's level is what he finds.

Then, obtain the method table address (0x002a354c) through the dumpheap loading type information ),

!dumpheap -type Three
Address    MT   Size
01d332c4 002a354c    12
total 1 objects
Statistics:
    MT  Count  TotalSize Class Name
002a354c    1      12 Anytao.Insidenet.MetadataIL.Three

View related methoddesc information with dumpmt Based on the MT address,

!dumpmt -md 002a354c
EEClass: 002a15b4
Module: 002a2f2c
Name: Anytao.Insidenet.MetadataIL.Three
mdToken: 02000003 (E:\anytao\Today\OnWriting\MetadataIL\Anytao.Insidenet.MetadataIL\Anytao.Insidenet.MetadataIL\bin\Debug\Anytao.Insidenet.MetadataIL.exe)
BaseSize: 0xc
ComponentSize: 0x0
Number of IFaces in IFaceMap: 0
Slots in VTable: 10
--------------------------------------
MethodDesc Table
   Entry MethodDesc   JIT Name
6f756a70  6f5d1328  PreJIT System.Object.ToString()
6f756a90  6f5d1330  PreJIT System.Object.Equals(System.Object)
6f756b00  6f5d1360  PreJIT System.Object.GetHashCode()
6f7c7460  6f5d1384  PreJIT System.Object.Finalize()
002ac0b8  002a3514   NONE Anytao.Insidenet.MetadataIL.Three.N()
002ac0d0  002a3540   JIT Anytao.Insidenet.MetadataIL.Three..ctor()
002ac0a8  002a34f4   NONE Anytao.Insidenet.MetadataIL.Three.get_ID()
002ac0b0  002a3504   NONE Anytao.Insidenet.MetadataIL.Three.set_ID(Int32)
002ac0c0  002a3520   NONE Anytao.Insidenet.MetadataIL.Three.M()
002ac0c8  002a3530   NONE Anytao.Insidenet.MetadataIL.Three.M1()

After a simple dump, the method table information is almost the same as the information shown in the figure. A careful audience may find that the dump information does not contain three: cctor (), so you are right. The cctor shown in the figure is specially added based on the implementation of the Type constructor (static constructor) for three, and the dump method table in the Code does not include the Type constructor, this is a small carelessness. I hope you can see it carefully.

 

Detailed execution rules

The specific execution process is as follows:

Class Loader loads related metadata information from the typedef metadata table, including the current type, inheriting all parent classes of the hierarchy, and implementing interface metadata. Based on this information, the corinfo_class_struct structure is established:

Of course, for Class Loader, we can do a little bit of knowledge:

 

Class: Class Loader

Classic loader is one of the basic components provided by CLR. as its name implies, it loads a class to CLR and Class Loader extracts metadata and IL from the PE file, and load it to the runtime memory. Simply put, it is the epitome of the whole process we will introduce below.

Of course, if you are always worried about the CLR classic loader, you cannot let it go. So, we can also refer to the msdn documents to implement the custom classic loader [How to: Write A Class Loader], hoping that it can provide a clear thinking.

 

After the method is loaded, all the method table slots in corinfo_class_struct before the method execution Save the behavior logic that the method should execute. The information is saved in the structure called methodsdesc, methoddesc is initialized to point to the Il code, and also contains a prejitstub address pointing to triggering JIT compilation, as follows:

 

All the methods described above point to their respective il Code addresses and JIT compilers. Here we will only describe the N () method as an example. For details, refer to the relevant content of msdn.

Simply put, any method will first trigger JIT compilation upon first execution. The main task of JIT is to translate the Il code into native code, insert the JMP command address pointing to the native code to overwrite the original call JIT compiler command:

When this method is executed again, because the machine code address is saved in methoddesc, The x86 (x64) machine code will be directly executed without executing the JIT compilation process.

Throughout the entire JIT compilation process, the implementation of the details is far more complex than what we present here. In the rough steps, we roughly understand the roles, roles, and relationships of metadata and IL in the entire process, to understand the CLR operating mechanism, it is wise to make proper choices. If you have more thoughts on exploring the mechanism, let's make it simple and complex in the years to come, but I believe this must be a wonderful journey.

6 conclusion

Metadata describes the static structure, while IL explains the dynamic execution, which carries too many technical mysteries.

When this article is coming to an end, I find that the new problems introduced by this will come one after another. The method calling, assembly, program domain, and CLR loading process will become invisible in the analysis of metadata and Il, also drive me to pay attention to what you must know. net. In addition to the complex concepts and essence, we can comprehensively grasp all the content from points and points to form a comprehensive understanding and a line of understanding. In the future, anytao will continue to share the following information:

 

Series announcements

Assembly and module

Program domain

CLR Loading Process

JIT compilation

Method call

Reflection

Others...

 

Due to the busy schedule, I cannot give you a clear timetable, but I try to give you enough gains for every content, if you are right. net is always interested, so stay tuned to what you must know. net.

Source: http://anytao.cnblogs.com/

The copyright of this article is owned by the author. You are welcome to repost this article, but you must keep this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be entitled to pursue legal liability.

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.