www.digitalmars.com         C & C++   DMDScript  

D - DMD optimizer ( -O) bug

reply "dickl" <dick221z yahoo.com> writes:
When compiling without the -O option, the compiler generates the following
code (correctly)
for the functions HIWORD & LOWORD.
--------------------------------------------
WORD HIWORD(int l)
0040902C  enter       4,0
00409030  mov         dword ptr [ebp-4],eax
{
    return (WORD)((l >> 16) & 0xFFFF);
    00409033  mov         eax,dword ptr [l]
    00409036  sar           eax,10h
    00409039  mov         ecx,0FFFFh
    0040903E  and          eax,ecx
}
00409040  leave
00409041  ret
00409042  int         3
00409043  int         3


WORD LOWORD(int l)
00409044  enter       4,0
00409048  mov         dword ptr [ebp-4],eax
{
    return (WORD)l;
    0040904B  mov         ax,word ptr [l]
}

With the -O option used, it does some strange stuff
-----------------------------------------------------
WORD HIWORD(int l)
00408F98  ret
00408F99  int         3
00408F9A  int         3
00408F9B  int         3
{
    return (WORD)((l >> 16) & 0xFFFF);
}

WORD LOWORD(int l)
00408F9C  ret
00408F9D  int         3
00408F9E  int         3
00408F9F  int         3
{
    return (WORD)l;
}
Dec 11 2003
parent reply "Walter" <walter digitalmars.com> writes:
I get, optimized -O -release on std\c\windows\windows.d:

_HIWORD 4       comdat
        assume  CS:_HIWORD 4
                mov     AX,6[ESP]
                ret     4
_HIWORD 4       ends
_LOWORD 4       comdat
        assume  CS:_LOWORD 4
                mov     AX,4[ESP]
                ret     4
_LOWORD 4       ends

which looks correct.
Dec 11 2003
parent reply "Y.Tomino" <demoonlit inter7.jp> writes:
It seems relying on extern(xx).

YT

-------------------------------------
//dmd -O test.d
alias int ULONG;
alias ushort WORD;

WORD HIWORD(ULONG l)
{
    return (WORD)((l >> 16) & 0xFFFF);
}

extern(Windows) WORD HIWORD_w(ULONG l)
{
    return (WORD)((l >> 16) & 0xFFFF);
}

int main()
{
 printf("%x\n", HIWORD(0x12345678)); //5678
 printf("%x\n", HIWORD_w(0x12345678)); //1234
 return 0;
}
--------------------------------

"Walter" <walter digitalmars.com> wrote in message
news:braobo$2t83$1 digitaldaemon.com...
 I get, optimized -O -release on std\c\windows\windows.d:

 _HIWORD 4       comdat
         assume  CS:_HIWORD 4
                 mov     AX,6[ESP]
                 ret     4
 _HIWORD 4       ends
 _LOWORD 4       comdat
         assume  CS:_LOWORD 4
                 mov     AX,4[ESP]
                 ret     4
 _LOWORD 4       ends

 which looks correct.
Dec 11 2003
parent reply "Walter" <walter digitalmars.com> writes:
I'm not sure what you mean here. Is it working in your tests or not?

"Y.Tomino" <demoonlit inter7.jp> wrote in message
news:brardl$90$1 digitaldaemon.com...
 It seems relying on extern(xx).

 YT

 -------------------------------------
 //dmd -O test.d
 alias int ULONG;
 alias ushort WORD;

 WORD HIWORD(ULONG l)
 {
     return (WORD)((l >> 16) & 0xFFFF);
 }

 extern(Windows) WORD HIWORD_w(ULONG l)
 {
     return (WORD)((l >> 16) & 0xFFFF);
 }

 int main()
 {
  printf("%x\n", HIWORD(0x12345678)); //5678
  printf("%x\n", HIWORD_w(0x12345678)); //1234
  return 0;
 }
 --------------------------------

 "Walter" <walter digitalmars.com> wrote in message
 news:braobo$2t83$1 digitaldaemon.com...
 I get, optimized -O -release on std\c\windows\windows.d:

 _HIWORD 4       comdat
         assume  CS:_HIWORD 4
                 mov     AX,6[ESP]
                 ret     4
 _HIWORD 4       ends
 _LOWORD 4       comdat
         assume  CS:_LOWORD 4
                 mov     AX,4[ESP]
                 ret     4
 _LOWORD 4       ends

 which looks correct.
