加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么C#编译器在IL中发出额外的OpCode?

发布时间:2020-12-15 04:06:59 所属栏目:百科 来源:网络整理
导读:如果我有一个Multiply方法定义为: 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{ .maxsta
如果我有一个Multiply方法定义为:
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中发出额外的OpCode?

我正在使用调试模式.

解决方法

主要用于调试和断点支持;在这里看到答案: Why are these nop instructions in my debug build?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读