gpt4 book ai didi

cil - MSIL : Superfluous branch

转载 作者:行者123 更新时间:2023-12-04 05:04:22 26 4
gpt4 key购买 nike

考虑以下C#代码段:

static string input = null;
static string output = null;

static void Main(string[] args)
{
input = "input";
output = CallMe(input);
}

public static string CallMe(string input)
{
output = "output";
return output;
}

使用Reflector进行拆卸显示:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 8
L_0000: nop
L_0001: ldstr "input"
L_0006: stsfld string Reflector_Test.Program::input
L_000b: ldsfld string Reflector_Test.Program::input
L_0010: call string Reflector_Test.Program::CallMe(string)
L_0015: stsfld string Reflector_Test.Program::output
L_001a: ret
}

.method public hidebysig static string CallMe(string input) cil managed
{
.maxstack 1
.locals init (
[0] string CS$1$0000)
L_0000: nop
L_0001: ldstr "output"
L_0006: stsfld string Reflector_Test.Program::output
L_000b: ldsfld string Reflector_Test.Program::output
L_0010: stloc.0
L_0011: br.s L_0013
L_0013: ldloc.0
L_0014: ret
}

令我困惑的是:
L_0010: stloc.0 
L_0011: br.s L_0013
L_0013: ldloc.0

它存储该项目,分支到下一行(无论如何将执行该行),然后再次加载它。

是否有一个原因?

最佳答案

这仅在Debug中发生,而在Release中不发生。我怀疑它可以在调试过程中提供帮助。它可能使您可以在语句中插入断点并查看返回值。

请注意,发行版具有更简洁的IL:

.method private hidebysig static void Main(string[] args) cil managed
{
.maxstack 8
L_0000: ldstr "input"
L_0005: stsfld string Reflector_Test.Program::input
L_000a: ldsfld string Reflector_Test.Program::input
L_000f: call string Reflector_Test.Program::CallMe(string)
L_0014: stsfld string Reflector_Test.Program::output
L_0019: ret
}




.method public hidebysig static string CallMe(string input) cil managed
{
.maxstack 8
L_0000: ldstr "output"
L_0005: stsfld string Reflector_Test.Program::output
L_000a: ldsfld string Reflector_Test.Program::output
L_000f: ret
}

关于cil - MSIL : Superfluous branch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/675278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com