IL 中的 if 是什么样的?

发布于 2024-09-17 22:48:13 字数 79 浏览 4 评论 0原文

if 语句编译成 IL 后会是什么样子?

这是 C# 中非常简单的构造。有人能给我一个更抽象的定义吗?

What does an if statement look like when it's compiled into IL?

It's a very simple construct in C#. Can sombody give me a more abstract definition of what it really is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋心╮凉 2024-09-24 22:48:13

以下是一些 if 语句以及它们如何转换为 IL:

ldc.i4.s 0x2f                      var i = 47;
stloc.0 

ldloc.0                            if (i == 47)
ldc.i4.s 0x2f
bne.un.s L_0012

ldstr "forty-seven!"                   Console.WriteLine("forty-seven!");
call Console::WriteLine

L_0012:
ldloc.0                            if (i > 0)
ldc.i4.0 
ble.s L_0020

ldstr "greater than zero!"             Console.WriteLine("greater than zero!");
call Console::WriteLine

L_0020:
ldloc.0                            bool b = (i != 0);
ldc.i4.0 
ceq 
ldc.i4.0 
ceq 
stloc.1 

ldloc.1                            if (b)
brfalse.s L_0035

ldstr "boolean true!"                  Console.WriteLine("boolean true!");
call Console::WriteLine

L_0035:
ret

这里需要注意的一件事:IL 指令始终是“相反的”。 if (i > 0) 转换为有效的意思是“如果 i <= 0,则跳过 if 的主体堵塞”。

Here are a few if statements and how they translate to IL:

ldc.i4.s 0x2f                      var i = 47;
stloc.0 

ldloc.0                            if (i == 47)
ldc.i4.s 0x2f
bne.un.s L_0012

ldstr "forty-seven!"                   Console.WriteLine("forty-seven!");
call Console::WriteLine

L_0012:
ldloc.0                            if (i > 0)
ldc.i4.0 
ble.s L_0020

ldstr "greater than zero!"             Console.WriteLine("greater than zero!");
call Console::WriteLine

L_0020:
ldloc.0                            bool b = (i != 0);
ldc.i4.0 
ceq 
ldc.i4.0 
ceq 
stloc.1 

ldloc.1                            if (b)
brfalse.s L_0035

ldstr "boolean true!"                  Console.WriteLine("boolean true!");
call Console::WriteLine

L_0035:
ret

One thing to note here: The IL instructions are always the “opposite”. if (i > 0) translates to something that effectively means “if i <= 0, then jump over the body of the if block”.

忆沫 2024-09-24 22:48:13

使用分支指令,根据堆栈顶部的值跳转到目标指令。

brfalse Branch to target if value is zero (false)
brtrue  Branch to target if value is non-zero (true)
beq     Branch to target if equal
bge     Branch to target if greater than or equal to
bgt     Branch to target if greater than
ble     Branch to target if less than or equal to
blt     Branch to target if less than
bne.un  Branch to target if unequal or unordered

A branch instruction is used that will jump to a target instruction depending on the value(s) on top of the stack.

brfalse Branch to target if value is zero (false)
brtrue  Branch to target if value is non-zero (true)
beq     Branch to target if equal
bge     Branch to target if greater than or equal to
bgt     Branch to target if greater than
ble     Branch to target if less than or equal to
blt     Branch to target if less than
bne.un  Branch to target if unequal or unordered
梦醒时光 2024-09-24 22:48:13

这取决于 if 的条件。例如,如果您正在检查 null 的引用,编译器将发出 brfalse 指令(或 brtrue 取决于您所写的内容)。

实际 if 条件将根据条件本身而有所不同,但像 ILDASM 或 Reflector 这样的反汇编器将是了解更多信息的更好工具。

It depends on the condition of the if. For example if you are checking a reference against null the compiler will emit a brfalse instruction (or a brtrue depending on what you wrote).

The actual if condition will differ based on the condition itself but a dissasembler like ILDASM or Reflector would be a better tool for learning more.

半寸时光 2024-09-24 22:48:13

一个简单的例子:

ldloc.1                    // loads first local variable to stack
ldc.i4.0                   // loads constant 0 to stack
beq                        // branch if equal

这将等于

if(i == 0) //if i is the first local variable

其他 if 会有所不同,包括条件分支。这确实太多了,无法在一篇文章中解释,您最好寻找 IL-Code 的介绍。

codeproject 上有一篇关于此的好文章。

A simple example:

ldloc.1                    // loads first local variable to stack
ldc.i4.0                   // loads constant 0 to stack
beq                        // branch if equal

This would be equal to

if(i == 0) //if i is the first local variable

Other ifs would differ, including conditional branches. This really is too much to explain in one post, you are better of looking for an introduction to IL-Code.

There is a nice article on codeproject concerning this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文