www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - DMD now incorporates a disassembler

reply Walter Bright <newshound2 digitalmars.com> writes:
Compile with -vasm to see it! Enjoy!

For the file test.d:

   int demo(int x)
   {
     return x * x;
   }

Compiling with:

   dmd test.d -c -vasm

prints:

   _D4test4demoFiZi:
   0000:   89 F8                   mov     EAX,EDI
   0002:   0F AF C0                imul    EAX,EAX
   0005:   C3                      ret


https://github.com/dlang/dmd/pull/13447
Jan 07 2022
next sibling parent dd <dd dax.moe> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
Oh very nice! I need to finish my debugger/disassembler project.
Jan 07 2022
prev sibling next sibling parent reply Dukc <ajieskola gmail.com> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 For the file test.d:

   int demo(int x)
   {
     return x * x;
   }

 Compiling with:

   dmd test.d -c -vasm

 prints:

   _D4test4demoFiZi:
   0000:   89 F8                   mov     EAX,EDI
   0002:   0F AF C0                imul    EAX,EAX
   0005:   C3                      ret


 https://github.com/dlang/dmd/pull/13447
Wow, very useful! This feature surely lowers the bar to check the disassembly when optimising. Thanks! I'm slightly disappointed it does not output the asm inlined to D code but that's just my daydreaming with no practical reasons to back it up.
Jan 07 2022
parent reply max haughton <maxhaton gmail.com> writes:
On Friday, 7 January 2022 at 23:14:54 UTC, Dukc wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 For the file test.d:

   int demo(int x)
   {
     return x * x;
   }

 Compiling with:

   dmd test.d -c -vasm

 prints:

   _D4test4demoFiZi:
   0000:   89 F8                   mov     EAX,EDI
   0002:   0F AF C0                imul    EAX,EAX
   0005:   C3                      ret


 https://github.com/dlang/dmd/pull/13447
Wow, very useful! This feature surely lowers the bar to check the disassembly when optimising. Thanks! I'm slightly disappointed it does not output the asm inlined to D code but that's just my daydreaming with no practical reasons to back it up.
https://stackoverflow.com/questions/2511018/how-does-objdump-manage-to-display-source-code-with-the-s-option Enjoy
Jan 09 2022
parent Walter Bright <newshound2 digitalmars.com> writes:
On 1/9/2022 11:33 AM, max haughton wrote:
 https://stackoverflow.com/questions/2511018/how-does-objdump-manage-to-display-source-co
e-with-the-s-option 
obj2asm does the same thing: https://www.digitalmars.com/ctg/obj2asm.html
Jan 09 2022
prev sibling next sibling parent reply Elronnd <elronnd elronnd.net> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
   0000:   89 F8                   mov     EAX,EDI
Feature request: octal.
Jan 07 2022
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 1/7/2022 4:43 PM, Elronnd wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
   0000:   89 F8                   mov     EAX,EDI
Feature request: octal.
I buried my PDP-11 long ago. Sob.
Jan 08 2022
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 09/01/2022 4:01 PM, Walter Bright wrote:
 I buried my PDP-11 long ago. Sob.
There is a kit for the control panel[0]. Backed by a raspberry pi. I'm pretty keen to eventually buy one and build it. These kits are cool! [0] https://www.tindie.com/products/obso/pdp-11-replica-kit-the-pidp-11/
Jan 08 2022
prev sibling next sibling parent reply Brian Callahan <bcallah openbsd.org> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 For the file test.d:

   int demo(int x)
   {
     return x * x;
   }

 Compiling with:

   dmd test.d -c -vasm

 prints:

   _D4test4demoFiZi:
   0000:   89 F8                   mov     EAX,EDI
   0002:   0F AF C0                imul    EAX,EAX
   0005:   C3                      ret


 https://github.com/dlang/dmd/pull/13447
Thanks Walter. This is quite useful. Will put it through its paces. Already spotted some print formatting weirdness; will send as bug reports. ~Brian
Jan 07 2022
parent Walter Bright <newshound2 digitalmars.com> writes:
On 1/7/2022 7:25 PM, Brian Callahan wrote:
 Thanks Walter. This is quite useful.
Welcs. I'm already productively using it myself.
Jan 08 2022
prev sibling next sibling parent reply ag0aep6g <anonymous example.com> writes:
On 07.01.22 22:41, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
With feature creep in full swing now, when can I expect to read my email with DMD?
Jan 07 2022
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Jan 08, 2022 at 07:39:54AM +0100, ag0aep6g via Digitalmars-d-announce
wrote:
 On 07.01.22 22:41, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
With feature creep in full swing now, when can I expect to read my email with DMD?
You already can: echo 'import std;void main(){execute("/usr/bin/mail");}' | dmd -run - :-P T -- "Real programmers can write assembly code in any language. :-)" -- Larry Wall
Jan 07 2022
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 1/7/2022 10:39 PM, ag0aep6g wrote:
 With feature creep in full swing now, when can I expect to read my email with
