www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12968] New: DMD inline asm outputs wrong instruction

https://issues.dlang.org/show_bug.cgi?id=12968

          Issue ID: 12968
           Summary: DMD inline asm outputs wrong instruction
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: safety0ff.bugz gmail.com

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
}

--
Jun 23 2014