What does an if look like in IL?
Asked Answered
R

4

5

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?

Rebellion answered 7/9, 2010 at 23:21 Comment(3)
You can check this using ildasm.exe from a Visual Studio command prompt.Lidstone
It's a very simple construct, period. I don't know in IL, but in assembly code, you perform a comparison and then a branch. Very simple in asm, so one has to assume it's just as simple in a high level language.Fallen
Your question is answered by the statement of this question from earlier today. #3659593Gast
P
11

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”.

Purse answered 7/9, 2010 at 23:33 Comment(3)
Is there a copy-paste error in the chunk starting with L_0020: repeating ltc.i4.0 then ceq?Cassel
@AndyDent No, this is the exact output of the compiler. You can argue that it could be written shorter, but if you just remove one ldc.i4.0 + ceq, the meaning would change: it would then mean i == 0 instead of i != 0.Purse
got it! (I think), in long form, that IL is equivalent to b = ((i==0) == 0)Cassel
S
5

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
Sheik answered 7/9, 2010 at 23:40 Comment(0)
P
4

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.

Proline answered 7/9, 2010 at 23:24 Comment(0)
I
2

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.

Inshore answered 7/9, 2010 at 23:29 Comment(2)
ldloc.1 loads the second local variable. ☺ Also, in your example if translates to beq, not ceq.Purse
ldloc.0 usually is the local class. you are right with beq of course.Inshore

© 2022 - 2024 — McMap. All rights reserved.