Hi all, I moved this to
microsoft.public.dotnet.framework.clr
as I thought it fit better there...
-Ben
> Hi,
>
[quoted text clipped - 45 lines]
>
> -Ben
> http://msdn2.microsoft.com/en-us/library/system.reflection.emit.opcodes.unbox.aspx
> Now a few things: It mentions an "object reference" and a "value type
> pointer."
An object, traditionally, looks somewhat like this:
<vtable pointer>
<object data>
<object data...>
The equivalent in the CLR is:
<MethodTable pointer>
<object data>
<object data...>
So, if you have a boxed int, you've got this:
<System.Int32 MethodTable pointer>
<int32 m_value>
(You can view this 'm_value' field if you use reflection to look into a
boxed Int32.)
So, to 'unbox' a boxed instance, all the CLR needs to do is increment
the pointer. A boxed instance looks like this (including the pointer):
-> <System.Int32 MethodTable pointer>
<int32 m_value>
... to:
<System.Int32 MethodTable pointer>
-> <int32 m_value>
et voilà - a managed pointer to the value!
> What to type O and type & refer to?
Type O is an object type, and will always point to a fully-fledged
object on the heap. A managed pointer, denoted by '&', may point to an
arbitrary typed location. For example, 'ref' and 'out' parameters are
implemented using '&', in which case the pointers might point to locals
on the stack.
> Another question:
> "unbox is not required to copy the value type from the object. Typically it
> simply computes the address of the value type that is already present inside
> of the boxed object."
Hopefully this is clear from the adjustment above - typically, the CLR
simply needs to add IntPtr.Size to the pointer (4 on 32-bit, 8 on
64-bit).
> It seems then that unbox which is supposed to return a value type is simply
> returning a pointer to what is inside the box.
It loads a managed pointer onto the stack. You can then use ldobj to
dereference the managed pointer and convert it into a fully-fledged
value type on the stack. There is an instruction on 2.0, unbox.any,
which combines the two steps, and is pretty essential for generics to
work on both value types and integers.
> I thought value types were
> very simple: they contain only the actual data you're using. If you push the
> integer 6 on the stack, you don't deal with addresses at all, you simply put
> a 6 on the stack. right?
Yes.
> so why is this unbox instruction able to avoid
> copying from the box and just return a pointer to what's in the box?
You may be calling an instance method on the value type. In that case,
argument 0 - i.e. the 'this' argument - needs to be a pointer.
Also, I hasten to point out that the IL instructions are simply a
description of required semantics. I think it's wise not to think of the
CLI's abstract machine as an actual CPU with x cost for this instruction
and y cost for that instruction, but rather as input to a compiler and
optimizer which can figure things out properly. When considering how to
generate code, look at the output of the C# or C++/CLI compiler for an
example expression / statement. The C++/CLI compiler in particular seems
to sometimes generate slightly faster IL.
-- Barry

Signature
http://barrkel.blogspot.com/
Ben R. - 27 Jul 2006 03:32 GMT
Hi Barry,
Thanks, this info is really useful. Your thoroughness is much appreciated.
You've inspired me to read more about methodtables.
There was one thing you mentioned, though, that I have trouble understanding:
> > so why is this unbox instruction able to avoid
> > copying from the box and just return a pointer to what's in the box?
>
> You may be calling an instance method on the value type. In that case,
> argument 0 - i.e. the 'this' argument - needs to be a pointer.
Would an example of this be if you define a function within a struct? I'm
also not sure how this ties into the "this" pointer. Could you elaborate on
this?
Thanks...
-Ben
> > http://msdn2.microsoft.com/en-us/library/system.reflection.emit.opcodes.unbox.aspx
>
[quoted text clipped - 83 lines]
>
> -- Barry
Barry Kelly - 27 Jul 2006 09:54 GMT
> Thanks, this info is really useful. Your thoroughness is much appreciated.
> You've inspired me to read more about methodtables.
[quoted text clipped - 8 lines]
>
> Would an example of this be if you define a function within a struct?
Yes, if you define a method on the struct.
> I'm
> also not sure how this ties into the "this" pointer. Could you elaborate on
> this?
Consider this struct:
---8<---
using System;
class Program
{
struct S
{
int _value;
public S(int value) { _value = value; }
public void PrintValue() { Console.WriteLine(_value); }
}
static void Main()
{
object o = new S(42);
((S) o).PrintValue();
}
}
--->8---
When you compile this and decompile it, the relevant instructions in
Main() to do the call look like this:
---8<---
IL_000e: ldloc.0
IL_000f: unbox.any Program/S
IL_0014: stloc.1
IL_0015: ldloca.s V_1
IL_0017: call instance void Program/S::PrintValue()
--->8---
These could be rewritten like this:
---8<---
ldloc.0
unbox Program/S
call instance void Program/S::PrintValue()
--->8---
If you disassemble the above C# program and replace the given lines with
these new lines, and then reassemble, you'll find that it works just the
same.
Basically, when you call an instance method on something, whether it's
an object or a struct, it needs a 'this' pointer. In the case of value
types, the 'this' pointer is a managed pointer, and for classes it's an
object reference. The 'this' pointer is always the first (i.e. slot 0)
argument to the instance method. That means it needs to be loaded onto
the stack before any other arguments to the method.
-- Barry

Signature
http://barrkel.blogspot.com/
Ben R. - 27 Jul 2006 18:54 GMT
Thanks Barry,
After stairing for a bit, I think this makes sense to me.
PS: I've added your blog to my RSS feeds!
Best,
-Ben
> > Thanks, this info is really useful. Your thoroughness is much appreciated.
> > You've inspired me to read more about methodtables.
[quoted text clipped - 67 lines]
>
> -- Barry