[C++/CLI编程宝典][6]IL中间语言

 

继续上节的ildasm.exe对main.exe的IL中间语言的分析。我们知道我们在main.exe中定义了NativeClass,ValueStruct和RefClass。下面我们将做类型与IL中间语言的一一对应:

 

1)NativeClass

C++/CLI代码:

Code

 

IL中间代码:

NativeClass的定义:

[C++/CLI编程宝典][6]IL中间语言_第1张图片

上面我们看到了NativeClass的定义,他继承于System.ValueType,但是原本属于该类的成员现在都不在了,原因是ISOC++的class的成员被编译为IL中间代码后变为全局的静态成员,看下图:

2)ValueStruct

ValueStruct的定义:

Code

 

IL中间代码:(可以看到ValueStruct继承于System.ValueType)

3)RefClass

RefClass的定义:

Code

 

IL中间代码: (可以看到RefClass继承于System.IDisposable)

[C++/CLI编程宝典][6]IL中间语言_第2张图片

4)main函数

main函数代码:

 

Code

 

IL中间代码main的函数头(可以看到被编译为全局的静态函数):

具体的main实现:

 

.method  assembly static  int32   modopt ([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) 
        main() 
cil   managed
{
  .vtentry 
1  :  1
  
//  Code size       200 (0xc8)
   .maxstack    3
  
.locals  ( class  RefClass V_0,
           valuetype NativeClass* V_1,
           
class  [mscorlib]System.IDisposable V_2,
           valuetype NativeClass* V_3,
           valuetype NativeClass* V_4,
           valuetype NativeClass* 
modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst) V_5,
           
int32  V_6,
           
void * V_7,
           valuetype NativeClass* V_8,
           valuetype std.
' basic_string<char,std::char_traits<char>,std::allocator<char> > ' modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst) V_9,
           valuetype std.
' basic_string<char,std::char_traits<char>,std::allocator<char> > ' * V_10,
           valuetype ValueStruct V_11,
           valuetype std.
' basic_string<char,std::char_traits<char>,std::allocator<char> > '  V_12)
  
IL_0000:    ldnull
  
IL_0001:    stloc.0
  
IL_0002:    ldc.i4.s     28
  
IL_0004:    call         void modopt ([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)  new (uint32)
  
IL_0009:    stloc.1
  .try
  {
    
IL_000a:    ldloc.1
    
IL_000b:    brfalse.s   IL_002b
    
IL_000d:    ldloca.s    V_12
    
IL_000f:    stloc.s     V_10
    
IL_0011:    ldloc.s     V_10
    
IL_0013:    ldsflda     valuetype  ' <CppImplementationDetails> ' .$ArrayType$$$BY0BL@$$CBD  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)  ' ?A0x3ea463b6.unnamed-global-1 '
    
IL_0018:    call        valuetype std. ' basic_string<char,std::char_traits<char>,std::allocator<char> > ' modopt ([mscorlib]System.Runtime.CompilerServices.CallConvThiscall)  ' std.basic_string<char,std::char_traits<char>,std::allocator<char> >.{ctor} ' (valuetype std. ' basic_string<char,std::char_traits<char>,std::allocator<char> > ' modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst),
                                                                                                                                                                                                                                                          
int8   modopt ([mscorlib]System.Runtime.CompilerServices.IsSignUnspecifiedByte)  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)*)
    
IL_001d:    stloc.s     V_9
    
IL_001f:    ldloc.1
    
IL_0020:    ldloc.s     V_9
    
IL_0022:    call        valuetype NativeClass*  modopt ([mscorlib]System.Runtime.CompilerServices.CallConvThiscall)  ' NativeClass.{ctor} ' (valuetype NativeClass*  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst),
                                                                                                                                        valuetype std.
' basic_string<char,std::char_traits<char>,std::allocator<char> > '   modreq ([mscorlib]System.Runtime.CompilerServices.IsCopyConstructed)*)
    
IL_0027:    stloc.s     V_5
    
IL_0029:    br.s        IL_002e
    
IL_002b:    ldc.i4.0
    
IL_002c:    stloc.s     V_5
    
IL_002e:    ldloc.s     V_5
    
IL_0030:    stloc.s     V_8
    
IL_0032:    leave .s    IL_003b
  }  
//  end .try
  fault
  {
    
IL_0034:    ldloc.1
    
IL_0035:    call         void   modopt ([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) delete( void *)
    
IL_003a:    endfinally
  }  
//  end handler
   IL_003b:    ldloc.s     V_8
  
IL_003d:    stloc.s     V_4
  
IL_003f:    ldloc.s     V_4
  
IL_0041:    call         void   modopt ([mscorlib]System.Runtime.CompilerServices.CallConvThiscall) NativeClass.Print(valuetype NativeClass*  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst))
  
