Digital Mars D compiler; acquiring ASM output
Asked Answered
L

3

7

I am reading the book from Andrei Alexandrescu about the D programming language. He's an excellent writer and does a pretty good job at explaining aspects of the D language. I however find certain constructs hard to understand when I cannot imagine the ASM output or consequences of certain keywords (such as in, out, etc. or other constructs). Even though my ASM is pretty bad and I never use it, it helps me a lot to be able to understand how certain keywords work out to the computer and the work being done.

The DMD compiler has many interesting features (code coverage, generating interfaces (header files), generating documentation, profiling, ...) but I haven't seen a switch to output ASM code. The compiler does generate .obj files, and from reading the following link: http://www.digitalmars.com/ctg/obj2asm.html I suspect I need a tool to convert the object files manually. I would prefer a compiler switch, is there one?

On the bottom of that page, I get linked to a page where I can pay for the products mentioning containing that tool. Coming from a GNU background I highly frowned up on that. Is this for C/C++ only, or does this also apply for the D compiler?

Is there any other way to convert these .obj files to readable ASM code, or must I resort to other D compilers (such as GDC or LDC) to acquire ASM output? I prefer not to. DMD is created by the founder himself, I'm sure he implemented most features correctly / largely optimized.

So, how can I take a peek at the ASM code?

Thank you.

Lamellibranch answered 28/8, 2010 at 21:42 Comment(1)
Additionally, DMD is the only compiler with full support for D 2.0Krissy
T
7

You could try objconv. It's what I use. The reason DMD doesn't output assembler via a switch is because it never generates ASM as a discrete step. It generates binary opcodes directly from its internal representation. This is part of the reason why it compiles so fast.

Or you can use the DMD obj2asm tool which comes packaged with DMD.

obj2asm somefile.o > somefile.s
Tragacanth answered 28/8, 2010 at 22:48 Comment(5)
Thank you, that does give me ASM output indeed.Lamellibranch
Is there a way to reduce the ASM output to just the code I wrote myself? At the moment the object file seems to have been linked statically to the D's library, is this possible or should I just live with it?Lamellibranch
@daevius: There's probably not much you can do here. These should be template instantations, as the rest of Phobos shouldn't end up in the same object file because it's separately compiled.Tragacanth
If you happen to be on linux, you might be able to use sed to filter out the stuff you don't want.Heerlen
This is now objdump.Se
K
10

The obj2asm utility is provided by the DMD compiler suite, which is available for free (under a dual GPL and Artistic license). See DMD Compiler for Linux on the D Programming Language website.

Krissy answered 28/8, 2010 at 21:56 Comment(2)
Thank you, thus a Linux OS would be required to obtain ASM sources (or some other tool)? I am not sure why this is Linux only...Lamellibranch
Here is the windows binary of obj2asm github.com/DigitalMars/dmc/blob/master/bin/obj2asm.exeWilden
T
7

You could try objconv. It's what I use. The reason DMD doesn't output assembler via a switch is because it never generates ASM as a discrete step. It generates binary opcodes directly from its internal representation. This is part of the reason why it compiles so fast.

Or you can use the DMD obj2asm tool which comes packaged with DMD.

obj2asm somefile.o > somefile.s
Tragacanth answered 28/8, 2010 at 22:48 Comment(5)
Thank you, that does give me ASM output indeed.Lamellibranch
Is there a way to reduce the ASM output to just the code I wrote myself? At the moment the object file seems to have been linked statically to the D's library, is this possible or should I just live with it?Lamellibranch
@daevius: There's probably not much you can do here. These should be template instantations, as the rest of Phobos shouldn't end up in the same object file because it's separately compiled.Tragacanth
If you happen to be on linux, you might be able to use sed to filter out the stuff you don't want.Heerlen
This is now objdump.Se
E
-1

The Digital Mars compiler's backend is commercially licensed, not open source, perhaps explaining why tools relating to the backend output are also proprietary.

For educational purposes you might try http://dgcc.sourceforge.net/ which is a D front end on the GCC backend, thus entirely open source.

Elidaelidad answered 28/8, 2010 at 21:51 Comment(3)
All Digital Mars compilers are (AFAIK) available for free; and the reference D compiler (DMD) is under a GPL license. Most of their tools are, as well.Krissy
Only the front end is GPL. The back end is commercially licensed.Reign
@Peter Alexander but the source code is available nevertheless.Guile

© 2022 - 2024 — McMap. All rights reserved.