Skip to content

Commit 5fad05e

Browse files
committed
[MCInstPrinter] Pass Address parameter to MCOI::OPERAND_PCREL typed operands. NFC
Follow-up of D72172 and D72180 This patch passes `uint64_t Address` to print methods of PC-relative operands so that subsequent target specific patches can change `*InstPrinter::print{Operand,PCRelImm,...}` to customize the output. Add MCInstPrinter::PrintBranchImmAsAddress which is set to true by llvm-objdump. ``` // Current llvm-objdump -d output aarch64: 20000: bl #0 ppc: 20000: bl .+4 x86: 20000: callq 0 // Ideal output aarch64: 20000: bl 0x20000 ppc: 20000: bl 0x20004 x86: 20000: callq 0x20005 // GNU objdump -d. The lack of 0x is not ideal because the result cannot be re-assembled aarch64: 20000: bl 20000 ppc: 20000: bl 0x20004 x86: 20000: callq 20005 ``` In `lib/Target/X86/X86GenAsmWriter1.inc` (generated by `llvm-tblgen -gen-asm-writer`): ``` case 12: // CALL64pcrel32, CALLpcrel16, CALLpcrel32, EH_SjLj_Setup, JCXZ, JECXZ, J... - printPCRelImm(MI, 0, O); + printPCRelImm(MI, Address, 0, O); return; ``` Some targets have 2 `printOperand` overloads, one without `Address` and one with `Address`. They should annotate derived `Operand` properly with `let OperandType = "OPERAND_PCREL"`. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D76574
1 parent b9943d6 commit 5fad05e

File tree

17 files changed

+80
-17
lines changed

17 files changed

+80
-17
lines changed

