Search code examples
c#cilreflection.emitil

Conditionals in CIL--how to get the next free location to store to?


I'm converting code to IL (CIL/MSIL, etc.) from C#. I'm stuck on the conditionals because I want to be able to store to the next available free location. Example:

var x = 0;
if(x > 20)
 x = 1;

Console.WriteLine(x);

If I convert this to what I believe is correct IL, I get:

IL_0001:  ldc.i4.0    
IL_0002:  stloc.0     
IL_0003:  ldloc.0     
IL_0004:  ldc.i4.s    14 
IL_0006:  cgt         
IL_0008:  ldc.i4.0    
IL_0009:  ceq         
IL_000B:  stloc.1     
IL_000C:  ldloc.1     
IL_000D:  brtrue.s    IL_0011
IL_000F:  ldc.i4.1    
IL_0010:  stloc.0     
IL_0011:  ldloc.0     
IL_0012:  call        System.Console.WriteLine

I believe this to be correct IL, but my example is very static. If you see in the IL code, it stores the result of ceq into loc.1

And thus is my issue--the compiler saw that loc.0 was already taken(the variable 'x') and used the next free location, which was 1. My goal is to do this dynamically where a given method could have N variable before the conditional.

So, finally, here is my question: How do I, from C#, emit an opcode to say "stloc.nextAvailable" and it's equivalent load?


Solution

  • To me it looks like you're looking at IL generated while compiling for debug mode, the stloc.1 and ldloc.1 are referencing a local that doesn't exist in your code but may be created to give the nice little tooltip while hovering over the greater than symbol while debugging.

    I would expect release mode generated IL to look more like this (if it were to not optimize the whole thing down to Console.WriteLine(0); in advance):

    //load the 4-byte integer 0 on to the stack
    IL_0001:    ldc.i4.0
    //set the value of local 0 (x) to the previous value on the stack (0)
    IL_0002:    stloc.0
    //load up x for the comparison
    IL_0003:    ldloc.0
    //load the 4-byte integer 0x14 (20) on to the stack
    IL_0004:    ldc.i4.s    14
    //check to see whether two elements back on the stack is greater than one element back on the stack (x > 20), push 1 one back on the stack in the case the first is greater, 0 otherwise
    IL_0005:    cgt
    //if 0x14 (20) was the greater (or equal) value, jump over the body of the if block
    IL_0006:    brfalse.s    IL_0009
    //load the 4-byte integer 1 (1) on to the stack
    IL_0007:    ldc.i4.1
    //set the value of local 0 (x) to the previous value on the stack (1)
    IL_0008:    stloc.0
    //load the value of local 0 (x) on to the stack
    IL_0009:    ldloc.0
    //call Console.WriteLine
    IL_000A:    call    System.Console.WriteLine