dmd 2.029 release

bearophile bearophileHUGS at lycos.com
Tue Apr 21 07:20:49 PDT 2009


Don:
>  From 2.208:
> 2804 Impure nested functions should be legal inside pure functions

Very good. Now that little example works:

import std.c.stdio: printf;
import std.conv: to;

pure int double_sqr(int x) {
    int y, z;
    void do_sqr() { y *= y; }
    y = x;
    do_sqr();
    z += y;
    y = x;
    do_sqr();
    z += y;
    return z;
}

void main(string[] args) {
    int x = args.length == 2 ? to!(int)(args[1]) : 10;
    int y = double_sqr(x) + double_sqr(x);
    printf("4 * x * x = %d\n", y);
}

The outout is correct:
4 * x * x = 400


"Cleaned up" ASM produced by DMD V.2.029, -O -release -inline:

double_sqr:
        push    EAX
        mov ECX,EAX
        mov [ESP],0
        mov [ESP],ECX
        imul    EAX,[ESP]
        mov EDX,EAX
        mov [ESP],EAX
        mov EAX,ECX
        mov [ESP],ECX
        imul    EAX,[ESP]
        add EDX,EAX
        mov [ESP],EAX
        mov EAX,EDX
        pop ECX
        ret

double_sqr.do_sqr:
        mov ECX,[EAX]
        imul    ECX,[EAX]
        mov [EAX],ECX
        ret

main:
L0:     sub ESP,014h
        cmp dword ptr 018h[ESP],2
        jne L21
        mov EDX,01Ch[ESP]
        mov EAX,018h[ESP]
        mov EAX,8[EDX]
        mov EDX,0Ch[EDX]
        push    EDX
        push    EAX
        call    near ptr parseString
        jmp short   L26
L21:        mov EAX,0Ah
L26:        call    near ptr double_sqr
        add EAX,EAX
        mov ECX,offset FLAT:_DATA
        push    EAX
        push    ECX
        call    near ptr printf

As you can see there's only one call to double_sqr in the main program, so the pure semantics is working here.

You can also see do_sqr is inlined into double_sqr. But the asm code inside double_sqr looks very hairy and long, and double_sqr itself isn't inlined in the main.

Bye,
bearophile


More information about the Digitalmars-d-announce mailing list