G
Guest
Hi,
I'm following a tutorial and they translate this C#:
object[] a = {1, "Hello", 123};
to this IL:
..entrypoint
..locals (class System.Object[] V_0,class System.Object V_1,class
System.Object[] V_2,int32 V_3)
ldc.i4.3
newarr [mscorlib]System.Object
stloc.2
ldloc.2
ldc.i4.0
ldc.i4.1
stloc.3
ldloca.s V_3
box [mscorlib]System.Int32
stelem.ref
ldloc.2
ldc.i4.1
ldstr "Hello"
stelem.ref
ldloc.2
ldc.i4.2
ldc.i4.s 123
stloc.3
ldloca.s V_3
box [mscorlib]System.Int32
stelem.ref
But when I compile similar code, I get a big difference. Instead of putting
the integers in a local variable, they are stored directly and boxed via:
IL_0006: stloc.2
IL_0007: ldloc.2
IL_0008: ldc.i4.0
IL_0009: ldc.i4.1
IL_000a: box [mscorlib]System.Int32
IL_000f: stelem.ref
IL_0010: ldloc.2
IL_0011: ldc.i4.1
IL_0012: ldstr "Hello"
IL_0017: stelem.ref
IL_0018: ldloc.2
IL_0019: ldc.i4.2
IL_001a: ldc.i4.s 123
IL_001c: box [mscorlib]System.Int32
IL_0021: stelem.ref
The big difference is that the number is pushed directly on the stack and
boxed, rather than the number going into a local variable and the address
being pushed and then boxed.
So 2 questions:
1) Why would there be a difference? I tried both 1.1 and 2.0 compilers, but
can't seem to get the ldloca.s version?
2) The more interesting question is how does the box instruction work on a
"transient pointer?" Here's the interesting segment:
ldloc.2 <- push address of array
ldc.i4.0 <- push index 0
ldc.i4.1
stloc.3 <- store number one in local integer variable V_3
ldloca.s V_3 <- Put address of local variable on stack
box [mscorlib]System.Int32 <- I guess Box will be able to work with
the address??? and make a box based on the dereferenced address?
stelem.ref
For those curious, the tutorial I'm following is:
http://www.vijaymukhi.com/documents/books/ilbook/chap12.htm
And do a search for the text "It is in stelem.ref and we are not privy to
this code". (It's the following example, after this text)
Thanks all...
-Ben
I'm following a tutorial and they translate this C#:
object[] a = {1, "Hello", 123};
to this IL:
..entrypoint
..locals (class System.Object[] V_0,class System.Object V_1,class
System.Object[] V_2,int32 V_3)
ldc.i4.3
newarr [mscorlib]System.Object
stloc.2
ldloc.2
ldc.i4.0
ldc.i4.1
stloc.3
ldloca.s V_3
box [mscorlib]System.Int32
stelem.ref
ldloc.2
ldc.i4.1
ldstr "Hello"
stelem.ref
ldloc.2
ldc.i4.2
ldc.i4.s 123
stloc.3
ldloca.s V_3
box [mscorlib]System.Int32
stelem.ref
But when I compile similar code, I get a big difference. Instead of putting
the integers in a local variable, they are stored directly and boxed via:
IL_0006: stloc.2
IL_0007: ldloc.2
IL_0008: ldc.i4.0
IL_0009: ldc.i4.1
IL_000a: box [mscorlib]System.Int32
IL_000f: stelem.ref
IL_0010: ldloc.2
IL_0011: ldc.i4.1
IL_0012: ldstr "Hello"
IL_0017: stelem.ref
IL_0018: ldloc.2
IL_0019: ldc.i4.2
IL_001a: ldc.i4.s 123
IL_001c: box [mscorlib]System.Int32
IL_0021: stelem.ref
The big difference is that the number is pushed directly on the stack and
boxed, rather than the number going into a local variable and the address
being pushed and then boxed.
So 2 questions:
1) Why would there be a difference? I tried both 1.1 and 2.0 compilers, but
can't seem to get the ldloca.s version?
2) The more interesting question is how does the box instruction work on a
"transient pointer?" Here's the interesting segment:
ldloc.2 <- push address of array
ldc.i4.0 <- push index 0
ldc.i4.1
stloc.3 <- store number one in local integer variable V_3
ldloca.s V_3 <- Put address of local variable on stack
box [mscorlib]System.Int32 <- I guess Box will be able to work with
the address??? and make a box based on the dereferenced address?
stelem.ref
For those curious, the tutorial I'm following is:
http://www.vijaymukhi.com/documents/books/ilbook/chap12.htm
And do a search for the text "It is in stelem.ref and we are not privy to
this code". (It's the following example, after this text)
Thanks all...
-Ben