我在调试模式下编译了一些测试代码,并用ILSpy反映了生成的程序集。这是我收到的IL:为我解释maxstack指令
.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
我看到.maxstack被设置为两个,但是当我通过手工代码我有时会计算堆栈上3个元素。为什么?我是一个糟糕的IL-stack-counter,还是这背后有意义?
在我的世界中,IL_0014上的第一个ldloc.0永远不会被删除,但也许只是我没有得到callvirt如何get_Item的作品。当我到达IL_0014时,我必须参考堆栈文件中的列表,然后在IL_0015上,当0被推入评估堆栈时,程序违反了.maxstack。
我是新手,所以肯定有些东西是不对的。也许我的计数是正确的,我对.maxstack的理解是错误的,或者也许是相反的。有人能告诉我,如果我对.maxstack的理解是错误的,那么.maxstack会在运行时显示栈上的最大元素。或者如果我的计数错误?添加是否删除对列表实例的引用?编辑: 这里是我如何计数。计数是在IL代码跑完之后:
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)
我还没有计算其余的,因为我知道我得到了错误的东西。
谢谢
目前尚不清楚,你怎么计算,这使得它很难看到你可能做错了。你是否考虑过'callvirt'从栈中弹出目标和参数?参见ECMA-335的第III.4.2节。 –
对不起。我已经更新了我的答案和我的数据。我没有阅读ECMA。我正在实实在在地寻找那个。我在我的IL里有点生疏。谢谢!也许我没有得到那个callvirt弹出的参考呢? – mslot
'0e'弹出两个ArrayList的参考和数量,然后按下一个'Int32' - 这就是你在哪里错了,你没有注意到ArrayList中提及弹出。 –