In-depth understanding of the JIT compilation method of .NET

xiaoxiao2021-03-06  41

The CLR only performs the machine code of this unit. There are two ways to generate machine code: real-time compilation (JIT) and pre-compiled mode (generating native image). Here, I want to talk about JIT. The CLR uses the type of method to route all method calls. The method of type is composed of multiple entrance items. Each entry is directed to a unique settler routine. When initialization, each stub routine contains a call to the JIT compiler of the CLR (it is disclosed by the internal PRESTUBWORKER program). After generating this machine code after the JIT compiler, it will rewrite the root routine, insert a JMP instruction to jump to the code just just the JIT compiler. The JIT compiler is compiled into the corresponding local machine code versions when you want to call a method. This will optimize the work set of the program. For the following example: // use system; public class bob {static int x; static void a () {x = 2;} static void b () {x = 3;} static void c () {x = 4; Public static void f () {c (); b (); a ();}} public class myclass {public static void main () {bob.f ();}} JIT debugging with the debugger.

First, look at the assembly display of each method: the compilation of main () is displayed as: Push Ebpmov EBP, ESP // Call Bob.f () Method Call DWORD PTR DS: [00975394H] NOP POP EBPRET [Note] 00975394h is Bob .f () The corresponding memory address in the internal data structure in Corinfo_Class_Struct, the content in this address is the start address of the corresponding settle routine.

F () assembly is displayed as: PUSH EBPMOV EBP, ESP // Call BOB.C () Method Call DWORD PTR DS: [00975390H] // Call Bob.B () Method Call DWORD PTR DS: [0097538CH] // Call Bob.a () Method Call DWORD PTR DS: [00975388H] Noppop EBPRET [Note] 00975390,0097538c, 00975388 is bob.c (), bob.b (), bob.a () in corInfo_class_struct this internal data structure The corresponding memory address, the content in this address is the start address of the corresponding settlement routine.

C () assembly shows: Push EBPMOV EBP, ESPADD DWORD PTR DS: [0097539CH], 4NOPPOP EBPRET [Note] 0097539C is the memory address of Bob.x.

B () assembly is displayed as: PUSH EBPMOV EBP, ESPADD DWORD PTR DS: [0097539CH], 3NOPPOP EBPRET [Note] 0097539C is Bob.x memory address.

A () assembly is shown as: PUSH EBPMOV EBP, ESPADD DWORD PTR DS: [0097539CH], 2NOPPOP EBPRET [Note] 0097539C is the memory address of Bob.x.

Below, let's take a look at the debugging, the address is 00975394H, 00975390H, 0097538CH, 00975388H content: 0x00975384 2B 85 BF 79 03 53 97 00 0X009753 53 97 00 0X00975394 33 53 97 00 43 53 97 00 00 00 00 00 00 00 00 Green is Bob.f () corresponding to the memory address in the internal data structure; purple is bob.c () in Corinfo_Class_Struct, the memory address corresponding in the internal data structure; gray is BOB .b () in the memory address corresponding to the internal data structure of Corinfo_Class_Struct; yellow is bob.a () corresponding to the memory address in Corinfo_Class_Struct This internal data structure; [Note] Red content is the value of Bob.x. ! Do not believe? Then, you're looking at the changes in the red color during the debugging process, you will understand: enter f (): 0x00975384 2B 85 BF 79 03 53 97 00 0X00975394 33 53 97 00 43 53 97 00 00 00 00 00 00c () plus 4 after the change to: 0x00975384 2B 85 BF 79 03 53 97 00 13 53 97 00 23 53 97 00 0x00975394 33 53 97 00 43 53 97 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 00 00 00 加 3 turn to: 0x00975384 2B 85 BF 79 03 53 97 00 03 97 00 23 53 97 00 0x00975394 33 53 97 00 00 00 00 00 00 00 plus 2 after: 0x00975384 2B 85 BF 79 03 53 97 00 13 53 97 00 23 53 97 00 0x00975394 33 53 97 00 00 00 00 00 00 00 00 below Let us see the root departure before calling bob.f () Content: 0x00975303 E8 D0 52 7D FF 04 00 10 00 50 20 00 c0 02 00 FE 0x00975313 E8 C0 52 7D FF 05 00 10 00 6C 20 00 c0 03 00 Fc 0x00975323 E8 B0 52 7D FF 06 00 10 00 88 20 00 C0 04 00 FA 0x00975333 E8 A0 52 7D FF 07 00 10 00 A4 20 00 c0 05 00 F8 is the content of the bob.f () of the retrieval routine; the purple is the contents of the bob.c () of the root routine The gray is the content of the bob.b () of the retrieval routine; the yellow is bob.a () the content of the retrieval routine; let us see the root departure from the BOB.f () method Content: 0x00975303 E8 D0 52 7D FF 04 00 10 00 50 20 00 c0 02 00 Fe 0x00975313 E8 C0 52 7D FF 05 00 10 00 6C 20 00 C0 03 00 FC 0x00975323 E8 B0 52 7D FF 06 00 10 00 88 20 00 C0 04 00 FA 0x00975333 E9 40 AD 39 06 07 00 10 00 78 00 D1 06 05 00 F8 easy to see, Only the contents of the bob.f () of the researcher have changed.

This shows that the JIT compiler is called. At the same time, the compiler converts F () CIL method body into machine code versions in address space. Replace the original content of the stub routine. After replacing, the first address of the F () method is the first address of 0x06d10078 (content at blue labels). Do you not believe? Well, let's take a look at the memory of 0x06d10078: 0x06d10078 55 8b EC FF 15 90 53 97 00 FF 15 8c 53 97 00 ff 0x06d10088 15 88 53 97 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! You may wish to look back Look at F () assembly display, here: Purple is not a memory address corresponding to bob.c () in Corinfo_Class_Struct, the gray is not for bob.b () in corInfo_class_struct this internal data structure The corresponding memory address; the yellow is not for bob.a () in the memory address corresponding to the internal data structure of CORINFO_CLASS_STRUCT; notice the F () assembly display: // Call Bob.c () method Call DWORD PTR DS: [00975390H] // Call Bob.B () method Call DWORD PTR DS: [0097538CH] // Call Bob.a () Method Call DWORD PTR DS: [00975388H] understand! Ok, the following is the same for C (), B (), and A () call is also the same.

转载请注明原文地址:https://www.9cbs.com/read-55482.html

New Post(0)