Skip to content

Commit

Permalink
Add printer for ADR operands.
Browse files Browse the repository at this point in the history
The ADR immediate operand is PC relative. But it misses its own printing function
(in comparision to ADRP for example). If during disassembly the immediate was not resolved
to a symbol (which is always the case for CS and llvm-objdump) the immediate is printed as is.
Although it should print Address + Imm.

This case should now be handled in printAdrLabel()
  • Loading branch information
Rot127 committed Aug 28, 2023
1 parent fe79def commit c3484b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions llvm/lib/Target/AArch64/AArch64InstrFormats.td
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ def AdrOperand : AsmOperandClass {
}
def adrlabel : Operand<i64> {
let EncoderMethod = "getAdrLabelOpValue";
let PrintMethod = "printAdrLabel";
let ParserMatchClass = AdrOperand;
let OperandType = "OPERAND_PCREL";
}

class SImmOperand<int width> : AsmOperandClass {
Expand Down
23 changes: 23 additions & 0 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,29 @@ void AArch64InstPrinter::printAlignedLabel(const MCInst *MI, uint64_t Address,
}
}

void AArch64InstPrinter::printAdrLabel(const MCInst *MI, uint64_t Address,
unsigned OpNum,
const MCSubtargetInfo &STI,
raw_ostream &O) {
const MCOperand &Op = MI->getOperand(OpNum);

// If the label has already been resolved to an immediate offset (say, when
// we're running the disassembler), just print the immediate.
if (Op.isImm()) {
const int64_t Offset = Op.getImm();
O << markup("<imm:");
if (PrintBranchImmAsAddress)
O << formatHex((Address & -4096) + Offset);
else
O << "#" << Offset;
O << markup(">");
return;
}

// Otherwise, just print the expression.
MI->getOperand(OpNum).getExpr()->print(O, &MAI);
}

void AArch64InstPrinter::printAdrpLabel(const MCInst *MI, uint64_t Address,
unsigned OpNum,
const MCSubtargetInfo &STI,
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class AArch64InstPrinter : public MCInstPrinter {
const MCSubtargetInfo &STI, raw_ostream &O);
void printMatrixIndex(const MCInst *MI, unsigned OpNum,
const MCSubtargetInfo &STI, raw_ostream &O);
void printAdrLabel(const MCInst *MI, uint64_t Address, unsigned OpNum,
const MCSubtargetInfo &STI, raw_ostream &O);
void printAdrpLabel(const MCInst *MI, uint64_t Address, unsigned OpNum,
const MCSubtargetInfo &STI, raw_ostream &O);
void printBarrierOption(const MCInst *MI, unsigned OpNum,
Expand Down

0 comments on commit c3484b1

Please sign in to comment.