x86asm: disassemble CMP instruction's arguments in the opposite order

That way it matches what the compiler's -S flag generates, and what we write
in assembly.

  CMP AX, $16
  JLE foo

should get to foo if AX <= 16. Without this CL, the disassembly looks like

  CMP $16, AX
  JLE foo

which reads like we should get to foo if 16 <= AX, which is not what these
two instructions actually do.

It was originally this way because the CMP instruction parallels the SUB
instruction, except it throws away the non-flags result. We write that
subtraction as

  SUB $16, AX  // AX <- AX-16

but we don't need to match the SUB's disassembly order, as CMP is not
writing to a register output.

Update golang/go#60920
(This fixes the underlying issue, but the actual "fixes" comment needs to go
on the CL that vendors x/arch containing this CL into the main branch.)

Change-Id: Ifa8d3878453d6e33ae144bfdb01b34171c2106a1
Reviewed-on: https://go-review.googlesource.com/c/arch/+/505375
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
diff --git a/x86/x86asm/plan9x.go b/x86/x86asm/plan9x.go
index de41794..9e866d8 100644
--- a/x86/x86asm/plan9x.go
+++ b/x86/x86asm/plan9x.go
@@ -83,6 +83,12 @@
 		}
 	}
 
+	if inst.Op == CMP {
+		// Use reads-left-to-right ordering for comparisons.
+		// See issue 60920.
+		args[0], args[1] = args[1], args[0]
+	}
+
 	if args != nil {
 		op += " " + strings.Join(args, ", ")
 	}
diff --git a/x86/x86asm/testdata/decode.txt b/x86/x86asm/testdata/decode.txt
index cbd536a..af840c2 100644
--- a/x86/x86asm/testdata/decode.txt
+++ b/x86/x86asm/testdata/decode.txt
@@ -2125,35 +2125,35 @@
 37|11223344556677885f5f5f5f5f5f5f	64	intel	error: unrecognized instruction
 37|11223344556677885f5f5f5f5f5f5f	64	plan9	error: unrecognized instruction
 3811|223344556677885f5f5f5f5f5f5f	32	intel	cmp byte ptr [ecx], dl
-3811|223344556677885f5f5f5f5f5f5f	32	plan9	CMPB DL, 0(CX)
+3811|223344556677885f5f5f5f5f5f5f	32	plan9	CMPB 0(CX), DL
 3811|223344556677885f5f5f5f5f5f5f	64	gnu	cmp %dl,(%rcx)
 3811|223344556677885f5f5f5f5f5f5f	64	intel	cmp byte ptr [rcx], dl
-3811|223344556677885f5f5f5f5f5f5f	64	plan9	CMPB DL, 0(CX)
+3811|223344556677885f5f5f5f5f5f5f	64	plan9	CMPB 0(CX), DL
 3911|223344556677885f5f5f5f5f5f5f	32	intel	cmp dword ptr [ecx], edx
-3911|223344556677885f5f5f5f5f5f5f	32	plan9	CMPL DX, 0(CX)
+3911|223344556677885f5f5f5f5f5f5f	32	plan9	CMPL 0(CX), DX
 3911|223344556677885f5f5f5f5f5f5f	64	gnu	cmp %edx,(%rcx)
 3911|223344556677885f5f5f5f5f5f5f	64	intel	cmp dword ptr [rcx], edx
-3911|223344556677885f5f5f5f5f5f5f	64	plan9	CMPL DX, 0(CX)
+3911|223344556677885f5f5f5f5f5f5f	64	plan9	CMPL 0(CX), DX
 3a11|223344556677885f5f5f5f5f5f5f	32	intel	cmp dl, byte ptr [ecx]
-3a11|223344556677885f5f5f5f5f5f5f	32	plan9	CMPB 0(CX), DL
+3a11|223344556677885f5f5f5f5f5f5f	32	plan9	CMPB DL, 0(CX)
 3a11|223344556677885f5f5f5f5f5f5f	64	gnu	cmp (%rcx),%dl
 3a11|223344556677885f5f5f5f5f5f5f	64	intel	cmp dl, byte ptr [rcx]
