Question

I have compiled some test code in debug mode and reflected the resulting assembly with ILSpy. This is the IL I am getting:

.class private auto ansi beforefieldinit ArrayListBoxAndUnBox.Program
extends [mscorlib]System.Object
   {
// Nested Types
.class nested public auto ansi beforefieldinit Point
    extends [mscorlib]System.Object
{
    // Fields
    .field public int32 x
    .field public int32 y

    // Methods
    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x209f
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: ret
    } // end of method Point::.ctor

} // end of class Point


// Methods
.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 59 (0x3b)
    .maxstack 2
    .entrypoint
    .locals init (
        [0] class [mscorlib]System.Collections.ArrayList list,
        [1] int32 i,
        [2] class ArrayListBoxAndUnBox.Program/Point p
    )

    IL_0000: nop
    IL_0001: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor()
    IL_0006: stloc.0
    IL_0007: ldloc.0
    IL_0008: ldc.i4.1
    IL_0009: box [mscorlib]System.Int32
    IL_000e: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
    IL_0013: pop
    IL_0014: ldloc.0
    IL_0015: ldc.i4.0
    IL_0016: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
    IL_001b: unbox.any [mscorlib]System.Int32
    IL_0020: stloc.1
    IL_0021: ldloc.0
    IL_0022: newobj instance void ArrayListBoxAndUnBox.Program/Point::.ctor()
    IL_0027: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
    IL_002c: pop
    IL_002d: ldloc.0
    IL_002e: ldc.i4.1
    IL_002f: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
    IL_0034: castclass ArrayListBoxAndUnBox.Program/Point
    IL_0039: stloc.2
    IL_003a: ret
} // end of method Program::Main

.method public hidebysig specialname rtspecialname 
    instance void .ctor () cil managed 
{
    // Method begins at RVA 0x2097
    // Code size 7 (0x7)
    .maxstack 8

    IL_0000: ldarg.0
    IL_0001: call instance void [mscorlib]System.Object::.ctor()
    IL_0006: ret
} // end of method Program::.ctor

} // end of class ArrayListBoxAndUnBox.Program

I see the .maxstack is set to two, but when I go through the code by hand I sometimes get 3 elements on the evaluation stack. Why? Am I a bad IL-stack-counter, or is there a meaning behind this?

In my world the first ldloc.0 at IL_0014 is never removed, but maybe it is just me that is not getting how callvirt to get_Item works. When I reach to IL_0014, I have to references to list on my paper of the stack and after that at IL_0015 the program violates the .maxstack when 0 is pushed onto the evalution stack.

I am newbie, so there must be something that is not right. Maybe my counting is right, and my understanding of .maxstack is wrong, or maybe it is the other way around. Could someone please tell me if my understanding of .maxstack is wrong, that .maxstack shows the maximum elements on stack at runtime. Or if my counting is wrong? Does Add remove the reference to the list instance?

EDIT: Here is how I count. The count is after the IL code has ran:

IL_0000: 0 on stack
IL_0001: 1 on stack (the reference to array list)
IL_0006: 0 on stack
IL_0007: 1 on stack (the reference to arraylist)
IL_0008: 2 on stack (reference to arraylist and int 1)
IL_0009: 2 on stack (refernce to arraylist and reference to object that wrap 1)
IL_000e: 2 on stack (reference to arraylist and index to the added boxed int object)
IL_0013: 1 on stack (reference to arraylist)
IL_0014: 2 on stack (reference to arraylist and reference to arraylist)
IL_0015: 3 on stack (reference to arraylist and reference to arraylist and int 0)

I have not counted the rest, because I know I am getting something wrong.

Thanks

Was it helpful?

Solution

Hard to see where you went wrong. Stack depth annotated on the left, after the instruction executed:

0    IL_0000: nop
1    IL_0001: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor()
0    IL_0006: stloc.0
1    IL_0007: ldloc.0
2    IL_0008: ldc.i4.1
2    IL_0009: box [mscorlib]System.Int32
1    IL_000e: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
0    IL_0013: pop
1    IL_0014: ldloc.0
2    IL_0015: ldc.i4.0
1    IL_0016: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
1    IL_001b: unbox.any [mscorlib]System.Int32
0    IL_0020: stloc.1
1    IL_0021: ldloc.0
2    IL_0022: newobj instance void ArrayListBoxAndUnBox.Program/Point::.ctor()
1    IL_0027: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
0    IL_002c: pop
1    IL_002d: ldloc.0
2    IL_002e: ldc.i4.1
1    IL_002f: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
1    IL_0034: castclass ArrayListBoxAndUnBox.Program/Point
0    IL_0039: stloc.2
0    IL_003a: ret

The only non-trivial ones are the ArrayList.Add() calls. It pops two stack values, the ArrayList object reference and the Add() argument. And pushes one back, the return value of Add().

After edit: which is indeed where you went wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top