Add support for 'objdump -print-imm-hex' for imm64, operand imm and branch target. If user programs encode immediate values as hex numbers, such an option will make it easy to correlate asm insns with source code. This option also makes it easy to correlate imm values with insn encoding. There is one changed behavior in this patch. In old way, we print the 64bit imm as u64: O << (uint64_t)Op.getImm(); and the new way is: O << formatImm(Op.getImm()); The formatImm is defined in llvm/MC/MCInstPrinter.h as format_object<int64_t> formatImm(int64_t Value) So the new way to print 64bit imm is i64 type. If a 64bit value has the highest bit set, the old way will print the value as a positive value and the new way will print as a negative value. The new way is consistent with x86_64. For the code (see the test program): ... if (a == 0xABCDABCDabcdabcdULL) ... x86_64 objdump, with and without -print-imm-hex, looks like: 48 b8 cd ab cd ab cd ab cd ab movabsq $-6067004223159161907, %rax 48 b8 cd ab cd ab cd ab cd ab movabsq $-0x5432543254325433, %rax Signed-off-by: Yonghong Song <yhs@fb.com> llvm-svn: 321215
110 lines
3.2 KiB
C++
110 lines
3.2 KiB
C++
//===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This class prints an BPF MCInst to a .s file.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "BPFInstPrinter.h"
|
|
#include "BPF.h"
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
#include "llvm/MC/MCExpr.h"
|
|
#include "llvm/MC/MCInst.h"
|
|
#include "llvm/MC/MCSymbol.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
#include "llvm/Support/FormattedStream.h"
|
|
using namespace llvm;
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
// Include the auto-generated portion of the assembly writer.
|
|
#include "BPFGenAsmWriter.inc"
|
|
|
|
void BPFInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
|
|
StringRef Annot, const MCSubtargetInfo &STI) {
|
|
printInstruction(MI, O);
|
|
printAnnotation(O, Annot);
|
|
}
|
|
|
|
static void printExpr(const MCExpr *Expr, raw_ostream &O) {
|
|
#ifndef NDEBUG
|
|
const MCSymbolRefExpr *SRE;
|
|
|
|
if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
|
|
SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
|
|
else
|
|
SRE = dyn_cast<MCSymbolRefExpr>(Expr);
|
|
assert(SRE && "Unexpected MCExpr type.");
|
|
|
|
MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
|
|
|
|
assert(Kind == MCSymbolRefExpr::VK_None);
|
|
#endif
|
|
O << *Expr;
|
|
}
|
|
|
|
void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
|
|
raw_ostream &O, const char *Modifier) {
|
|
assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
if (Op.isReg()) {
|
|
O << getRegisterName(Op.getReg());
|
|
} else if (Op.isImm()) {
|
|
O << formatImm((int32_t)Op.getImm());
|
|
} else {
|
|
assert(Op.isExpr() && "Expected an expression");
|
|
printExpr(Op.getExpr(), O);
|
|
}
|
|
}
|
|
|
|
void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
|
|
const char *Modifier) {
|
|
const MCOperand &RegOp = MI->getOperand(OpNo);
|
|
const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
|
|
|
|
// register
|
|
assert(RegOp.isReg() && "Register operand not a register");
|
|
O << getRegisterName(RegOp.getReg());
|
|
|
|
// offset
|
|
if (OffsetOp.isImm()) {
|
|
auto Imm = OffsetOp.getImm();
|
|
if (Imm >= 0)
|
|
O << " + " << formatImm(Imm);
|
|
else
|
|
O << " - " << formatImm(-Imm);
|
|
} else {
|
|
assert(0 && "Expected an immediate");
|
|
}
|
|
}
|
|
|
|
void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
|
|
raw_ostream &O) {
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
if (Op.isImm())
|
|
O << formatImm(Op.getImm());
|
|
else if (Op.isExpr())
|
|
printExpr(Op.getExpr(), O);
|
|
else
|
|
O << Op;
|
|
}
|
|
|
|
void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
|
|
raw_ostream &O) {
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
if (Op.isImm()) {
|
|
int16_t Imm = Op.getImm();
|
|
O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
|
|
} else if (Op.isExpr()) {
|
|
printExpr(Op.getExpr(), O);
|
|
} else {
|
|
O << Op;
|
|
}
|
|
}
|