-3a11|223344556677885f5f5f5f5f5f5f	64	plan9	CMPB 0(CX), DL
+3a11|223344556677885f5f5f5f5f5f5f	64	plan9	CMPB DL, 0(CX)
 3b11|223344556677885f5f5f5f5f5f5f	32	intel	cmp edx, dword ptr [ecx]
-3b11|223344556677885f5f5f5f5f5f5f	32	plan9	CMPL 0(CX), DX
+3b11|223344556677885f5f5f5f5f5f5f	32	plan9	CMPL DX, 0(CX)
 3b11|223344556677885f5f5f5f5f5f5f	64	gnu	cmp (%rcx),%edx
 3b11|223344556677885f5f5f5f5f5f5f	64	intel	cmp edx, dword ptr [rcx]
-3b11|223344556677885f5f5f5f5f5f5f	64	plan9	CMPL 0(CX), DX
+3b11|223344556677885f5f5f5f5f5f5f	64	plan9	CMPL DX, 0(CX)
 3c11|223344556677885f5f5f5f5f5f5f	32	intel	cmp al, 0x11
-3c11|223344556677885f5f5f5f5f5f5f	32	plan9	CMPL $0x11, AL
+3c11|223344556677885f5f5f5f5f5f5f	32	plan9	CMPL AL, $0x11
 3c11|223344556677885f5f5f5f5f5f5f	64	gnu	cmp $0x11,%al
 3c11|223344556677885f5f5f5f5f5f5f	64	intel	cmp al, 0x11
-3c11|223344556677885f5f5f5f5f5f5f	64	plan9	CMPL $0x11, AL
+3c11|223344556677885f5f5f5f5f5f5f	64	plan9	CMPL AL, $0x11
 3d11223344|556677885f5f5f5f5f5f5f	32	intel	cmp eax, 0x44332211
-3d11223344|556677885f5f5f5f5f5f5f	32	plan9	CMPL $0x44332211, AX
+3d11223344|556677885f5f5f5f5f5f5f	32	plan9	CMPL AX, $0x44332211
 3d11223344|556677885f5f5f5f5f5f5f	64	gnu	cmp $0x44332211,%eax
 3d11223344|556677885f5f5f5f5f5f5f	64	intel	cmp eax, 0x44332211
-3d11223344|556677885f5f5f5f5f5f5f	64	plan9	CMPL $0x44332211, AX
+3d11223344|556677885f5f5f5f5f5f5f	64	plan9	CMPL AX, $0x44332211
 3e67e011|223344556677885f5f5f5f5f	32	intel	addr16 loopne .+0x11
 3e67e011|223344556677885f5f5f5f5f	32	plan9	LOOPNE .+17
 3e67e011|223344556677885f5f5f5f5f	64	gnu	loopne,pt .+0x11
@@ -2482,13 +2482,13 @@
 483511223344|556677885f5f5f5f5f5f	64	plan9	XORQ $0x44332211, AX
 483911|223344556677885f5f5f5f5f5f	64	gnu	cmp %rdx,(%rcx)
 483911|223344556677885f5f5f5f5f5f	64	intel	cmp qword ptr [rcx], rdx
-483911|223344556677885f5f5f5f5f5f	64	plan9	CMPQ DX, 0(CX)
+483911|223344556677885f5f5f5f5f5f	64	plan9	CMPQ 0(CX), DX
 483b11|223344556677885f5f5f5f5f5f	64	gnu	cmp (%rcx),%rdx
 483b11|223344556677885f5f5f5f5f5f	64	intel	cmp rdx, qword ptr [rcx]
