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?
To me it looks like you’re looking at IL generated while compiling for debug mode, the
stloc.1andldloc.1are 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):