DMD?
The real question is why doesn't your email reader have an option to disassemble the email?
Jan 08 2022
prev sibling next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 For the file test.d:

   int demo(int x)
   {
     return x * x;
   }

 Compiling with:

   dmd test.d -c -vasm

 prints:

   _D4test4demoFiZi:
   0000:   89 F8                   mov     EAX,EDI
   0002:   0F AF C0                imul    EAX,EAX
   0005:   C3                      ret


 https://github.com/dlang/dmd/pull/13447
Nice!
Jan 08 2022
prev sibling next sibling parent Andre Pany <andre s-e-a-p.de> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 For the file test.d:

   int demo(int x)
   {
     return x * x;
   }

 Compiling with:

   dmd test.d -c -vasm

 prints:

   _D4test4demoFiZi:
   0000:   89 F8                   mov     EAX,EDI
   0002:   0F AF C0                imul    EAX,EAX
   0005:   C3                      ret


 https://github.com/dlang/dmd/pull/13447
Great news, this worth a hn / reddit post! Kind regards Andre
Jan 08 2022
prev sibling next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 [snip]
Would make a nice project for someone to integrate this into run.dlang.org
Jan 08 2022
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/8/22 12:23 PM, jmh530 wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 [snip]
Would make a nice project for someone to integrate this into run.dlang.org
Isn't there already an ASM button? -Steve
Jan 08 2022
parent reply max haughton <maxhaton gmail.com> writes:
On Saturday, 8 January 2022 at 18:08:27 UTC, Steven Schveighoffer 
wrote:
 On 1/8/22 12:23 PM, jmh530 wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!

 [snip]
Would make a nice project for someone to integrate this into run.dlang.org
Isn't there already an ASM button? -Steve
Yup.
Jan 08 2022
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Jan 08, 2022 at 08:29:20PM +0000, max haughton via
Digitalmars-d-announce wrote:
 On Saturday, 8 January 2022 at 18:08:27 UTC, Steven Schveighoffer wrote:
 On 1/8/22 12:23 PM, jmh530 wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
[...]
 Would make a nice project for someone to integrate this into
 run.dlang.org
Isn't there already an ASM button?
[...]
 Yup.
