You have to know how .Net code "managed"?

Basically, all managed code directives are translated into IL directives, all IL directives are translated into assembly directives. The path is:

C# code/Vb.Net code/other .Net code    ====>     IL directive      ==JIT==>    assembly directive (machine code)

Take the method

System.Linq.Expressions.ExpressionCompiler.CompileDynamicLambda(...)

for example.

IL code dumpped by IL DASM

IL Code
.method private hidebysig instance class [mscorlib]System.Delegate 
        CompileDynamicLambda(
class System.Linq.Expressions.LambdaExpression lambda) cil managed
{
  
// Code size       87 (0x57)
  .maxstack  5
  
.locals init (class System.Linq.Expressions.ExpressionCompiler/LambdaInfo V_0,
           
class System.Runtime.CompilerServices.ExecutionScope V_1,
           
int32 V_2)
  
IL_0000:  ldarg.0
  
IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<class System.Linq.Expressions.ExpressionCompiler/LambdaInfo>::.ctor()
  
IL_0006:  stfld      class [mscorlib]System.Collections.Generic.List`1<class System.Linq.Expressions.ExpressionCompiler/LambdaInfo> System.Linq.Expressions.ExpressionCompiler::lambdas
  
IL_000b:  ldarg.0
  
IL_000c:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<object>::.ctor()
  
IL_0011:  stfld      class [mscorlib]System.Collections.Generic.List`1<object> System.Linq.Expressions.ExpressionCompiler::globals
  
IL_0016:  ldarg.0
  
IL_0017:  ldarg.1
  
IL_0018:  call       instance int32 System.Linq.Expressions.ExpressionCompiler::GenerateLambda(class System.Linq.Expressions.LambdaExpression)
  
IL_001d:  stloc.2
  
IL_001e:  ldarg.0
  
IL_001f:  ldfld      class [mscorlib]System.Collections.Generic.List`1<class System.Linq.Expressions.ExpressionCompiler/LambdaInfo> System.Linq.Expressions.ExpressionCompiler::lambdas
  
IL_0024:  ldloc.2
  
IL_0025:  callvirt   instance !0 class [mscorlib]System.Collections.Generic.List`1<class System.Linq.Expressions.ExpressionCompiler/LambdaInfo>::get_Item(int32)
  
IL_002a:  stloc.0
  
IL_002b:  ldnull
  
IL_002c:  ldloc.0
  
IL_002d:  ldarg.0
  
IL_002e:  ldfld      class [mscorlib]System.Collections.Generic.List`1<object> System.Linq.Expressions.ExpressionCompiler::globals
  
IL_0033:  callvirt   instance !0[] class [mscorlib]System.Collections.Generic.List`1<object>::ToArray()
  
IL_0038:  ldnull
  
IL_0039:  newobj     instance void System.Runtime.CompilerServices.ExecutionScope::.ctor(class System.Runtime.CompilerServices.ExecutionScope,
                                                                                           
class System.Linq.Expressions.ExpressionCompiler/LambdaInfo,
                                                                                           
object[],
                                                                                           
object[])
  
IL_003e:  stloc.1
  
IL_003f:  ldloc.0
  
IL_0040:  ldfld      class [mscorlib]System.Reflection.MethodInfo System.Linq.Expressions.ExpressionCompiler/LambdaInfo::Method
  
IL_0045:  castclass  [mscorlib]System.Reflection.Emit.DynamicMethod
  
IL_004a:  ldarg.1
  
IL_004b:  callvirt   instance class [mscorlib]System.Type System.Linq.Expressions.Expression::get_Type()
  
IL_0050:  ldloc.1
  
IL_0051:  callvirt   instance class [mscorlib]System.Delegate [mscorlib]System.Reflection.Emit.DynamicMethod::CreateDelegate(class [mscorlib]System.Type,
                                                                                                                               
object)
  
IL_0056:  ret
// end of method ExpressionCompiler::CompileDynamicLambda

assembly code dumped by SOS in runtime

Assembly Code
!u 6c20cef0
preJIT generated code
System.Linq.Expressions.ExpressionCompiler.CompileDynamicLambda(System.Linq.Expressions.LambdaExpression)
Begin 6c318010, size c7
6C318010 
55               push        ebp
6C318011 8BEC             
mov         ebp,esp
6C318013 
57               push        edi
6C318014 
56               push        esi
6C318015 
53               push        ebx
6C318016 
50               push        eax
6C318017 8BF9             
mov         edi,ecx
6C318019 8BDA             
mov         ebx,edx
6C31801B B9B0A4376C       
mov         ecx,6C37A4B0h (MT: System.Collections.Generic.List`1[[System.Linq.Expressions.ExpressionCompiler+LambdaInfo, System.Core]])
6C318020 E853EFF1FF       
call        6C236F78
6C318025 8BF0             
mov         esi,eax
6C318027 8BCE             
mov         ecx,esi
6C318029 E8D2D1F1FF       
call        6C235200 (System.Collections.Generic.List`1[[System.__Canon, mscorlib]]..ctor(), mdToken: 060019e0)
6C31802E 8D5704           
lea         edx,[edi+4]
6C318031 E8BAEFF1FF       
call        6C236FF0
6C318036 8B0DF4E01D6C     
mov         ecx,dword ptr ds:[6C1DE0F4h] (MT: System.Collections.Generic.List`1[[System.Object, mscorlib]])
6C31803C E837EFF1FF       
call        6C236F78
6C318041 8BF0             
mov         esi,eax
6C318043 8BCE             
mov         ecx,esi
6C318045 E8B6D1F1FF       
call        6C235200 (System.Collections.Generic.List`1[[System.__Canon, mscorlib]]..ctor(), mdToken: 060019e0)
6C31804A 8D5708           
lea         edx,[edi+8]
6C31804D E89EEFF1FF       
call        6C236FF0
6C318052 8BD3             
mov         edx,ebx
6C318054 8BCF             
mov         ecx,edi
6C318056 E8CD7AF2FF       
call        6C23FB28 (System.Linq.Expressions.ExpressionCompiler.GenerateLambda(System.Linq.Expressions.LambdaExpression), mdToken: 06000374)
6C31805B 8B4F04           
mov         ecx,dword ptr [edi+4]
6C31805E 8BD0             
mov         edx,eax
6C318060 
3909             cmp         dword ptr [ecx],ecx
6C318062 E8D9D3F1FF       
call        6C235440 (System.Collections.Generic.List`1[[System.__Canon, mscorlib]].get_Item(Int32), mdToken: 060019eb)
6C318067 8945F0           
mov         dword ptr [ebp-10h],eax
6C31806A B9ACAD386C       
mov         ecx,6C38ADACh (MT: System.Runtime.CompilerServices.ExecutionScope)
6C31806F E804EFF1FF       
call        6C236F78
6C318074 8BF0             
mov         esi,eax
6C318076 8B4F08           
mov         ecx,dword ptr [edi+8]
6C318079 
3909             cmp         dword ptr [ecx],ecx
6C31807B E830D2F1FF       
call        6C2352B0 (System.Collections.Generic.List`1[[System.__Canon, mscorlib]].ToArray(), mdToken: 06001a25)
6C318080 8BC8             
mov         ecx,eax
6C318082 33D2             
xor         edx,edx
6C318084 
895604           mov         dword ptr [esi+4],edx
6C318087 8B45F0           
mov         eax,dword ptr [ebp-10h]
6C31808A 8D5610           
lea         edx,[esi+10h]
6C31808D E8CEEEF1FF       
call        6C236F60
6C318092 8D5608           
lea         edx,[esi+8]
6C318095 E8AEEFF1FF       
call        6C237048
6C31809A 33D2             
xor         edx,edx
6C31809C 8956
0C           mov         dword ptr [esi+0Ch],edx
6C31809F 8B45F0           
mov         eax,dword ptr [ebp-10h]
6C3180A2 8B780C           
mov         edi,dword ptr [eax+0Ch]
6C3180A5 85FF             
test        edi,edi
6C3180A7 741B             
je          6C3180C4
6C3180A9 8B07             
mov         eax,dword ptr [edi]
6C3180AB 3B057CE11D6C     
cmp         eax,dword ptr ds:[6C1DE17Ch]
6C3180B1 
7502             jne         6C3180B5
6C3180B3 EB0F             
jmp         6C3180C4
6C3180B5 8B0D7CE11D6C     
mov         ecx,dword ptr ds:[6C1DE17Ch] (MT: System.Reflection.Emit.DynamicMethod)
6C3180BB 8BD7             
mov         edx,edi
6C3180BD E83EEFF1FF       
call        6C237000
6C3180C2 8BF8             
mov         edi,eax
6C3180C4 8BCF             
mov         ecx,edi
6C3180C6 8B5304           
mov         edx,dword ptr [ebx+4]
6C3180C9 
56               push        esi
6C3180CA 
3909             cmp         dword ptr [ecx],ecx
6C3180CC E877D3F1FF       
call        6C235448 (System.Reflection.Emit.DynamicMethod.CreateDelegate(System.Type, System.Object), mdToken: 06004946)
6C3180D1 
59               pop         ecx
6C3180D2 5B               
pop         ebx
6C3180D3 5E               
pop         esi
6C3180D4 5F               
pop         edi
6C3180D5 5D               
pop         ebp
6C3180D6 C3               
ret
原文地址:https://www.cnblogs.com/silva/p/2126258.html