I'm trying to interpret a CIL instruction by instruction. I'm using Mono.Reflection to get a list of instructions and I don't know how to interpret 'ldarga.s'.
I know "ldarga" - Load the argument address to the evaluation stack." but what is the argument / index of 'ldarga.s', i.e. load / push on stack? - the instruction is under IL_0004 the following list of assemblers.
another instruction "ldarg.0" (without "a") argumet load / push "first" on the stack, "ldarg.1" argumet load / push secound on the stack In another example, "ldloca.s" - "Loads the address of the local variable at the specified index into the evaluation stack.") index is in the instruction operand.
Here is my code I have function "Funkcja1" in class Test
public int Funkcja1(int p1, string p2, ProcesTest p3)
{
var zmienalokaln1 = p1;
var zmiennaLokaln2 = p2 + p1.ToString();
Metoda1();
var zmienna3 = zmienalokaln1 + zmiennaLokaln2;
return zmienna + p1 + p2.Length + zmienna3.Length;
}
Function assembler (generated by JetBrains dotPeek):
.method public hidebysig instance int32
Funkcja1(
int32 p1,
string p2,
class Cvl.VirtualMachine.Test.ProcesTest p3
) cil managed
{
.maxstack 2
.locals init (
[0] int32 zmienalokaln1,
[1] string zmiennaLokaln2,
[2] string zmienna3,
[3] int32 V_3
)
// [18 9 - 18 10]
IL_0000: nop
// [19 13 - 19 36]
IL_0001: ldarg.1 // p1
IL_0002: stloc.0 // zmienalokaln1
// [20 13 - 20 53]
IL_0003: ldarg.2 // p2
IL_0004: ldarga.s p1
IL_0006: call instance string [System.Runtime]System.Int32::ToString()
IL_000b: call string [System.Runtime]System.String::Concat(string, string)
IL_0010: stloc.1 // zmiennaLokaln2
// [22 13 - 22 23]
IL_0011: ldarg.0 // this
IL_0012: call instance void Cvl.VirtualMachine.Test.ProcesTest::Metoda1()
IL_0017: nop
// [24 13 - 24 59]
IL_0018: ldloca.s zmienalokaln1
IL_001a: call instance string [System.Runtime]System.Int32::ToString()
IL_001f: ldloc.1 // zmiennaLokaln2
IL_0020: call string [System.Runtime]System.String::Concat(string, string)
IL_0025: stloc.2 // zmienna3
// [26 13 - 26 63]
IL_0026: ldarg.0 // this
IL_0027: ldfld int32 Cvl.VirtualMachine.Test.ProcesTest::zmienna
IL_002c: ldarg.1 // p1
IL_002d: add
IL_002e: ldarg.2 // p2
IL_002f: callvirt instance int32 [System.Runtime]System.String::get_Length()
IL_0034: add
IL_0035: ldloc.2 // zmienna3
IL_0036: callvirt instance int32 [System.Runtime]System.String::get_Length()
IL_003b: add
IL_003c: stloc.3 // V_3
IL_003d: br.s IL_003f
// [27 9 - 27 10]
IL_003f: ldloc.3 // V_3
IL_0040: ret
} // end of method ProcesTest::Funkcja1
From Mono.Reflection.Disassembler.GetInstructions(this MethodBase self)
I need to somehow specify the index of this parameter ('p1') in the function parameter list, which in this case is equal to 0 (first parameter)
Operand in ldarga.s can by System.Reflection.ParameterInfo.
and index = parameterInfo.Position;
if (instruction.Operand is System.Reflection.ParameterInfo parameterInfo)
{
Index = parameterInfo.Position;
}