Skip to content

Commit df277ec

Browse files
committed
[X86][MC] Fix the bug of -output-asm-variant=1 for intel syntax
Before this patch ``` $ echo "leal (,%r15), %eax" | llvm-mc --show-encoding --output-asm-variant=1 lea eax, [r15] # encoding: [0x42,0x8d,0x04,0x3d,0x00,0x00,0x00,0x00] $ echo "lea eax, [r15]" | llvm-mc --show-encoding -x86-asm-syntax=intel --output-asm-variant=1 lea eax, [r15] # encoding: [0x41,0x8d,0x07] ``` MC printed the register r15 as a base in intel syntax even when it's an index. Then we got a different encoding by using the assembly from the output of the first command. I believe the behavior is too weird to be called a feature. After this patch, we get ``` $ echo "leal (,%r15), %eax" | llvm-mc --show-encoding --output-asm-variant=1 lea eax, [1*r15] # encoding: [0x42,0x8d,0x04,0x3d,0x00,0x00,0x00,0x00] ``` Reviewed By: RKSimon, pengfei, MaskRay Differential Revision: https://reviews.llvm.org/D144183
1 parent 7495a2e commit df277ec

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

llvm/lib/Target/X86/MCTargetDesc/X86IntelInstPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
398398

399399
if (IndexReg.getReg()) {
400400
if (NeedPlus) O << " + ";
401-
if (ScaleVal != 1)
401+
if (ScaleVal != 1 || !BaseReg.getReg())
402402
O << ScaleVal << '*';
403403
printOperand(MI, Op+X86::AddrIndexReg, O);
404404
NeedPlus = true;

llvm/test/MC/Disassembler/X86/intel-syntax.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,11 @@
171171
# CHECK: lea rcx, [rsp + 4]
172172
0x48 0x8d 0x4c 0x24 0x04
173173

174+
# CHECK: lea rcx, [1*rax]
175+
0x48 0x8d 0x0c 0x05 0x00 0x00 0x00 0x00
174176

177+
# CHECK: lea rcx, [1*rax + 4]
178+
0x48 0x8d 0x0c 0x05 0x04 0x00 0x00 0x00
179+
180+
# CHECK: lea rcx, [2*rax]
181+
0x48 0x8d 0x0c 0x45 0x00 0x00 0x00 0x00

0 commit comments

Comments
 (0)