-483b11|223344556677885f5f5f5f5f5f	64	plan9	CMPQ 0(CX), DX
+483b11|223344556677885f5f5f5f5f5f	64	plan9	CMPQ DX, 0(CX)
 483d11223344|556677885f5f5f5f5f5f	64	gnu	cmp $0x44332211,%rax
 483d11223344|556677885f5f5f5f5f5f	64	intel	cmp rax, 0x44332211
-483d11223344|556677885f5f5f5f5f5f	64	plan9	CMPQ $0x44332211, AX
+483d11223344|556677885f5f5f5f5f5f	64	plan9	CMPQ AX, $0x44332211
 4850|11223344556677885f5f5f5f5f5f	64	gnu	push %rax
 4850|11223344556677885f5f5f5f5f5f	64	intel	push rax
 4850|11223344556677885f5f5f5f5f5f	64	plan9	PUSHQ AX
@@ -2536,7 +2536,7 @@
 48813011223344|556677885f5f5f5f5f	64	plan9	XORQ $0x44332211, 0(AX)
 48813811223344|556677885f5f5f5f5f	64	gnu	cmpq $0x44332211,(%rax)
 48813811223344|556677885f5f5f5f5f	64	intel	cmp qword ptr [rax], 0x44332211
-48813811223344|556677885f5f5f5f5f	64	plan9	CMPQ $0x44332211, 0(AX)
+48813811223344|556677885f5f5f5f5f	64	plan9	CMPQ 0(AX), $0x44332211
 48830011|223344556677885f5f5f5f5f	64	gnu	addq $0x11,(%rax)
 48830011|223344556677885f5f5f5f5f	64	intel	add qword ptr [rax], 0x11
 48830011|223344556677885f5f5f5f5f	64	plan9	ADDQ $0x11, 0(AX)
@@ -2560,7 +2560,7 @@
 48833011|223344556677885f5f5f5f5f	64	plan9	XORQ $0x11, 0(AX)
 48833811|223344556677885f5f5f5f5f	64	gnu	cmpq $0x11,(%rax)
 48833811|223344556677885f5f5f5f5f	64	intel	cmp qword ptr [rax], 0x11
-48833811|223344556677885f5f5f5f5f	64	plan9	CMPQ $0x11, 0(AX)
+48833811|223344556677885f5f5f5f5f	64	plan9	CMPQ 0(AX), $0x11
 488511|223344556677885f5f5f5f5f5f	64	gnu	test %rdx,(%rcx)
 488511|223344556677885f5f5f5f5f5f	64	intel	test qword ptr [rcx], rdx
 488511|223344556677885f5f5f5f5f5f	64	plan9	TESTQ DX, 0(CX)
@@ -4233,20 +4233,20 @@
 66351122|3344556677885f5f5f5f5f5f	64	intel	xor ax, 0x2211
 66351122|3344556677885f5f5f5f5f5f	64	plan9	XORW $0x2211, AX
 663911|223344556677885f5f5f5f5f5f	32	intel	cmp word ptr [ecx], dx
-663911|223344556677885f5f5f5f5f5f	32	plan9	CMPW DX, 0(CX)
+663911|223344556677885f5f5f5f5f5f	32	plan9	CMPW 0(CX), DX
 663911|223344556677885f5f5f5f5f5f	64	gnu	cmp %dx,(%rcx)
 663911|223344556677885f5f5f5f5f5f	64	intel	cmp word ptr [rcx], dx
-663911|223344556677885f5f5f5f5f5f	64	plan9	CMPW DX, 0(CX)
+663911|223344556677885f5f5f5f5f5f	64	plan9	CMPW 0(CX), DX
 663b11|223344556677885f5f5f5f5f5f	32	intel	cmp dx, word ptr [ecx]
-663b11|223344556677885f5f5f5f5f5f	32	plan9	CMPW 0(CX), DX
+663b11|223344556677885f5f5f5f5f5f	32	plan9	CMPW DX, 0(CX)
 663b11|223344556677885f5f5f5f5f5f	64	gnu	cmp (%rcx),%dx
 663b11|223344556677885f5f5f5f5f5f	64	intel	cmp dx, word ptr [rcx]
