LDC 0.12.0 has been released

bearophile bearophileHUGS at lycos.com
Wed Oct 23 11:40:32 PDT 2013


David Nadlinger:

> 244d52f3c4e03dc2ed5d84940aaa04ab  ldc2-0.12.0-mingw-x86.7z

If I compile some C-looking D code using -noruntime can't LDC2 
not put those runtime routine inside the binary and produce a 
smaller binary?

-----------------------

A small test program:


void main() {
     import core.stdc.stdio, std.math;
     immutable r = 11;
     foreach (immutable y; -r .. r + 1) {
         foreach (immutable x; -2 * r .. 2 * r + 1) {
             bool circle(in int c, in int r) pure nothrow {
                 return r ^^ 2 >= (x / 2) ^^ 2 + (y - c) ^^ 2;
             }
             putchar(circle(-r / 2, r / 6) ? '#' :
                     circle( r / 2, r / 6) ? '.' :
                     circle(-r / 2, r / 2) ? '.' :
                     circle( r / 2, r / 2) ? '#' :
                     circle(     0, r)     ? "#."[x < 0] :
                                           ' ');
         }
         '\n'.putchar;
     }
}


Compiling using ldc 0.12 with ldmd2 -O -release -inline 
-noboundscheck -output-s it produces (note the __d_allocmemory, 
dmd doesn't inline the lambda but it doesn't perform that call):


__Dmain:
     pushl   %ebp
     pushl   %ebx
     pushl   %edi
     pushl   %esi
     subl    $8, %esp
     movl    $8, (%esp)
     calll   __d_allocmemory
     movl    %eax, 4(%esp)
     movl    $-11, %edi
     .align  16, 0x90
LBB0_1:
     movl    4(%esp), %eax
     movl    %edi, (%eax)
     movl    $-22, %ebx
     .align  16, 0x90
LBB0_2:
     movl    4(%esp), %eax
     movl    %ebx, 4(%eax)
     movl    %ebx, %ecx
     shrl    $31, %ecx
     leal    (%ebx,%ecx), %edx
     sarl    %edx
     imull   %edx, %edx
     leal    5(%edi), %ebp
     imull   %ebp, %ebp
     addl    %edx, %ebp
     movl    $35, %eax
     cmpl    $2, %ebp
     jl  LBB0_8
     leal    -5(%edi), %esi
     imull   %esi, %esi
     addl    %edx, %esi
     movl    $46, %eax
     cmpl    $2, %esi
     jl  LBB0_8
     cmpl    $26, %ebp
     jl  LBB0_8
     cmpl    $26, %esi
     movl    $35, %eax
     jl  LBB0_8
     movl    %edi, %esi
     imull   %esi, %esi
     addl    %edx, %esi
     movl    $32, %eax
     cmpl    $121, %esi
     jg  LBB0_8
     movzbl  _.str(%ecx), %eax
     .align  16, 0x90
LBB0_8:
     calll   __D4core4stdc5stdio7putcharFNbNeiZi
     incl    %ebx
     cmpl    $23, %ebx
     jne LBB0_2
     movl    $10, %eax
     calll   __D4core4stdc5stdio7putcharFNbNeiZi
     incl    %edi
     cmpl    $12, %edi
     jne LBB0_1
     xorl    %eax, %eax
     addl    $8, %esp
     popl    %esi
     popl    %edi
     popl    %ebx
     popl    %ebp
     ret


If I replace the lambda with an inner function:

void main() {
     import core.stdc.stdio, std.math;
     immutable r = 11;
     foreach (immutable y; -r .. r + 1) {
         foreach (immutable x; -2 * r .. 2 * r + 1) {
             bool circle(in int c, in int r) pure nothrow {
                 return r ^^ 2 >= (x / 2) ^^ 2 + (y - c) ^^ 2;
             }
             putchar(circle(-r / 2, r / 6) ? '#' :
                     circle( r / 2, r / 6) ? '.' :
                     circle(-r / 2, r / 2) ? '.' :
                     circle( r / 2, r / 2) ? '#' :
                     circle(     0, r)     ? "#."[x < 0] :
                                           ' ');
         }
         '\n'.putchar;
     }
}


Now ldc doesn't show the call to __d_allocmemory, is this 
expected?

__Dmain:
     pushl   %ebp
     pushl   %ebx
     pushl   %edi
     pushl   %esi
     subl    $12, %esp
     movl    $-11, %eax
     .align  16, 0x90
LBB0_1:
     movl    %eax, (%esp)
     movl    %eax, %ecx
     imull   %ecx, %ecx
     movl    %ecx, 8(%esp)
     leal    -5(%eax), %ecx
     imull   %ecx, %ecx
     movl    %ecx, 4(%esp)
     leal    5(%eax), %ebp
     imull   %ebp, %ebp
     movl    $-22, %esi
     .align  16, 0x90
LBB0_2:
     movl    %esi, %ecx
     shrl    $31, %ecx
     leal    (%esi,%ecx), %edx
     sarl    %edx
     imull   %edx, %edx
     leal    (%edx,%ebp), %edi
     movl    $35, %eax
     cmpl    $2, %edi
     jl  LBB0_8
     movl    4(%esp), %eax
     leal    (%edx,%eax), %ebx
     movl    $46, %eax
     cmpl    $2, %ebx
     jl  LBB0_8
     cmpl    $26, %edi
     jl  LBB0_8
     cmpl    $26, %ebx
     movl    $35, %eax
     jl  LBB0_8
     addl    8(%esp), %edx
     movl    $32, %eax
     cmpl    $121, %edx
     jg  LBB0_8
     movzbl  _.str(%ecx), %eax
     .align  16, 0x90
LBB0_8:
     calll   __D4core4stdc5stdio7putcharFNbNeiZi
     incl    %esi
     cmpl    $23, %esi
     jne LBB0_2
     movl    $10, %eax
     calll   __D4core4stdc5stdio7putcharFNbNeiZi
     movl    (%esp), %eax
     incl    %eax
     cmpl    $12, %eax
     jne LBB0_1
     xorl    %eax, %eax
     addl    $12, %esp
     popl    %esi
     popl    %edi
     popl    %ebx
     popl    %ebp
     ret


Bye,
bearophile


More information about the digitalmars-d-ldc mailing list