Dec 11 2003
parent reply "Y.Tomino" <demoonlit inter7.jp> writes:
HIWORD of std.c.windows.windows.d is compiled with extern(Windows).
Please test HIWORD for every "LinkageAttribute"  with -O.

HIWORD and HIWORD_w in my test are the same logic.
But These return different values.

  printf("%x\n", HIWORD(0x12345678)); //5678
The result is 0x5678. incorrect
  printf("%x\n", HIWORD_w(0x12345678)); //1234
The result is 0x1234. correct extern(Windows) HIWORD is correct. extern(D) HIWORD is incorrect. (dickl probably wrote this case) Thanks. YT "Walter" <walter digitalmars.com> wrote in message news:brbcro$opk$1 digitaldaemon.com...
 I'm not sure what you mean here. Is it working in your tests or not?

 "Y.Tomino" <demoonlit inter7.jp> wrote in message
 news:brardl$90$1 digitaldaemon.com...
 It seems relying on extern(xx).

 YT

 -------------------------------------
 //dmd -O test.d
 alias int ULONG;
 alias ushort WORD;

 WORD HIWORD(ULONG l)
 {
     return (WORD)((l >> 16) & 0xFFFF);
 }

 extern(Windows) WORD HIWORD_w(ULONG l)
 {
     return (WORD)((l >> 16) & 0xFFFF);
 }

 int main()
 {
  printf("%x\n", HIWORD(0x12345678)); //5678
  printf("%x\n", HIWORD_w(0x12345678)); //1234
  return 0;
 }
 --------------------------------

 "Walter" <walter digitalmars.com> wrote in message
 news:braobo$2t83$1 digitaldaemon.com...
 I get, optimized -O -release on std\c\windows\windows.d:

 _HIWORD 4       comdat
         assume  CS:_HIWORD 4
                 mov     AX,6[ESP]
                 ret     4
 _HIWORD 4       ends
 _LOWORD 4       comdat
         assume  CS:_LOWORD 4
                 mov     AX,4[ESP]
                 ret     4
 _LOWORD 4       ends

 which looks correct.
Dec 12 2003
parent "dickl" <dick221z yahoo.com> writes:
It was compiled as extern(D)

----------------------------------
"Y.Tomino" <demoonlit inter7.jp> wrote in message
news:brc86e$2145$1 digitaldaemon.com...
 HIWORD of std.c.windows.windows.d is compiled with extern(Windows).
 Please test HIWORD for every "LinkageAttribute"  with -O.

 HIWORD and HIWORD_w in my test are the same logic.
 But These return different values.

  printf("%x\n", HIWORD(0x12345678)); //5678
The result is 0x5678. incorrect
  printf("%x\n", HIWORD_w(0x12345678)); //1234
The result is 0x1234. correct extern(Windows) HIWORD is correct. extern(D) HIWORD is incorrect. (dickl probably wrote this case) Thanks. YT "Walter" <walter digitalmars.com> wrote in message news:brbcro$opk$1 digitaldaemon.com...
 I'm not sure what you mean here. Is it working in your tests or not?

 "Y.Tomino" <demoonlit inter7.jp> wrote in message
 news:brardl$90$1 digitaldaemon.com...
 It seems relying on extern(xx).

 YT

 -------------------------------------
 //dmd -O test.d
 alias int ULONG;
 alias ushort WORD;

 WORD HIWORD(ULONG l)
 {
     return (WORD)((l >> 16) & 0xFFFF);
 }

 extern(Windows) WORD HIWORD_w(ULONG l)
 {
     return (WORD)((l >> 16) & 0xFFFF);
 }

 int main()
 {
  printf("%x\n", HIWORD(0x12345678)); //5678
  printf("%x\n", HIWORD_w(0x12345678)); //1234
  return 0;
 }
 --------------------------------

 "Walter" <walter digitalmars.com> wrote in message
 news:braobo$2t83$1 digitaldaemon.com...
 I get, optimized -O -release on std\c\windows\windows.d:

 _HIWORD 4       comdat
         assume  CS:_HIWORD 4
                 mov     AX,6[ESP]
                 ret     4
 _HIWORD 4       ends
 _LOWORD 4       comdat
         assume  CS:_LOWORD 4
                 mov     AX,4[ESP]
                 ret     4
 _LOWORD 4       ends

 which looks correct.
Dec 12 2003