-663b11|223344556677885f5f5f5f5f5f	64	plan9	CMPW 0(CX), DX
+663b11|223344556677885f5f5f5f5f5f	64	plan9	CMPW DX, 0(CX)
 663d1122|3344556677885f5f5f5f5f5f	32	intel	cmp ax, 0x2211
-663d1122|3344556677885f5f5f5f5f5f	32	plan9	CMPW $0x2211, AX
+663d1122|3344556677885f5f5f5f5f5f	32	plan9	CMPW AX, $0x2211
 663d1122|3344556677885f5f5f5f5f5f	64	gnu	cmp $0x2211,%ax
 663d1122|3344556677885f5f5f5f5f5f	64	intel	cmp ax, 0x2211
-663d1122|3344556677885f5f5f5f5f5f	64	plan9	CMPW $0x2211, AX
+663d1122|3344556677885f5f5f5f5f5f	64	plan9	CMPW AX, $0x2211
 6640|11223344556677885f5f5f5f5f5f	32	intel	inc ax
 6640|11223344556677885f5f5f5f5f5f	32	plan9	INCW AX
 66480f3a161122|3344556677885f5f5f	64	gnu	pextrq $0x22,%xmm2,(%rcx)
@@ -4343,10 +4343,10 @@
 6681301122|3344556677885f5f5f5f5f	64	intel	xor word ptr [rax], 0x2211
 6681301122|3344556677885f5f5f5f5f	64	plan9	XORW $0x2211, 0(AX)
 6681381122|3344556677885f5f5f5f5f	32	intel	cmp word ptr [eax], 0x2211
-6681381122|3344556677885f5f5f5f5f	32	plan9	CMPW $0x2211, 0(AX)
+6681381122|3344556677885f5f5f5f5f	32	plan9	CMPW 0(AX), $0x2211
 6681381122|3344556677885f5f5f5f5f	64	gnu	cmpw $0x2211,(%rax)
 6681381122|3344556677885f5f5f5f5f	64	intel	cmp word ptr [rax], 0x2211
-6681381122|3344556677885f5f5f5f5f	64	plan9	CMPW $0x2211, 0(AX)
+6681381122|3344556677885f5f5f5f5f	64	plan9	CMPW 0(AX), $0x2211
 66830011|223344556677885f5f5f5f5f	32	intel	add word ptr [eax], 0x11
 66830011|223344556677885f5f5f5f5f	32	plan9	ADDW $0x11, 0(AX)
 66830011|223344556677885f5f5f5f5f	64	gnu	addw $0x11,(%rax)
@@ -4383,10 +4383,10 @@
 66833011|223344556677885f5f5f5f5f	64	intel	xor word ptr [rax], 0x11
 66833011|223344556677885f5f5f5f5f	64	plan9	XORW $0x11, 0(AX)
 66833811|223344556677885f5f5f5f5f	32	intel	cmp word ptr [eax], 0x11
-66833811|223344556677885f5f5f5f5f	32	plan9	CMPW $0x11, 0(AX)
+66833811|223344556677885f5f5f5f5f	32	plan9	CMPW 0(AX), $0x11
 66833811|223344556677885f5f5f5f5f	64	gnu	cmpw $0x11,(%rax)
 66833811|223344556677885f5f5f5f5f	64	intel	cmp word ptr [rax], 0x11
-66833811|223344556677885f5f5f5f5f	64	plan9	CMPW $0x11, 0(AX)
+66833811|223344556677885f5f5f5f5f	64	plan9	CMPW 0(AX), $0x11
 668511|223344556677885f5f5f5f5f5f	32	intel	test word ptr [ecx], dx
 668511|223344556677885f5f5f5f5f5f	32	plan9	TESTW DX, 0(CX)
 668511|223344556677885f5f5f5f5f5f	64	gnu	test %dx,(%rcx)
