Inside .NET - The mystery of conv.ovf.<to type>.un…

I'm sure you've all read the ECMA-335 document; but for those of you who don't know, this is the document that specifies all the gory details of how .NET works.

When you got to Partition III, page 62 in your bedtime reading I expect you read it, thought nothing very much of it, but then went Hmmm? That's a little odd. Why is it needed exactly?

Because it's a little strange, and I just can't figure out why it's really needed.

Of course, this could be because I'm being a bit slow. If so, please just leave a comment explaining what it's about to put me out of my confusion - I'm not losing sleep over it, but it's close...

The problem is all about conversion instructions. When you write something like this:

int i = ...
byte b = (byte)i;

Then the CIL instruction generated for line 2 will be conv.u1, which tells the CIL runtime to convert whatever is at the top of the stack into a u1, which is the same as the C# byte type.

Simple.

Now, if you had this C#:

checked {
  int i = ...
  byte b = (byte)i;
}

Then the CIL instruction generated for line 3 will be conv.ovf.u1, which tells the CIL runtime to do the same conversion as last time, but do an overflow check as well, so if i doesn't fit in to a byte then an OverflowException will be thrown.

Still simple.

And, futhermore, if you have this in C#:

checked {
  uint i = ...
  byte b = (byte)i;
}

Then the CIL for line 3 will be conv.ovf.u1.un, which tells the CIL runtime to do the another checked conversion, but this time it is told that the source type is unsigned (the .un part of the CIL instruction).

Which still looks well, good, obvious and still simple.

But the thing is - it just isn't needed.

As part of the JIT, the runtime has to do a full stack analysis, and as part of that stack analysis the runtime will already know exactly what type is in the source stack entry. Therefore it can figure out whether it's a signed or unsigned int without any help from the CIL instruction.

And because there is a very small range of types that are allowed to be used as the operand to these conversion instructions, and the range of supported instructions is fixed (i.e. no extensibility is allowed), I cannot think of any situation where theses conv.ovf.<to type>.un instructions would actually be needed. The runtime could always just use the conv.ovf.<to type> instruction and figure out if it's signed or unsigned from the stack analysis.

3 Responses to “Inside .NET - The mystery of conv.ovf.<to type>.un…”

  1. Achille says:

    Hi, your project looks nice.

    Regarding your question, note that the only integer types the CLI operates on are int32, int64, native int (III.1.1). I guess this helps reduce the number of possibilities in the subsequent tables (1.5).

  2. LEWIS says:


    CheapTabletsOnline.com. Canadian Health&Care.Best quality drugs.No prescription online pharmacy.Special Internet Prices. No prescription drugs. Order pills online

    Buy:Aricept.Acomplia.Lasix.Prozac.Advair.Lipothin.Female Pink Viagra.Zetia.Buspar.Female Cialis.Ventolin.Zocor.SleepWell.Lipitor.Wellbutrin SR.Amoxicillin.Benicar.Seroquel.Cozaar.Nymphomax….

  3. ORLANDO says:


    CheapTabletsOnline.Com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Low price drugs. Buy pills online

    Buy:Benicar.Wellbutrin SR.Prozac.Zetia.Seroquel.Cozaar.Lipothin.Female Cialis.Zocor.Ventolin.Buspar.Acomplia.Amoxicillin.Lasix.SleepWell.Lipitor.Aricept.Advair.Female Pink Viagra.Nymphomax….

Leave a Reply