digitalmars.D.announce - DMD now incorporates a disassembler
- Walter Bright (14/14) Jan 07 2022 Compile with -vasm to see it! Enjoy!
- dd (2/3) Jan 07 2022 Oh very nice! I need to finish my debugger/disassembler project.
- Dukc (6/20) Jan 07 2022 Wow, very useful! This feature surely lowers the bar to check the
- max haughton (3/31) Jan 09 2022 https://stackoverflow.com/questions/2511018/how-does-objdump-manage-to-d...
- Walter Bright (3/4) Jan 09 2022 obj2asm does the same thing:
- Elronnd (2/3) Jan 07 2022 Feature request: octal.
- Walter Bright (2/6) Jan 08 2022 I buried my PDP-11 long ago. Sob.
- rikki cattermole (5/6) Jan 08 2022 There is a kit for the control panel[0].
- Brian Callahan (5/19) Jan 07 2022 Thanks Walter. This is quite useful. Will put it through its
- Walter Bright (2/3) Jan 08 2022 Welcs. I'm already productively using it myself.
- ag0aep6g (3/4) Jan 07 2022 With feature creep in full swing now, when can I expect to read my email...
- H. S. Teoh (7/12) Jan 07 2022 You already can:
- Walter Bright (3/4) Jan 08 2022 The real question is why doesn't your email reader have an option to dis...
- Imperatorn (2/16) Jan 08 2022 Nice!
- Andre Pany (4/18) Jan 08 2022 Great news, this worth a hn / reddit post!
- jmh530 (3/5) Jan 08 2022 Would make a nice project for someone to integrate this into
- Steven Schveighoffer (3/9) Jan 08 2022 Isn't there already an ASM button?
- max haughton (3/13) Jan 08 2022 Yup.
- H. S. Teoh (8/17) Jan 08 2022 [...]
- Vladimir Marchevsky (4/5) Jan 08 2022 Any practical reason to put disassembler into compiler instead of
- max haughton (85/90) Jan 08 2022 Most other compilers have been able to do this for years. The
- Walter Bright (5/25) Jan 08 2022 Compilers that take a detour through an assembler to generate code are
- max haughton (84/92) Jan 08 2022 For GCC/Clang you'd want -S (and then -masm=intel to make the
- Walter Bright (2/10) Jan 09 2022 Thanks. This helps a lot!
- Walter Bright (40/46) Jan 09 2022 int fred(int a[10])
- Patrick Schluter (3/10) Jan 10 2022 I prefer -save-temps -fverbose-asm which generates a supplemental
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (5/6) Jan 09 2022 Forever.
- Walter Bright (2/7) Jan 08 2022 https://www.digitalmars.com/ctg/obj2asm.html
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
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
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/13447Wow, 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
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:https://stackoverflow.com/questions/2511018/how-does-objdump-manage-to-display-source-code-with-the-s-option EnjoyCompile 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/13447Wow, 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 09 2022
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-optionobj2asm does the same thing: https://www.digitalmars.com/ctg/obj2asm.html
Jan 09 2022
On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:0000: 89 F8 mov EAX,EDIFeature request: octal.
Jan 07 2022
On 1/7/2022 4:43 PM, Elronnd wrote:On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:I buried my PDP-11 long ago. Sob.0000: 89 F8 mov EAX,EDIFeature request: octal.
Jan 08 2022
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
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/13447Thanks 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
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
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
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: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 WallCompile 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
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
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/13447Nice!
Jan 08 2022
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/13447Great news, this worth a hn / reddit post! Kind regards Andre
Jan 08 2022
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
On 1/8/22 12:23 PM, jmh530 wrote:On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:Isn't there already an ASM button? -SteveCompile with -vasm to see it! Enjoy! [snip]Would make a nice project for someone to integrate this into run.dlang.org
Jan 08 2022
On Saturday, 8 January 2022 at 18:08:27 UTC, Steven Schveighoffer wrote:On 1/8/22 12:23 PM, jmh530 wrote:Yup.On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:Isn't there already an ASM button? -SteveCompile with -vasm to see it! Enjoy! [snip]Would make a nice project for someone to integrate this into run.dlang.org
Jan 08 2022
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.orgIsn'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
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
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: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.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
On 1/8/2022 12:50 PM, max haughton wrote:On Saturday, 8 January 2022 at 18:47:11 UTC, Vladimir Marchevsky wrote:I've never seen one. What's the switch for gcc to do the same thing?On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:Most other compilers have been able to do this for years.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? :)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
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 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.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
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
On 1/8/2022 10:04 PM, max haughton wrote:For GCC/Clang you'd want -SI know about that, but take a look at it:cat fred.cint 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
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 prefer -save-temps -fverbose-asm which generates a supplemental .i and .s file without changing the .o file.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).
Jan 10 2022
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
On 1/8/2022 10:47 AM, Vladimir Marchevsky wrote:On Friday, 7 January 2022 at 21:41:55 UTC, Walter Bright wrote:https://www.digitalmars.com/ctg/obj2asm.htmlCompile 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