3 ms·
With the dmd compiler, compiling with -vasm will show the generated assembly as it compiles. It's been poo-pooed because why not use objdump or -S? But once you
by WalterBright 2y ago
With the dmd compiler, compiling with -vasm will show the generated assembly as it compiles. It's been poo-pooed because why not use objdump or -S? But once you try it, you'll know why it's so convenient, as it just emits the assembler, and not the huge pile of boilerplate needed to make an object file.
For example, I'm working on an AArch64 code generator, more specifically, generating floating point code. I have a function:
float test(float a, float b) { return a * b; }
Compiling it with:
dmd -c test.c -arm -vasm
yields:
test:
0000: A9 BE 7B FD stp x29,x30,[sp,#-32]! // https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#ldstpair_pre
0004: 91 00 03 FD mov x29,sp // https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#addsub_imm
0008: BD 00 0F A0 str s0,[x29,#12] // https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#ldst_pos
000c: B9 40 1B A0 ldr w0,[x29,#0x18] // https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#ldst_pos
0010: 1E 21 08 00 fmul s0,s0,s1 // https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#floatdp2
0014: A8 C2 7B FD ldp x29,x30,[sp],#0x20 // https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#ldstpair_post
0018: D6 5F 03 C0 ret // https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#branch_reg
It emits the address, the hex instruction, the instruction mnemonic, and the URL to the instruction specification.
Yes, I know the code isn't quite correct, did I mention I was working on it? :-)
- WalterBright 2y agoYou might think that the compiler was generating assembler code, and then assembling the code to binary. Nope. It generates the binary instructions directly. The compiler has a builtin disassembler for the display. This makes for a fantastic way to make sure the correct binary is generated. It has saved me an enormous amount of debugging time.