Better yet, the ASM button on run.dlang.org shows disassembly for all 3 compilers, not just dmd. T -- The early bird gets the worm. Moral: ewww...
Jan 08 2022
prev sibling parent reply Vladimir Marchevsky <vladimmi gmail.com> writes:
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
Any practical reason to put disassembler into compiler instead of making it a separate tool? Any ETA for renaming it into DMD Burning ROM? :)
Jan 08 2022
next sibling parent reply max haughton <maxhaton gmail.com> writes:
On Saturday, 8 January 2022 at 18:47:11 UTC, Vladimir Marchevsky 
wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
Any practical reason to put disassembler into compiler instead of making it a separate tool? Any ETA for renaming it into DMD Burning ROM? :)
Most other compilers have been able to do this for years. The only difference is that the way the dmd backend is designed basically means that it never knows the instructions in a given basic block until they are actually emitted, so it has to disassemble it's own output rather than printing it's internal representation with (say) Intel assembly syntax. See https://llvm.org/doxygen/classllvm_1_1MachineInstr.html from LLVM GCC actually *only* uses an assembler to build object files. It doesn't have a distinct IR like LLVM does but the final stage of the RTL is basically a 1:1 representation of the instruction set: ```d void phoneHome(size_t); auto getLen(int[] arr) { phoneHome(arr.length); return arr.length; } ``` ends up as ``` ;; Function getLen (_D7example6getLenFAiZm, funcdef_no=0, decl_uid=1395, cgraph_uid=2, symbol_order=1) (note 1 0 37 NOTE_INSN_DELETED) (note 37 1 8 (var_location arr (parallel [ (expr_list:REG_DEP_TRUE (reg:DI 5 di [ arr ]) (const_int 0 [0])) (expr_list:REG_DEP_TRUE (reg:DI 4 si [ arr+8 ]) (const_int 8 [0x8])) ])) NOTE_INSN_VAR_LOCATION) (note 8 37 7 2 [bb 2] NOTE_INSN_BASIC_BLOCK) (note 7 8 26 2 NOTE_INSN_FUNCTION_BEG) (insn/f:TI 26 7 27 2 (set (mem:DI (pre_dec:DI (reg/f:DI 7 sp)) [0 S8 A8]) (reg:DI 3 bx)) "/app/example.d":2:6 54 {*pushdi2_rex64} (expr_list:REG_DEAD (reg:DI 3 bx) (nil))) (note 27 26 2 2 NOTE_INSN_PROLOGUE_END) (insn 2 27 38 2 (set (reg:DI 3 bx [orig:85 arr ] [85]) (reg:DI 5 di [92])) "/app/example.d":2:6 80 {*movdi_internal} (nil)) (note 38 2 13 2 (var_location arr (reg:TI 3 bx [orig:85 arr ] [85])) NOTE_INSN_VAR_LOCATION) (call_insn:TI 13 38 39 2 (call (mem:QI (symbol_ref:DI ("_D7example9phoneHomeFmZv") [flags 0x41] <function_decl 0x7f491d317600 phoneHome>) [0 phoneHome S1 A8]) (const_int 0 [0])) "/app/example.d":4:14 886 {*call} (expr_list:REG_CALL_ARG_LOCATION (nil) (expr_list:REG_DEAD (reg:DI 5 di) (expr_list:REG_CALL_DECL (symbol_ref:DI ("_D7example9phoneHomeFmZv") [flags 0x41] <function_decl 0x7f491d317600 phoneHome>) (nil)))) (expr_list:DI (use (reg:DI 5 di)) (nil))) (note/c 39 13 17 2 (var_location arr (nil)) NOTE_INSN_VAR_LOCATION) (insn 17 39 36 2 (set (reg/i:DI 0 ax) (reg:DI 3 bx [orig:85 arr ] [85])) "/app/example.d":6:1 80 {*movdi_internal} (expr_list:REG_DEAD (reg:DI 3 bx [orig:85 arr ] [85]) (nil))) (note 36 17 29 2 NOTE_INSN_EPILOGUE_BEG) (insn/f 29 36 40 2 (set (reg:DI 3 bx) (mem:DI (post_inc:DI (reg/f:DI 7 sp)) [0 S8 A8])) "/app/example.d":6:1 62 {*popdi1} (expr_list:REG_CFA_ADJUST_CFA (set (reg/f:DI 7 sp) (plus:DI (reg/f:DI 7 sp) (const_int 8 [0x8]))) (nil))) (note 40 29 18 2 (var_location arr (nil) [uninit]) NOTE_INSN_VAR_LOCATION) (insn 18 40 30 2 (use (reg/i:DI 0 ax)) "/app/example.d":6:1 -1 (nil)) (jump_insn:TI 30 18 33 2 (simple_return) "/app/example.d":6:1 910 {simple_return_internal} (nil) -> simple_return) (barrier 33 30 25) (note 25 33 0 NOTE_INSN_DELETED) ``` just prior to spitting it out for the assembler to process.
Jan 08 2022
next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 1/8/2022 12:50 PM, max haughton wrote:
 On Saturday, 8 January 2022 at 18:47:11 UTC, Vladimir Marchevsky wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
Any practical reason to put disassembler into compiler instead of making it a separate tool?  Any ETA for renaming it into DMD Burning ROM? :)
Most other compilers have been able to do this for years.
I've never seen one. What's the switch for gcc to do the same thing?
 The only difference is 
 that the way the dmd backend is designed basically means that it never knows
the 
 instructions in a given basic block until they are actually emitted, so it has 
 to disassemble it's own output rather than printing it's internal
representation 
 with (say) Intel assembly syntax.
 
 See https://llvm.org/doxygen/classllvm_1_1MachineInstr.html from LLVM
 
 GCC actually *only* uses an assembler to build object files.
Compilers that take a detour through an assembler to generate code are inherently slower.
 It doesn't have a 
 distinct IR like LLVM does but the final stage of the RTL is basically a 1:1 
 representation of the instruction set:
That looks like intermediate code, not assembler.
Jan 08 2022
parent reply max haughton <maxhaton gmail.com> writes:
On Sunday, 9 January 2022 at 02:58:43 UTC, Walter Bright wrote:
 I've never seen one. What's the switch for gcc to do the same 
 thing?
For GCC/Clang you'd want -S (and then -masm=intel to make the output ~~beautiful to nobody but the blind~~ readable). This dumps the output to a file, which isn't exactly the same as what -vasm does, but I have already begun piping the -vasm output to a file since (say) hello world yields a thousand lines of output which is much easier to consume in a text editor. To do it with ldc the flag is `--output-s`. I have opened a PR to make the ldc dmd-compatibility wrapper (`ldmd2`) mimic -vasm Intel (and to a lesser extent Clang) actually annotate the generated text with annotations intended to be read by the humans. e.g. Intel C++ (which is in the process of being replaced with Clang relabeled as Intel C++) prints it's (hopeless unless you are using PGO, but still) estimates of the branch probabilities. ``` ``` You can also ask the compiler to generate an optimization report inline with the assembly code. This *is* useful when tuning since you can tell what the compiler is or isn't getting right (e.g. find which roads to force the loop unrolling down). The Intel Compiler also has a reputation for having an arsenal of dirty tricks to make your code "faster" which it will deploy on the hope that you (say) don't notice that your floating point numbers are now less precise. `-qopt-report-phase=vec` yields: ``` ``` People don't seem to care about SPEC numbers too much anymore, but the Intel Compilers still have many features for gaming standard test scores. http://www.spec.org/cpu2006/results/res2007q3/cpu2006-20070821-01880.html If you looked at this, you'd think that Intel just managed a huge increase on `libquantum` which we can all use on our own code, but it turns out they worked out they can just tell the compiler to automagically parallelize the code, but still only have 1 nominal process. https://stackoverflow.com/questions/61016358/why-can-gcc-only-do-loop-interchange-optimization-when-the-i t-size-is-a-compile for more overfitting.
 Compilers that take a detour through an assembler to generate 
 code are inherently slower.
