Walter Bright:

http://wiki.dlang.org/DIP60

Start on implementation:

https://github.com/D-Programming-Language/dmd/pull/3455

If I have this program:

__gshared int x = 5;
int main() {
    int[] a = [x, x + 10, x * x];
    return a[0] + a[1] + a[2];
}


If I compile with all optimizations DMD produces this X86 asm, that contains the call to __d_arrayliteralTX, so that main can't be @nogc:

__Dmain:
L0:     push    EAX
        push    EAX
        mov EAX,offset FLAT:_D11TypeInfo_Ai6__initZ
        push    EBX
        push    ESI
        push    EDI
        push    3
        push    EAX
        call    near ptr __d_arrayliteralTX
        mov EBX,EAX
        mov ECX,_D4test1xi
        mov [EBX],ECX
        mov EDX,_D4test1xi
        add EDX,0Ah
        mov 4[EBX],EDX
        mov ESI,_D4test1xi
        imul    ESI,ESI
        mov 8[EBX],ESI
        mov EAX,3
        mov ECX,EBX
        mov 014h[ESP],EAX
        mov 018h[ESP],ECX
        add ESP,8
        mov EDI,010h[ESP]
        mov EAX,[EDI]
        add EAX,4[EDI]
        add EAX,8[EDI]
        pop EDI
        pop ESI
        pop EBX
        add ESP,8
        ret


If I compile that code with ldc2 without optimizations the result is similar, there is a call to __d_newarrayvT:

__Dmain:
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %esi
    andl    $-8, %esp
    subl    $32, %esp
    leal    __D11TypeInfo_Ai6__initZ, %eax
    movl    $3, %ecx
    movl    %eax, (%esp)
    movl    $3, 4(%esp)
    movl    %ecx, 12(%esp)
    calll   __d_newarrayvT
    movl    %edx, %ecx
    movl    __D4test1xi, %esi
    movl    %esi, (%edx)
    movl    __D4test1xi, %esi
    addl    $10, %esi
    movl    %esi, 4(%edx)
    movl    __D4test1xi, %esi
    imull   __D4test1xi, %esi
    movl    %esi, 8(%edx)
    movl    %eax, 16(%esp)
    movl    %ecx, 20(%esp)
    movl    20(%esp), %eax
    movl    20(%esp), %ecx
    movl    (%eax), %eax
    addl    4(%ecx), %eax
    movl    20(%esp), %ecx
    addl    8(%ecx), %eax
    leal    -4(%ebp), %esp
    popl    %esi
    popl    %ebp
    ret



But if I compile the code with ldc2 with full optimizations the compiler is able to perform a bit of escape analysis, and to see the array doesn't need to be allocated, and produces the asm:

__Dmain:
    movl    __D4test1xi, %eax
    movl    %eax, %ecx
    imull   %ecx, %ecx
    addl    %eax, %ecx
    leal    10(%eax,%ecx), %eax
    ret

Now there are no memory allocations.

So what's the right behavour of @nogc? Is it possible to compile this main with a future version of ldc2 if I compile the code with full optimizations?

Bye,
bearophile

Reply via email to