为什么 C# 编译器在 IL 中发出额外的操作码?
如果我有一个方法 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 中发出额外的操作码?
我正在使用调试模式。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要用于调试和断点支持;请参阅此处的答案:为什么这些 nop 指令出现在我的调试版本中?
Mostly for debugging & breakpoint support; See an answer here: Why are these nop instructions in my debug build?