2011-10-06 71 views
6

如果我有一個方法Multiply定義爲:爲什麼C#編譯器在IL中發出額外的OpCode?

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

那麼爲什麼編譯器發出此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 
} 

正如你所看到的,它也包含一些額外的操作碼這不實際上我期望下面的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 
} 

它做的是同樣的事情。

所以問題是,爲什麼編譯器會在IL中發出額外的OpCodes?

我正在使用調試模式。

+1

調試或發佈配置? –

+0

@AlexFarber:調試。 – Nawaz

+0

至少'NOP'只是調試,並創建某種類型的序列點,JITter可能不會重新排序指令。這有助於調試。 – CodesInChaos

回答

相關問題