Understanding IL generated of the following snippet

81 Views Asked by At

Hello i am trying to use Reflection.Emit to generate a method dynamically and i do not know how to interpret the following snippet:

C# (What i want to generate)

 public Task<Cell> GetAsync(string key)
{
    IEnumerable<Component<Cell>> async = Core.ComponentEnumerator.Instance.GetAsync(key);
    Command<Cell> command = new Command<Cell>(async);
    return this.ProcessAsync<Cell>(command);
}

IL generated in release mode

method public hidebysig newslot virtual final instance class [System.Runtime]System.Threading.Tasks.Task`1<valuetype redius.Cell> GetAsync(string key) cil managed
{
    .maxstack 2
    .locals init (
        [0] class [System.Runtime]System.Collections.Generic.IEnumerable`1<valuetype redius.Component`1<valuetype redius.Cell>> enumerable,
        [1] valuetype redius.Command`1<valuetype redius.Cell> command)
    L_0000: call class redius.Core/ComponentEnumerator redius.Core/ComponentEnumerator::get_Instance()
    L_0005: ldarg.1 
    L_0006: callvirt instance class [System.Runtime]System.Collections.Generic.IEnumerable`1<valuetype redius.Component`1<valuetype redius.Cell>> redius.Core/ComponentEnumerator::GetAsync(string)
    L_000b: stloc.0 
    L_000c: ldloca.s command
    L_000e: ldloc.0 
    L_000f: call instance void redius.Command`1<valuetype redius.Cell>::.ctor(class [System.Runtime]System.Collections.Generic.IEnumerable`1<valuetype redius.Component`1<!0>>)
    L_0014: ldarg.0 
    L_0015: ldloc.1 
    L_0016: call instance class [System.Runtime]System.Threading.Tasks.Task`1<!!0> redius.OpsGenTemplate::ProcessAsync<valuetype redius.Cell>(valuetype redius.Command`1<!!0>)
    L_001b: ret 
}

Can someone please explain to me the following points:

1 The first instruction L_0000 calls a singleton object but does not store its value anywhere.Why does it not use ldarg.0 and then forward this value into the Command<Cell> constructor?.
2 It then calls ldarg.1...when in our method we have only 1 argument of the method.

I was expecting something along the lines:

1.Load the argument 0 - ldarg.0
2.stloc it in async variable
3. call constructor of Command<Cell> that consumes the above line
4.stloc it in command
5. call ProcessAsync<Cell>
6.ret

0

There are 0 best solutions below