Thread overview
[Issue 8903] New: Bad code for enum array members
Oct 28, 2012
yebblies
Jan 31, 2013
Don
October 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8903

           Summary: Bad code for enum array members
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: siegelords_abode@yahoo.com


--- Comment #0 from siegelords_abode@yahoo.com 2012-10-28 00:32:58 PDT ---
struct A
{
    immutable int arr[8] = [1, 2, 3, 4, 5, 6, 7, 8];
}

struct B
{
    enum int arr[8] = [1, 2, 3, 4, 5, 6, 7, 8];
}

void test_a(int n)
{
    auto a = A();
    auto b = a.arr[n];
}

/*
0000000000416c68 <_D4test6test_aFiZv>:
  416c68:    55                       push   rbp
  416c69:    48 8b ec                 mov    rbp,rsp
  416c6c:    48 83 ec 20              sub    rsp,0x20
  416c70:    89 7d f8                 mov    DWORD PTR [rbp-0x8],edi
  416c73:    c6 45 f0 00              mov    BYTE PTR [rbp-0x10],0x0
  416c77:    48 63 45 f8              movsxd rax,DWORD PTR [rbp-0x8]
  416c7b:    48 83 f8 08              cmp    rax,0x8
  416c7f:    48 89 45 e8              mov    QWORD PTR [rbp-0x18],rax
  416c83:    72 0a                    jb     416c8f <_D4test6test_aFiZv+0x27>
  416c85:    bf 10 00 00 00           mov    edi,0x10
  416c8a:    e8 a1 00 00 00           call   416d30 <_D4test7__arrayZ>
  416c8f:    48 8b 4d e8              mov    rcx,QWORD PTR [rbp-0x18]
  416c93:    8b 14 8d f0 41 63 00     mov    edx,DWORD PTR [rcx*4+0x6341f0]
  416c9a:    c9                       leave
  416c9b:    c3                       ret
*/


void test_b(int n)
{
    auto a = B();
    auto b = a.arr[n];
}

/*
0000000000416c9c <_D4test6test_bFiZv>:
  416c9c:    55                       push   rbp
  416c9d:    48 8b ec                 mov    rbp,rsp
  416ca0:    48 83 ec 20              sub    rsp,0x20
  416ca4:    89 7d f8                 mov    DWORD PTR [rbp-0x8],edi
  416ca7:    c6 45 f0 00              mov    BYTE PTR [rbp-0x10],0x0
  416cab:    48 63 45 f8              movsxd rax,DWORD PTR [rbp-0x8]
  416caf:    48 b9 08 00 00 00 00     movabs rcx,0x8
  416cb6:    00 00 00
  416cb9:    48 3b c1                 cmp    rax,rcx
  416cbc:    48 89 45 e8              mov    QWORD PTR [rbp-0x18],rax
  416cc0:    72 0a                    jb     416ccc <_D4test6test_bFiZv+0x30>
  416cc2:    bf 16 00 00 00           mov    edi,0x16
  416cc7:    e8 64 00 00 00           call   416d30 <_D4test7__arrayZ>
  416ccc:    48 be 08 00 00 00 00     movabs rsi,0x8
  416cd3:    00 00 00
  416cd6:    48 bf 60 43 63 00 00     movabs rdi,0x634360
  416cdd:    00 00 00
  416ce0:    e8 1b 23 00 00           call   419000 <_d_arrayliteralTX>
  416ce5:    c7 00 01 00 00 00        mov    DWORD PTR [rax],0x1
  416ceb:    ba 02 00 00 00           mov    edx,0x2
  416cf0:    89 50 04                 mov    DWORD PTR [rax+0x4],edx
  416cf3:    c7 40 08 03 00 00 00     mov    DWORD PTR [rax+0x8],0x3
  416cfa:    c7 40 0c 04 00 00 00     mov    DWORD PTR [rax+0xc],0x4
  416d01:    c7 40 10 05 00 00 00     mov    DWORD PTR [rax+0x10],0x5
  416d08:    c7 40 14 06 00 00 00     mov    DWORD PTR [rax+0x14],0x6
  416d0f:    c7 40 18 07 00 00 00     mov    DWORD PTR [rax+0x18],0x7
  416d16:    c7 40 1c 08 00 00 00     mov    DWORD PTR [rax+0x1c],0x8
  416d1d:    48 8b 4d e8              mov    rcx,QWORD PTR [rbp-0x18]
  416d21:    8b 04 88                 mov    eax,DWORD PTR [rax+rcx*4]
  416d24:    c9                       leave
  416d25:    c3                       ret
  416d26:    90                       nop
  416d27:    90                       nop
*/

void main()
{
}

It makes no sense to me for the enum array to cause re-allocation when instantiated. I know that enum arrays are problematic entities to begin with, but while they are allowed to exist, they shouldn't generate horrible code like that.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8903


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |DUPLICATE


--- Comment #1 from yebblies <yebblies@gmail.com> 2012-10-29 05:14:55 EST ---
This is not wrong-code, this is a performance issue.

*** This issue has been marked as a duplicate of issue 2356 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8903


siegelords_abode@yahoo.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|DUPLICATE                   |


--- Comment #2 from siegelords_abode@yahoo.com 2012-10-28 12:35:49 PDT ---
No, it is not. Issue 2356 does not affect the array in struct A. The bug here is the difference between A and B and not how badly B is implemented. Both cases (or should) involve static array literal initialization, with the bug being that B does not use it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 31, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8903



--- Comment #3 from Don <clugdbug@yahoo.com.au> 2013-01-31 06:40:49 PST ---
I don't think the code should compile. 'enum' should be declaring a manifest constant, it shouldn't be possible to index it at run time. The whole point of these enum constants was that they shouldn't appear in the executable!

If you want it to appear in the executable, use can use immutable or const.

This is a duplicate of another bug, I forget which one though.

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