zoukankan      html  css  js  c++  java
  • Why does the C# compiler translate this != comparison as if it were a > comparison?

    Question:

    I have by pure chance discovered that the C# compiler turns this method:

    static bool IsNotNull(object obj)
    {
        return obj != null;
    }

    …into this IL:

    .method private hidebysig static bool IsNotNull(object obj) cil managed
    {
        ldarg.0   // obj
        ldnull
        cgt.un
        ret
    }

    …or, if you prefer looking at decompiled C# code:

    static bool IsNotNull(object obj)
    {
        return obj > null;   // (note: this is not a valid C# expression)
    }

    How come that the != gets translated as a ">"?


    Answer:

    Short answer:

    There is no "compare-not-equal" instruction in IL, so the C# != operator has no exact correspondence and cannot be translated literally.

    There is however a "compare-equal" instruction (ceq, a direct correspondence to the == operator), so in the general case, x != y gets translated like its slightly longer equivalent (x == y) == false.

    There is also a "compare-greater-than" instruction in IL (cgt) which allows the compiler to take certain shortcuts (i.e. generate shorter IL code), one being that inequality comparisons of objects against null, obj != null, get translated as if they were "obj > null".


    Let's go into some more detail.

    If there is no "compare-not-equal" instruction in IL, then how will the following method get translated by the compiler?

    static bool IsNotEqual(int x, int y)
    {
        return x != y;
    }

    As already said above, the compiler will turn the x != y into (x == y) == false:

    .method private hidebysig static bool IsNotEqual(int32 x, int32 y) cil managed 
    {
        ldarg.0   // x
        ldarg.1   // y
        ceq
        ldc.i4.0  // false
        ceq       // (note: two comparisons in total)
        ret
    }

    It turns out that the compiler does not always produce this fairly long-winded pattern. Let's see what happens when we replace y with the constant 0:

    static bool IsNotZero(int x)
    {
        return x != 0;
    }

    The IL produced is somewhat shorter than in the general case:

    .method private hidebysig static bool IsNotZero(int32 x) cil managed 
    {
        ldarg.0    // x
        ldc.i4.0   // 0
        cgt.un     // (note: just one comparison)
        ret
    }

    The compiler can take advantage of the fact that signed integers are stored in two's complement(where, if the resulting bit patterns are interpreted as unsigned integers — that's what the .un means — 0 has the smallest possible value), so it translates x == 0 as if it were unchecked((uint)x) > 0.

    It turns out the compiler can do just the same for inequality checks against null:

    static bool IsNotNull(object obj)
    {
        return obj != null;
    }

    The compiler produces almost the same IL as for IsNotZero:

    .method private hidebysig static bool IsNotNull(object obj) cil managed 
    {
        ldarg.0
        ldnull   // (note: this is the only difference)
        cgt.un
        ret
    }

    Apparently, the compiler is allowed to assume that the bit pattern of the null reference is the smallest bit pattern possible for any object reference.

    This shortcut is explicitly mentioned in the Common Language Infrastructure Annotated Standard (1st edition from Oct 2003) (on page 491, as a footnote of Table 6-4, "Binary Comparisons or Branch Operations"):

    "cgt.un is allowed and verifiable on ObjectRefs (O). This is commonly used when comparing an ObjectRef with null (there is no "compare-not-equal" instruction, which would otherwise be a more obvious solution)."



    想要看到更多学习笔记、考试复习资料、面试准备资料? 想要看到IBM工作时期的技术积累和国外初创公司的经验总结? 敬请关注: [CSDN](https://blog.csdn.net/u013152895) [简书](https://www.jianshu.com/u/594a3de3852d) [博客园](https://www.cnblogs.com/vigorz/) [51Testing](http://www.51testing.com/?15263728)
  • 相关阅读:
    改进ls的实现(课下作业)
    stat命令的实现-mysate
    (选做)实现mypwd
    2019-2020-1 20175209 20175213 20175214 实验五 通讯协议设计
    2019-2020-1 20175209 20175213 20175214 实验四 外设驱动程序设计
    2019-2020-1 20175209 20175213 20175214 实验三 并发程序
    2019-2020-1 20175209 20175213 20175214 实验三 并发程序
    2019-2020-1 20175209 20175213 20175214 实验一 开发环境的熟悉
    2018-2019-2 20175213实验五 《网络编程与安全》实验报告
    2018-2019-2 20175213实验四 《Android开发基础》实验报告
  • 原文地址:https://www.cnblogs.com/vigorz/p/10499219.html
Copyright © 2011-2022 走看看