Certainly, although in my experience not by much. Time spent in the assembler in dominated by time spent in the linker, and just about everywhere else in the compiler (especially when you turn optimizations on). Hello World is about 4ms in the assembler on my machine. GCC and Clang have very different architectures in this regard but end up being pretty similar in terms of compile times. The linker an exception to that rule of thumb, however, in that the LLVM linker is much faster than any current GNU offering.
 It doesn't have a distinct IR like LLVM does but the final 
 stage of the RTL is basically a 1:1 representation of the 
 instruction set:
That looks like intermediate code, not assembler.
It is the (final) intermediate code, but it's barely intermediate at this stage i.e. these are effectively just the target instructions printed with LISP syntax. It's, helpfully, quite obfuscated unfortunately: Some of that is technical baggage, some of it is due to the way that GCC was explicitly directed to be difficult to consume). I'm __not__ suggesting any normal programmer should use, just showing what GCC does since I mentioned LLVM. Anyway, I've been playing with -vasm and I think it seems pretty good so far. There are some formatting issues which shouldn't be hard to fix at all (this is why we asked for some basic tests of the shape of the output), put I think I've only found one (touch wood) situation where it actually gets the instruction *wrong* so far. Testing it has led to me finding some fairly bugs in the dmd inline assembler, which I am in the process of filing.
Jan 08 2022
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 1/8/2022 10:04 PM, max haughton wrote:
 Anyway, I've been playing with -vasm and I think it seems pretty good so far. 
 There are some formatting issues which shouldn't be hard to fix at all (this
is 
 why we asked for some basic tests of the shape of the output), put I think
I've 
 only found one (touch wood) situation where it actually gets the instruction 
 *wrong* so far.
 
 Testing it has led to me finding some fairly bugs in the dmd inline assembler, 
 which I am in the process of filing.
Thanks. This helps a lot!
Jan 09 2022
prev sibling next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 1/8/2022 10:04 PM, max haughton wrote:
 For GCC/Clang you'd want -S
I know about that, but take a look at it:
 cat fred.c
int fred(int a[10]) { return a[11]; }
 cc -S test.c
 cat test.s
.file "test.c" .text .globl test .type test, function test: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size test, .-test .ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4" .section .note.GNU-stack,"", progbits ************************************************ Contrast with what -vasm does:
 cat test.d:
int fred(int* a) { return a[11]; }
 dmd -c test.d -vasm
_D4test4fredFPiZi: 0000: 8B 47 2C mov EAX,02Ch[RDI] 0003: C3 ret *********************************************** -vasm gives me what I want to see. There aren't extra steps to getting it, the object code is included, and all the boilerplate is omitted. It's all about the friction.
Jan 09 2022
prev sibling parent Patrick Schluter <Patrick.Schluter bbox.fr> writes:
On Sunday, 9 January 2022 at 06:04:25 UTC, max haughton wrote:
 On Sunday, 9 January 2022 at 02:58:43 UTC, Walter Bright wrote:
 I've never seen one. What's the switch for gcc to do the same 
 thing?
For GCC/Clang you'd want -S (and then -masm=intel to make the output ~~beautiful to nobody but the blind~~ readable).
I prefer -save-temps -fverbose-asm which generates a supplemental .i and .s file without changing the .o file.
Jan 10 2022
prev sibling parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Saturday, 8 January 2022 at 20:50:56 UTC, max haughton wrote:
 Most other compilers have been able to do this for years.
Forever. I have never used a C compiler that doesn't output assembly on request. Pretty much a cultural requirement as C compilers used to pipe asm through a separate assembler.
Jan 09 2022
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 1/8/2022 10:47 AM, Vladimir Marchevsky wrote:
 On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:
 Compile with -vasm to see it! Enjoy!
Any practical reason to put disassembler into compiler instead of making it a separate tool?  Any ETA for renaming it into DMD Burning ROM? :)
https://www.digitalmars.com/ctg/obj2asm.html
Jan 08 2022