DMD outputs xchg RDX, RAX when you ask for xchg RDX, R8 /// CODE: ulong bug(ulong a, ulong b, ulong c) { version (D_InlineAsm_X86_64) { // RDI = c RSI = b RDX = a // RAX = return value // Scratch: RAX, RCX, R8, R9 version (linux) asm { naked; // Let's return c using iasm mov RAX, RDI; // DMD outputs: REX.W+90+rd XCHG r64, RAX // instead of: REX.W+87/r XCHG r/m64,r64 xchg RDX, R8; // Interestingly, DMD outputs: // xchg RDX,RDI // and xchg RDX,R9 correctly ret; } } } ulong nobug(ulong a, ulong b, ulong c) { version (D_InlineAsm_X86_64) { version (linux) asm { naked; mov RAX, RDI; // Let's return c using iasm db 0x4c; db 0x87; db 0xC2; // xchg RDX, R8; ret; } } } void main() { import std.stdio; writeln(bug(1, 2, 3)); // ERROR: outputs 1 writeln(nobug(1, 2, 3)); // outputs 3 as it should assert(bug(1, 2, 3) == 3);// should pass but fails }
Provided I understand the code correctly, I believe the offending code is backend/cod3.c line 5128. Now I just have to figure out why it's reaching there.
Well, it appears I don't understand the right format to get it to auto-reference the PR here, so I'll do it manually instead. https://github.com/D-Programming-Language/dmd/pull/3690
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/6f013e558501c7b736656bec12ee241bd6f65684 Fix issue 12968 - Incorrect codgen for xchg with R8 https://github.com/D-Programming-Language/dmd/commit/9ddfff545a12d9c3b4df4829e3d4f8c90679cc02 Merge pull request #3690 from Orvid/issue12968 Fix issue 12968 - incorrect codegen
(In reply to Orvid King from comment #2) > Well, it appears I don't understand the right format to get it to > auto-reference the PR here, so I'll do it manually instead. > > https://github.com/D-Programming-Language/dmd/pull/3690 There is no auto reference, only the message when the commit is merged.