2011-10-06 16 views
6

Se ho un metodo Multiply definito come:Perché il compilatore C# emette OpCode aggiuntivi in ​​IL?

public static class Experiment 
{ 
    public static int Multiply(int a, int b) 
    { 
     return a * b; 
    } 
} 

allora perché il compilatore emette questo IL:

.method public hidebysig static int32 Multiply(int32 a, int32 b) cil managed 
{ 
    .maxstack 2    //why is it not 16? 
    .locals init (
     [0] int32 CS$1$0000) //what is this? 
    L_0000: nop    //why this? 
    L_0001: ldarg.0 
    L_0002: ldarg.1 
    L_0003: mul 
    L_0004: stloc.0   //why this? 
    L_0005: br.s L_0007  //why this? 
    L_0007: ldloc.0   //why this? 
    L_0008: ret 
} 

Come si può vedere, contiene anche alcune OpCodes aggiuntivi che non lo fanno ho senso per me, quando in realtà mi aspetto il seguente IL:

.method public hidebysig static int32 MyMethod(int32 a, int32 b) cil managed 
{ 
    .maxstack 16 
    L_0000: ldarg.0 
    L_0001: ldarg.1 
    L_0002: mul 
    L_0003: ret 
} 

che fa la stessa cosa.

Quindi la domanda è: perché il compilatore emette OpCode aggiuntivi in ​​IL?

Sto usando la modalità Debug.

+1

Configurazione debug o rilascio? –

+0

@AlexFarber: debug. – Nawaz

+0

Almeno il 'NOP' è solo di debug e crea una sorta di punto di sequenza su cui JITter non può riordinare le istruzioni. Questo aiuta con il debug. – CodesInChaos

risposta

Problemi correlati