Skip to content

Commit

Permalink
Visibly fail if attempting to encode register AH,BH,CH,DH in a REX-pr…
Browse files Browse the repository at this point in the history
…efixed instruction.

Differential Revision: http://reviews.llvm.org/D13316
Fixes PR25003

llvm-svn: 252743
  • Loading branch information
snuglas committed Nov 11, 2015
1 parent 8fbd938 commit a140397
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
Expand Up @@ -988,6 +988,8 @@ void X86MCCodeEmitter::EmitVEXOpcodePrefix(uint64_t TSFlags, unsigned &CurByte,
static unsigned DetermineREXPrefix(const MCInst &MI, uint64_t TSFlags,
const MCInstrDesc &Desc) {
unsigned REX = 0;
bool UsesHighByteReg = false;

if (TSFlags & X86II::REX_W)
REX |= 1 << 3; // set REX.W

Expand All @@ -1004,6 +1006,8 @@ static unsigned DetermineREXPrefix(const MCInst &MI, uint64_t TSFlags,
const MCOperand &MO = MI.getOperand(i);
if (!MO.isReg()) continue;
unsigned Reg = MO.getReg();
if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH)
UsesHighByteReg = true;
if (!X86II::isX86_64NonExtLowByteReg(Reg)) continue;
// FIXME: The caller of DetermineREXPrefix slaps this prefix onto anything
// that returns non-zero.
Expand Down Expand Up @@ -1073,6 +1077,9 @@ static unsigned DetermineREXPrefix(const MCInst &MI, uint64_t TSFlags,
}
break;
}
if (REX && UsesHighByteReg)
report_fatal_error("Cannot encode high byte register in REX-prefixed instruction");

return REX;
}

Expand Down
3 changes: 3 additions & 0 deletions llvm/test/MC/X86/encoder-fail.s
@@ -0,0 +1,3 @@
// RUN: not llvm-mc -triple x86_64-unknown-unknown --show-encoding %s 2>&1 | FileCheck %s
// CHECK: LLVM ERROR: Cannot encode high byte register in REX-prefixed instruction
movzx %dh, %rsi

0 comments on commit a140397

Please sign in to comment.