It seems to me it would work right if the instruction at IL_0029 was removed. Of course I can't change the generated code manually to fix the bug since the real bug is obviously in my compiler, but I can't figure out what it is supposedly doing wrong.

EDIT: OK I found that if I change the C# code to this:

        public unsafe VirtualString(char* pStr)
            : base()
        {
            m_buffer = pStr;
            m_length = 0;

            if (m_buffer == null)
                return;

            while (*pStr != 0)
            {
                m_length++;
                pStr++;
            }
        }

I get the right results in the C++ version. This indicates that the problem is in how I handle "*pStr++".

EDIT EDIT:I wonder if it has something to do the the "dup" instructions. The way I handle dup is to clone the value of the item on the top of the "virtual" stack and push it so that there are two. Also, the way this virtual stack works is that it contains text that represents the local variable, parameter name or a previous operation. So in the case of the dup at IL_003b, the text on the stack will be "pStr". So when I perform the dup operation, the two top items on the stack now contain "pStr". I wonder whether I need to create a completely new local variable when I come across a dup operation.