D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12968 - DMD inline asm outputs wrong XCHG instruction
Summary: DMD inline asm outputs wrong XCHG instruction
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 All
: P1 major
Assignee: Orvid King
URL:
Keywords: iasm, wrong-code
Depends on:
Blocks:
 
Reported: 2014-06-23 10:29 UTC by safety0ff.bugz
Modified: 2014-06-27 12:37 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description safety0ff.bugz 2014-06-23 10:29:42 UTC
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
}
Comment 1 Orvid King 2014-06-23 20:03:47 UTC
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.
Comment 2 Orvid King 2014-06-23 20:37:43 UTC
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
Comment 4 yebblies 2014-06-27 12:37:01 UTC
(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.