IL_0046:    ldloc.s     V_4
  
IL_0048:    stloc.3
  
IL_0049:    ldloc.3
  
IL_004a:    brfalse.s   IL_0057
  
IL_004c:    ldloc.3
  
IL_004d:    ldc.i4.1
  
IL_004e:    call         void modopt ([mscorlib]System.Runtime.CompilerServices.CallConvThiscall) NativeClass.__delDtor(valuetype NativeClass*  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst)  modopt ([mscorlib]System.Runtime.CompilerServices.IsConst),
                                                                                                                      uint32)
  
IL_0053:    stloc.s     V_7
  
IL_0055:    br.s        IL_005a
  
IL_0057:    ldc.i4.0
  
IL_0058:    stloc.s     V_7
  
IL_005a:    ldloca.s    V_11
  
IL_005c:    ldstr       bytearray ( 60  4F 7D  59  0C FF  11   62  2F  66   58   62  A1 7B  76   00     //  `O}Yb/fXb.{v.
                                   61   00  6C  00   75   00   65   00  3C  50  7B 7C 8B  57   01  FF )  //  a.l.u.e.<P{|.W..
   IL_0061:    call         instance   void  ValueStruct::.ctor( string )
  
IL_0066:    ldloca.s    V_11
  
IL_0068:    call         instance   void  ValueStruct::Print()
  
IL_006d:    ldstr       bytearray ( 60  4F 7D  59  0C FF  11   62  2F  66   58   62  A1 7B  72   00     //  `O}Yb/fXb.{r.
                                   65   00   66   00   15  5F  28   75  7B 7C 8B  57   01  FF )        //  e.f.._(u{|.W..
   IL_0072:    newobj       instance   void  RefClass::.ctor( string )
  
IL_0077:    stloc.0
  
IL_0078:    ldloc.0
  
IL_0079:    ldnull
  
IL_007a:    ldftn        void   modopt ([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) StartPrint()
  
IL_0080:    newobj       instance   void  RefClass/PrintDelegate::.ctor( object ,
                                                                   
native  int)
  
IL_0085:    call         instance   void  RefClass::add_StartPrint( class  RefClass/PrintDelegate)
  
IL_008a:    ldloc.0
  
IL_008b:    ldnull
  
IL_008c:    ldftn        void   modopt ([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) EndPrint()
  
IL_0092:    newobj       instance   void  RefClass/PrintDelegate::.ctor( object ,
                                                                   
native  int)
  
IL_0097:    call         instance   void  RefClass::add_EndPrint( class  RefClass/PrintDelegate)
  
IL_009c:    ldloc.0
  
IL_009d:    call         instance   void  RefClass::Print()
  
IL_00a2:    ldloc.0
  
IL_00a3:    ldstr       bytearray ( 60  4F 7D  59  0C FF  11   62  2F  66   58   62  A1 7B  72   00     //  `O}Yb/fXb.{r.
                                   65   00   66   00   15  5F  28   75  7B 7C 8B  57   01  FF B0  73     //  e.f.._(u{|.Ws
                                   28   57   63  6B 1A  90  C7 8F  70   00   72   00  6F  00   70   00     //  (Wck.p.r.o.p.
                                   65   00   72   00   74   00   79   00  5E 5C  27   60  EE 4F  39   65     //  e.r.t.y.^\'`.O9e
                                   10   62   58   54   66   00   65   00   69   00  6C  00   64   00   57  5B    //  .bXTf.e.i.l.d.W[
                                  B5 6B  01  FF )                                      //  .k..
   IL_00a8:    call         instance   void  RefClass::set_Str( string )
  
IL_00ad:    ldloc.0
  
IL_00ae:    call         instance   void  RefClass::Print()
  
IL_00b3:    ldloc.0
  
IL_00b4:    stloc.2
  
IL_00b5:    ldloc.2
  
IL_00b6:    brfalse.s   IL_00c3
  
IL_00b8:    ldloc.2
  
IL_00b9:    callvirt     instance   void  [mscorlib]System.IDisposable::Dispose()
  
IL_00be:    ldc.i4.0
  
IL_00bf:    stloc.s     V_6
  
IL_00c1:    br.s        IL_00c6
  
IL_00c3:    ldc.i4.0
  
IL_00c4:    stloc.s     V_6
  
IL_00c6:    ldc.i4.0
  
IL_00c7:    ret
//  end of method 'Global Functions'::main

 

读者可以将以上的源代码和IL对照,达到对IL的简单理解!

完!

你可能感兴趣的:([C++/CLI编程宝典][6]IL中间语言)