Why does C# compiler emit additional OpCodes in IL?

孤街醉人 提交于 2019-12-12 10:48:19

问题


If I've a method Multiply defined as:

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

Then why does the compiler emit this 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 
}

As you can see, it also contains some additional OpCodes which don't make sense to me, when in fact I expect the following 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 
}

which does the very same thing.

So the question is, why does the compiler emit additional OpCodes in IL?

I'm using Debug mode.


回答1:


Mostly for debugging & breakpoint support; See an answer here: Why are these nop instructions in my debug build?



来源:https://stackoverflow.com/questions/7672624/why-does-c-sharp-compiler-emit-additional-opcodes-in-il

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!