Thread overview
Compiler Explorer Assembly Output for C, C++ and D (dlang)
May 27, 2021
Tariq Siddiqui
May 27, 2021
Dennis
May 27, 2021
Tariq Siddiqui
May 27, 2021
Dennis
May 27, 2021
Basile B.
May 27, 2021
Gavin Ray
May 27, 2021
Alain De Vos
May 27, 2021

When using Compiler Explorer (https://godbolt.org/) to compare assembly output of simple programs, why D language assembly output is so long compared to C or C++ output. The simple square function output is the same for C, C++, and D, but the D output has additional lines that are not highlighted when hovering over the square function in the source code.

  • What are these additional lines?
  • How can I remove these lines from being generated?
May 27, 2021

On Thursday, 27 May 2021 at 08:47:50 UTC, Tariq Siddiqui wrote:

>
  • What are these additional lines?

D generates extra symbols per module for things like module constructors, unittests and class introspection (I think).

>
  • How can I remove these lines from being generated?

Pass the -betterC flag to dmd/ldc2.

May 27, 2021

On Thursday, 27 May 2021 at 09:11:35 UTC, Dennis wrote:

>

On Thursday, 27 May 2021 at 08:47:50 UTC, Tariq Siddiqui wrote:

>
  • What are these additional lines?

D generates extra symbols per module for things like module constructors, unittests and class introspection (I think).

>
  • How can I remove these lines from being generated?

Pass the -betterC flag to dmd/ldc2.

Thanks for your answer, -betterC works well with simple code but when using templates in code -betterC compilation failed.

May 27, 2021

On Thursday, 27 May 2021 at 10:27:42 UTC, Tariq Siddiqui wrote:

>

Thanks for your answer, -betterC works well with simple code but when using templates in code -betterC compilation failed.

Templates are supported in -betterC, what's not supported can be found here:
https://dlang.org/spec/betterc.html#consequences

You're probably using or importing something that depends on Druntime.
For LDC, you can try adding this instead of using -betterC:

pragma(LDC_no_moduleinfo);

Though once you use non-betterC functions, your assembly will easily get messy compared to C's assembly.

May 27, 2021

On Thursday, 27 May 2021 at 08:47:50 UTC, Tariq Siddiqui wrote:

>

When using Compiler Explorer (https://godbolt.org/) to compare assembly output of simple programs, why D language assembly output is so long compared to C or C++ output. The simple square function output is the same for C, C++, and D, but the D output has additional lines that are not highlighted when hovering over the square function in the source code.

  • What are these additional lines?
  • How can I remove these lines from being generated?

In addition to other answers, see https://forum.dlang.org/post/myrjutqyzpzlyltrdkwc@forum.dlang.org

May 27, 2021

On Thursday, 27 May 2021 at 10:48:43 UTC, Basile B. wrote:

>

https://forum.dlang.org/post/myrjutqyzpzlyltrdkwc@forum.dlang.org

Thank you for sharing this!

The difference between setting pragma(LDC_no_moduleinfo); at the top, and -Os -gline-tables-only in compiler flags is drastic.

You wind up with

int example.square(int):
        mov     eax, edi
        imul    eax, edi
        ret
define i32 @_D7example6squareFiZi(i32 %num_arg) local_unnamed_addr #0 !dbg !5 {
  %1 = mul i32 %num_arg, %num_arg, !dbg !7
  ret i32 %1, !dbg !7
}

attributes #0 = { norecurse nounwind readnone uwtable "frame-pointer"="none" "target-cpu"="x86-64" "target-features"="+cx16" }
May 27, 2021

Sidequestion.
For what purpose is the registration of dso used ?