www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10634] New: Win64: wrong codegen with .int of small structs

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10634

           Summary: Win64: wrong codegen with .int of small structs
           Product: D
           Version: D1 & D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: r.sagitario gmx.de



PDT ---
This extract from datetime fires the assertion:

struct TimeOfDay
{
    ubyte h, m, s;
}

__gshared byte glob;

struct DateTime
{
    this(ubyte _d, ubyte _m, ubyte _y, TimeOfDay _tod = TimeOfDay.init)
    {
        d = _d;
        m = _m;
        y = _y;
        tod = _tod;
    }
    TimeOfDay tod;
    ubyte d, m, y;
}

void main()
{
    glob = 123;
    DateTime date1 = DateTime(0, 0, 0);
    DateTime date2;
    assert(date1 == date2);
}

Here is the disassembly:

_D4test8DateTime6__ctorMFNchhhS4test9TimeOfDayZS4test8DateTime:
  0000000000000000: 55                 push        rbp
  0000000000000001: 48 8B EC           mov         rbp,rsp
  0000000000000004: 8A 45 30           mov         al,byte ptr [rbp+30h]
  0000000000000007: 88 41 03           mov         byte ptr [rcx+3],al
  000000000000000A: 44 88 49 04        mov         byte ptr [rcx+4],r9b
  000000000000000E: 44 88 41 05        mov         byte ptr [rcx+5],r8b
  0000000000000012: 89 11              mov         dword ptr [rcx],edx
  0000000000000014: 48 89 C8           mov         rax,rcx
  0000000000000017: 5D                 pop         rbp
  0000000000000018: C3                 ret

_Dmain:
  0000000000000000: 55                 push        rbp
  0000000000000001: 48 8B EC           mov         rbp,rsp
  0000000000000004: 48 83 EC 20        sub         rsp,20h
  0000000000000008: 56                 push        rsi
  0000000000000009: 57                 push        rdi
  000000000000000A: C6 05 00 00 00 00  mov         byte ptr [_D4test4globg],7Bh
                    7B
  0000000000000011: 48 8D 45 E8        lea         rax,[rbp-18h]
  0000000000000015: 48 31 C9           xor         rcx,rcx
  0000000000000018: 89 08              mov         dword ptr [rax],ecx
  000000000000001A: 66 89 48 04        mov         word ptr [rax+4],cx
  000000000000001E: 48 83 EC 08        sub         rsp,8
  0000000000000022: 51                 push        rcx
  0000000000000023: 44 8A C9           mov         r9b,cl
  0000000000000026: 4D 89 C8           mov         r8,r9
  0000000000000029: 44 88 45 F0        mov         byte ptr [rbp-10h],r8b
  000000000000002D: 44 88 45 F1        mov         byte ptr [rbp-0Fh],r8b
  0000000000000031: 44 88 45 F2        mov         byte ptr [rbp-0Eh],r8b
  0000000000000035: 8B 55 F0           mov         edx,dword ptr [rbp-10h]
  0000000000000038: 48 89 C1           mov         rcx,rax
  000000000000003B: 48 83 EC 20        sub         rsp,20h
  000000000000003F: E8 00 00 00 00     call       
_D4test8DateTime6__ctorMFNchh
hS4test9TimeOfDayZS4test8DateTime
  0000000000000044: 48 83 C4 30        add         rsp,30h
  0000000000000048: 48 8D 45 F8        lea         rax,[rbp-8]
  000000000000004C: 4D 31 C9           xor         r9,r9
  000000000000004F: 44 89 08           mov         dword ptr [rax],r9d
  0000000000000052: 66 44 89 48 04     mov         word ptr [rax+4],r9w
  0000000000000057: 48 8D 75 E8        lea         rsi,[rbp-18h]
  000000000000005B: 48 89 C7           mov         rdi,rax
  000000000000005E: 48 B9 06 00 00 00  mov         rcx,6
                    00 00 00 00
  0000000000000068: 33 C0              xor         eax,eax
  000000000000006A: F3 A6              repe cmps   byte ptr [rsi],byte ptr
[rdi]

  000000000000006C: 74 12              je          0000000000000080
  000000000000006E: B9 1B 00 00 00     mov         ecx,1Bh
  0000000000000073: 48 83 EC 20        sub         rsp,20h
  0000000000000077: E8 00 00 00 00     call        _D4test8__assertFiZv
  000000000000007C: 48 83 C4 20        add         rsp,20h
  0000000000000080: 31 C0              xor         eax,eax
  0000000000000082: 5F                 pop         rdi
  0000000000000083: 5E                 pop         rsi
  0000000000000084: 48 8D 65 00        lea         rsp,[rbp]
  0000000000000088: 5D                 pop         rbp
  0000000000000089: C3                 ret

it shows that the init-value is prepared with 3 byte writes, but a 32-bit word
is read to be passed in edx. It then writes all 4 bytes into the DateTime
struct overwriting the following element with garbage.
If the initializer is taking from the BSS segment, it is the global variable
that is read as the 4th byte.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 13 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10634


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



22:11:58 PDT ---
https://github.com/D-Programming-Language/dmd/pull/2354

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10634




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/1b089fc3c80f5be470028d5eedc205f7597018ec
fix Issue 10634 - Win64: wrong codegen with .int of small structs

https://github.com/D-Programming-Language/dmd/commit/97b843779aa6d361b625052a9f1ffecd54a97129


fix Issue 10634 - Win64: wrong codegen with .int of small structs

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 17 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10634




Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/2c851a4ebffcc3903c403d893e54d6a43e4cd417


fix Issue 10634 - Win64: wrong codegen with .int of small structs

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 17 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10634


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 17 2013