@@ -4959,10 +4959,10 @@
 803011|223344556677885f5f5f5f5f5f	64	intel	xor byte ptr [rax], 0x11
 803011|223344556677885f5f5f5f5f5f	64	plan9	XORB $0x11, 0(AX)
 803811|223344556677885f5f5f5f5f5f	32	intel	cmp byte ptr [eax], 0x11
-803811|223344556677885f5f5f5f5f5f	32	plan9	CMPB $0x11, 0(AX)
+803811|223344556677885f5f5f5f5f5f	32	plan9	CMPB 0(AX), $0x11
 803811|223344556677885f5f5f5f5f5f	64	gnu	cmpb $0x11,(%rax)
 803811|223344556677885f5f5f5f5f5f	64	intel	cmp byte ptr [rax], 0x11
-803811|223344556677885f5f5f5f5f5f	64	plan9	CMPB $0x11, 0(AX)
+803811|223344556677885f5f5f5f5f5f	64	plan9	CMPB 0(AX), $0x11
 810011223344|556677885f5f5f5f5f5f	32	intel	add dword ptr [eax], 0x44332211
 810011223344|556677885f5f5f5f5f5f	32	plan9	ADDL $0x44332211, 0(AX)
 810011223344|556677885f5f5f5f5f5f	64	gnu	addl $0x44332211,(%rax)
@@ -4999,10 +4999,10 @@
 813011223344|556677885f5f5f5f5f5f	64	intel	xor dword ptr [rax], 0x44332211
 813011223344|556677885f5f5f5f5f5f	64	plan9	XORL $0x44332211, 0(AX)
 813811223344|556677885f5f5f5f5f5f	32	intel	cmp dword ptr [eax], 0x44332211
-813811223344|556677885f5f5f5f5f5f	32	plan9	CMPL $0x44332211, 0(AX)
+813811223344|556677885f5f5f5f5f5f	32	plan9	CMPL 0(AX), $0x44332211
 813811223344|556677885f5f5f5f5f5f	64	gnu	cmpl $0x44332211,(%rax)
 813811223344|556677885f5f5f5f5f5f	64	intel	cmp dword ptr [rax], 0x44332211
-813811223344|556677885f5f5f5f5f5f	64	plan9	CMPL $0x44332211, 0(AX)
+813811223344|556677885f5f5f5f5f5f	64	plan9	CMPL 0(AX), $0x44332211
 830011|223344556677885f5f5f5f5f5f	32	intel	add dword ptr [eax], 0x11
 830011|223344556677885f5f5f5f5f5f	32	plan9	ADDL $0x11, 0(AX)
 830011|223344556677885f5f5f5f5f5f	64	gnu	addl $0x11,(%rax)
@@ -5039,10 +5039,10 @@
 833011|223344556677885f5f5f5f5f5f	64	intel	xor dword ptr [rax], 0x11
 833011|223344556677885f5f5f5f5f5f	64	plan9	XORL $0x11, 0(AX)
 833811|223344556677885f5f5f5f5f5f	32	intel	cmp dword ptr [eax], 0x11
-833811|223344556677885f5f5f5f5f5f	32	plan9	CMPL $0x11, 0(AX)
+833811|223344556677885f5f5f5f5f5f	32	plan9	CMPL 0(AX), $0x11
 833811|223344556677885f5f5f5f5f5f	64	gnu	cmpl $0x11,(%rax)
 833811|223344556677885f5f5f5f5f5f	64	intel	cmp dword ptr [rax], 0x11
-833811|223344556677885f5f5f5f5f5f	64	plan9	CMPL $0x11, 0(AX)
+833811|223344556677885f5f5f5f5f5f	64	plan9	CMPL 0(AX), $0x11
 8411|223344556677885f5f5f5f5f5f5f	32	intel	test byte ptr [ecx], dl
 8411|223344556677885f5f5f5f5f5f5f	32	plan9	TESTB DL, 0(CX)
 8411|223344556677885f5f5f5f5f5f5f	64	gnu	test %dl,(%rcx)