www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18001] New: Wrong code on signed 32-bit compare of INT_MIN

https://issues.dlang.org/show_bug.cgi?id=18001

          Issue ID: 18001
           Summary: Wrong code on signed 32-bit compare of INT_MIN with
                    zero
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: yebblies gmail.com

The following code prints "result: 1 -2147483648"

extern(C) int printf(const char* format, ...);

uint get()
{
    return 0x80000000;
}

void test(bool b, int v)
{
    printf("result: %d %d\n", b, v);
}

void main()
{
    test(cast(int)get() > 0, get());
}

This happens because the codegen for the compare is using the identity (a > 0)
=== (-a < 0) which doesn't hold for INT_MIN.

0000000000000000 <_Dmain>:
   0:   55                      push   %rbp
   1:   48 8b ec                mov    %rsp,%rbp
   4:   48 83 ec 10             sub    $0x10,%rsp
   8:   e8 00 00 00 00          callq  d <_Dmain+0xd>
                        9: R_X86_64_PC32        _D5testx3getFZk-0x4
   d:   48 89 c6                mov    %rax,%rsi
  10:   f7 de                   neg    %esi
  12:   c1 ee 1f                shr    $0x1f,%esi
  15:   48 89 75 f8             mov    %rsi,-0x8(%rbp)
  19:   e8 00 00 00 00          callq  1e <_Dmain+0x1e>
                        1a: R_X86_64_PC32       _D5testx3getFZk-0x4
  1e:   48 89 c7                mov    %rax,%rdi
  21:   48 8b 75 f8             mov    -0x8(%rbp),%rsi
  25:   e8 00 00 00 00          callq  2a <_Dmain+0x2a>
                        26: R_X86_64_PC32       _D5testx4testFbiZv-0x4
  2a:   31 c0                   xor    %eax,%eax
  2c:   c9                      leaveq
  2d:   c3                      retq
        ...

--
Nov 21 2017