llvm/include/llvm/MC/MCInstPrinter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class MCInstPrinter {
5858
/// Which style to use for printing hexadecimal values.
5959
HexStyle::Style PrintHexStyle = HexStyle::C;
6060

61+
/// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal
62+
/// address (e.g. bl 0x20004). This is useful for a stream disassembler
63+
/// (llvm-objdump -d).
64+
bool PrintBranchImmAsAddress = false;
65+
6166
/// Utility function for printing annotations.
6267
void printAnnotation(raw_ostream &OS, StringRef Annot);
6368

@@ -100,6 +105,10 @@ class MCInstPrinter {
100105

101106
void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
102107

108+
void setPrintBranchImmAsAddress(bool Value) {
109+
PrintBranchImmAsAddress = Value;
110+
}
111+
103112
/// Utility function to print immediates in decimal or hex.
104113
format_object<int64_t> formatImm(int64_t Value) const {
105114
return PrintImmHex ? formatHex(Value) : formatDec(Value);

llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,8 @@ void AArch64InstPrinter::printVectorIndex(const MCInst *MI, unsigned OpNum,
13471347
O << "[" << MI->getOperand(OpNum).getImm() << "]";
13481348
}
13491349

1350-
void AArch64InstPrinter::printAlignedLabel(const MCInst *MI, unsigned OpNum,
1350+
void AArch64InstPrinter::printAlignedLabel(const MCInst *MI, uint64_t Address,
1351+
unsigned OpNum,
13511352
const MCSubtargetInfo &STI,
13521353
raw_ostream &O) {
13531354
const MCOperand &Op = MI->getOperand(OpNum);
@@ -1362,10 +1363,9 @@ void AArch64InstPrinter::printAlignedLabel(const MCInst *MI, unsigned OpNum,
13621363
// If the branch target is simply an address then print it in hex.
13631364
const MCConstantExpr *BranchTarget =
13641365
dyn_cast<MCConstantExpr>(MI->getOperand(OpNum).getExpr());
1365-
int64_t Address;
1366-
if (BranchTarget && BranchTarget->evaluateAsAbsolute(Address)) {
1367-
O << "0x";
1368-
O.write_hex(Address);
1366+
int64_t TargetAddress;
1367+
if (BranchTarget && BranchTarget->evaluateAsAbsolute(TargetAddress)) {
1368+
O << formatHex(TargetAddress);
13691369
} else {
13701370
// Otherwise, just print the expression.
13711371
MI->getOperand(OpNum).getExpr()->print(O, &MAI);

llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class AArch64InstPrinter : public MCInstPrinter {
100100
const MCSubtargetInfo &STI, raw_ostream &O);
101101
void printInverseCondCode(const MCInst *MI, unsigned OpNum,
102102
const MCSubtargetInfo &STI, raw_ostream &O);
103-
void printAlignedLabel(const MCInst *MI, unsigned OpNum,
103+
void printAlignedLabel(const MCInst *MI, uint64_t Address, unsigned OpNum,
104104
const MCSubtargetInfo &STI, raw_ostream &O);
105105
void printUImm12Offset(const MCInst *MI, unsigned OpNum, unsigned Scale,
106106
raw_ostream &O);

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUInstPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ class AMDGPUInstPrinter : public MCInstPrinter {
115115
raw_ostream &O);
116116
void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
117117
raw_ostream &O);
118+
void printOperand(const MCInst *MI, uint64_t /*Address*/, unsigned OpNum,
119+
const MCSubtargetInfo &STI, raw_ostream &O) {
120+
printOperand(MI, OpNum, STI, O);
121+
}
118122
void printOperandAndFPInputMods(const MCInst *MI, unsigned OpNo,
119123
const MCSubtargetInfo &STI, raw_ostream &O);
120124
void printOperandAndIntInputMods(const MCInst *MI, unsigned OpNo,

llvm/lib/Target/ARC/MCTargetDesc/ARCInstPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class ARCInstPrinter : public MCInstPrinter {
3636
private:
3737
void printMemOperandRI(const MCInst *MI, unsigned OpNum, raw_ostream &O);
3838
void printOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O);
39+
void printOperand(const MCInst *MI, uint64_t /*Address*/, unsigned OpNum,
40+
raw_ostream &O) {
41+
printOperand(MI, OpNum, O);
42+
}
3943
void printPredicateOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O);
4044
void printBRCCPredicateOperand(const MCInst *MI, unsigned OpNum,
4145
raw_ostream &O);

llvm/lib/Target/ARM/MCTargetDesc/ARMInstPrinter.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class ARMInstPrinter : public MCInstPrinter {
4343

4444
void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
4545
raw_ostream &O);
46+
void printOperand(const MCInst *MI, uint64_t /*Address*/, unsigned OpNum,
47+
const MCSubtargetInfo &STI, raw_ostream &O) {
48+
printOperand(MI, OpNum, STI, O);
49+
}
4650

4751
void printSORegRegOperand(const MCInst *MI, unsigned OpNum,
4852
const MCSubtargetInfo &STI, raw_ostream &O);
@@ -109,6 +113,12 @@ class ARMInstPrinter : public MCInstPrinter {
109113
template <unsigned scale>
110114
void printAdrLabelOperand(const MCInst *MI, unsigned OpNum,
111115
const MCSubtargetInfo &STI, raw_ostream &O);
116+
template <unsigned scale>
117+
void printAdrLabelOperand(const MCInst *MI, uint64_t /*Address*/,
118+
unsigned OpNum, const MCSubtargetInfo &STI,
119+
raw_ostream &O) {
120+
printAdrLabelOperand<scale>(MI, OpNum, STI, O);
121+
}
112122
void printThumbS4ImmOperand(const MCInst *MI, unsigned OpNum,
113123
const MCSubtargetInfo &STI, raw_ostream &O);
114124
void printThumbSRImm(const MCInst *MI, unsigned OpNum,
@@ -206,6 +216,11 @@ class ARMInstPrinter : public MCInstPrinter {
206216
const MCSubtargetInfo &STI, raw_ostream &O);
207217
void printThumbLdrLabelOperand(const MCInst *MI, unsigned OpNum,
208218
const MCSubtargetInfo &STI, raw_ostream &O);
219+
void printThumbLdrLabelOperand(const MCInst *MI, uint64_t /*Address*/,
220+
unsigned OpNum, const MCSubtargetInfo &STI,
221+
raw_ostream &O) {
222+
printThumbLdrLabelOperand(MI, OpNum, STI, O);
223+
}
209224
void printFBits16(const MCInst *MI, unsigned OpNum,
210225
const MCSubtargetInfo &STI, raw_ostream &O);
211226
void printFBits32(const MCInst *MI, unsigned OpNum,

llvm/lib/Target/AVR/MCTargetDesc/AVRInstPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class AVRInstPrinter : public MCInstPrinter {
3838

3939
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
4040
void printPCRelImm(const MCInst *MI, unsigned OpNo, raw_ostream &O);
41+
void printPCRelImm(const MCInst *MI, uint64_t /*Address*/, unsigned OpNo,
42+
raw_ostream &O) {
43+
printPCRelImm(MI, OpNo, O);
44+
}
4145
void printMemri(const MCInst *MI, unsigned OpNo, raw_ostream &O);
4246

4347
// Autogenerated by TableGen.

llvm/lib/Target/Mips/MCTargetDesc/MipsInstPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class MipsInstPrinter : public MCInstPrinter {
9292

9393
private:
9494
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
95+
void printOperand(const MCInst *MI, uint64_t /*Address*/, unsigned OpNum,
96+
raw_ostream &O) {
97+
printOperand(MI, OpNum, O);
98+
}
9599
template <unsigned Bits, unsigned Offset = 0>
96100
void printUImm(const MCInst *MI, int opNum, raw_ostream &O);
97101
void printMemOperand(const MCInst *MI, int opNum, raw_ostream &O);

llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class PPCInstPrinter : public MCInstPrinter {
6565
void printU16ImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
6666
void printImmZeroOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
6767
void printBranchOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
68+
void printBranchOperand(const MCInst *MI, uint64_t /*Address*/, unsigned OpNo,
69+
raw_ostream &O) {
70+
printBranchOperand(MI, OpNo, O);
71+
}
6872
void printAbsBranchOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
6973
void printTLSCall(const MCInst *MI, unsigned OpNo, raw_ostream &O);
7074

llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum,
155155
MO.getExpr()->print(O, &MAI);
156156
}
157157

158-
void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI, int OpNum,
158+
void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI,
159+
uint64_t Address, int OpNum,
159160
raw_ostream &O) {
160161
// Output the PC-relative operand.
161162
printPCRelOperand(MI, OpNum, O);

llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class SystemZInstPrinter : public MCInstPrinter {
4646
private:
4747
// Print various types of operand.
4848
void printOperand(const MCInst *MI, int OpNum, raw_ostream &O);
49+
void printOperand(const MCInst *MI, uint64_t /*Address*/, unsigned OpNum,
50+
raw_ostream &O) {
51+
printOperand(MI, OpNum, O);
52+
}
4953
void printBDAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O);
5054
void printBDXAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O);
5155
void printBDLAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O);
@@ -65,7 +69,12 @@ class SystemZInstPrinter : public MCInstPrinter {
6569
void printU32ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O);
6670
void printU48ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O);
6771
void printPCRelOperand(const MCInst *MI, int OpNum, raw_ostream &O);
68-
void printPCRelTLSOperand(const MCInst *MI, int OpNum, raw_ostream &O);
72+
void printPCRelOperand(const MCInst *MI, uint64_t /*Address*/, int OpNum,
73+
raw_ostream &O) {
74+
printPCRelOperand(MI, OpNum, O);
75+
}
76+
void printPCRelTLSOperand(const MCInst *MI, uint64_t Address, int OpNum,
77+
raw_ostream &O);
6978

7079
// Print the mnemonic for a condition-code mask ("ne", "lh", etc.)
7180
// This forms part of the instruction name rather than the operand list.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void X86ATTInstPrinter::printInst(const MCInst *MI, uint64_t Address,
5656
if (MI->getOpcode() == X86::CALLpcrel32 &&
5757
(STI.getFeatureBits()[X86::Mode64Bit])) {
5858
OS << "\tcallq\t";
59-
printPCRelImm(MI, 0, OS);
59+
printPCRelImm(MI, Address, 0, OS);
6060
}
6161
// data16 and data32 both have the same encoding of 0x66. While data32 is
6262
// valid only in 16 bit systems, data16 is valid in the rest.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ void X86InstPrinterCommon::printRoundingControl(const MCInst *MI, unsigned Op,
291291
/// being encoded as a pc-relative value (e.g. for jumps and calls). In
292292
/// Intel-style these print slightly differently than normal immediates.
293293
/// for example, a $ is not emitted.
294-
void X86InstPrinterCommon::printPCRelImm(const MCInst *MI, unsigned OpNo,
295-
raw_ostream &O) {
294+
void X86InstPrinterCommon::printPCRelImm(const MCInst *MI, uint64_t Address,
295+
unsigned OpNo, raw_ostream &O) {
296296
const MCOperand &Op = MI->getOperand(OpNo);
297297
if (Op.isImm())
298298
O << formatImm(Op.getImm());

llvm/lib/Target/X86/MCTargetDesc/X86InstPrinterCommon.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class X86InstPrinterCommon : public MCInstPrinter {
2929
void printVPCMPMnemonic(const MCInst *MI, raw_ostream &OS);
3030
void printCMPMnemonic(const MCInst *MI, bool IsVCmp, raw_ostream &OS);
3131
void printRoundingControl(const MCInst *MI, unsigned Op, raw_ostream &O);
32-
void printPCRelImm(const MCInst *MI, unsigned OpNo, raw_ostream &O);
32+
void printPCRelImm(const MCInst *MI, uint64_t Address, unsigned OpNo,
33+
raw_ostream &O);
34+
3335
protected:
3436
void printInstFlags(const MCInst *MI, raw_ostream &O);
3537
void printOptionalSegReg(const MCInst *MI, unsigned OpNo, raw_ostream &O);

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,7 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
16351635
reportError(Obj->getFileName(),
16361636
"no instruction printer for target " + TripleName);
16371637
IP->setPrintImmHex(PrintImmHex);
1638+
IP->setPrintBranchImmAsAddress(true);
16381639

16391640
PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
16401641
SourcePrinter SP(Obj, TheTarget->getName());

llvm/utils/TableGen/AsmWriterInst.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
3636
return Str;
3737

3838
std::string Result = Str + "(MI";
39+
if (PCRel)
40+
Result += ", Address";
3941
if (MIOpNo != ~0U)
4042
Result += ", " + utostr(MIOpNo);
4143
if (PassSubtarget)
@@ -179,7 +181,9 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
179181
CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo];
180182

181183
unsigned MIOp = OpInfo.MIOperandNo;
182-
Operands.emplace_back(OpInfo.PrinterMethodName, MIOp, Modifier);
184+
Operands.emplace_back(OpInfo.PrinterMethodName, MIOp, Modifier,
185+
AsmWriterOperand::isMachineInstrOperand,
186+
OpInfo.OperandType == "MCOI::OPERAND_PCREL");
183187
}
184188
LastEmitted = VarEnd;
185189
}

llvm/utils/TableGen/AsmWriterInst.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@ namespace llvm {
4848
/// an operand, specified with syntax like ${opname:modifier}.
4949
std::string MiModifier;
5050

51+
bool PCRel = false;
52+
5153
// To make VS STL happy
5254
AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
5355

5456
AsmWriterOperand(const std::string &LitStr,
5557
OpType op = isLiteralTextOperand)
5658
: OperandType(op), Str(LitStr) {}
5759

58-
AsmWriterOperand(const std::string &Printer,
59-
unsigned _MIOpNo,
60+
AsmWriterOperand(const std::string &Printer, unsigned _MIOpNo,
6061
const std::string &Modifier,
61-
OpType op = isMachineInstrOperand)
62-
: OperandType(op), MIOpNo(_MIOpNo), Str(Printer), MiModifier(Modifier) {}
62+
OpType op = isMachineInstrOperand, bool PCRel = false)
63+
: OperandType(op), MIOpNo(_MIOpNo), Str(Printer), MiModifier(Modifier),
64+
PCRel(PCRel) {}
6365

6466
bool operator!=(const AsmWriterOperand &Other) const {
6567
if (OperandType != Other.OperandType || Str != Other.Str) return true;

0 commit comments

Comments
 (0)