summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2011-08-08 18:56:44 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2011-08-08 18:56:44 +0000
commit41ab14b725c8f2bb3e54553d0d7d96ff184786b1 (patch)
tree72f291595c549382084181fd41ac671dbbee62f7 /tools
parentc13464f3c1148a7096356f34f33932d3e258570e (diff)
downloadllvm-41ab14b725c8f2bb3e54553d0d7d96ff184786b1.tar.gz
llvm-41ab14b725c8f2bb3e54553d0d7d96ff184786b1.tar.bz2
llvm-41ab14b725c8f2bb3e54553d0d7d96ff184786b1.tar.xz
Add MCInstrAnalysis class. This allows the targets to specify own versions of MCInstrDescs functions.
- Add overrides for ARM. - Teach llvm-objdump to use this instead of plain MCInstrDesc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@137059 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/llvm-objdump/MCFunction.cpp47
-rw-r--r--tools/llvm-objdump/MCFunction.h4
-rw-r--r--tools/llvm-objdump/llvm-objdump.cpp6
3 files changed, 26 insertions, 31 deletions
diff --git a/tools/llvm-objdump/MCFunction.cpp b/tools/llvm-objdump/MCFunction.cpp
index 66c6c4cba4..418e9d050c 100644
--- a/tools/llvm-objdump/MCFunction.cpp
+++ b/tools/llvm-objdump/MCFunction.cpp
@@ -17,6 +17,7 @@
#include "llvm/MC/MCDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstPrinter.h"
+#include "llvm/MC/MCInstrAnalysis.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/Support/MemoryObject.h"
@@ -28,7 +29,7 @@ using namespace llvm;
MCFunction
MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
const MemoryObject &Region, uint64_t Start,
- uint64_t End, const MCInstrInfo *InstrInfo,
+ uint64_t End, const MCInstrAnalysis *Ana,
raw_ostream &DebugOut) {
std::set<uint64_t> Splits;
Splits.insert(Start);
@@ -40,21 +41,17 @@ MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
MCInst Inst;
if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut)) {
- const MCInstrDesc &Desc = InstrInfo->get(Inst.getOpcode());
- if (Desc.isBranch()) {
- if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
- int64_t Imm = Inst.getOperand(0).getImm();
- // FIXME: Distinguish relocations from nop jumps.
- if (Imm != 0) {
- if (Index+Imm+Size >= End) {
- Instructions.push_back(MCDecodedInst(Index, Size, Inst));
- continue; // Skip branches that leave the function.
- }
- Splits.insert(Index+Imm+Size);
- }
+ if (Ana->isBranch(Inst)) {
+ uint64_t targ = Ana->evaluateBranch(Inst, Index, Size);
+ // FIXME: Distinguish relocations from nop jumps.
+ if (targ != -1ULL && (targ == Index+Size || targ >= End)) {
+ Instructions.push_back(MCDecodedInst(Index, Size, Inst));
+ continue; // Skip branches that leave the function.
}
+ if (targ != -1ULL)
+ Splits.insert(targ);
Splits.insert(Index+Size);
- } else if (Desc.isReturn()) {
+ } else if (Ana->isReturn(Inst)) {
Splits.insert(Index+Size);
}
@@ -90,26 +87,22 @@ MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
MCBasicBlock &BB = i->second;
if (BB.getInsts().empty()) continue;
const MCDecodedInst &Inst = BB.getInsts().back();
- const MCInstrDesc &Desc = InstrInfo->get(Inst.Inst.getOpcode());
- if (Desc.isBranch()) {
- // PCRel branch, we know the destination.
- if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
- int64_t Imm = Inst.Inst.getOperand(0).getImm();
- if (Imm != 0)
- BB.addSucc(&f.getBlockAtAddress(Inst.Address+Inst.Size+Imm));
- // Conditional branches can also fall through to the next block.
- if (Desc.isConditionalBranch() && llvm::next(i) != e)
- BB.addSucc(&llvm::next(i)->second);
- } else {
+ if (Ana->isBranch(Inst.Inst)) {
+ uint64_t targ = Ana->evaluateBranch(Inst.Inst, Inst.Address, Inst.Size);
+ if (targ == -1ULL) {
// Indirect branch. Bail and add all blocks of the function as a
// successor.
for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i)
BB.addSucc(&i->second);
- }
+ } else if (targ != Inst.Address+Inst.Size)
+ BB.addSucc(&f.getBlockAtAddress(targ));
+ // Conditional branches can also fall through to the next block.
+ if (Ana->isConditionalBranch(Inst.Inst) && llvm::next(i) != e)
+ BB.addSucc(&llvm::next(i)->second);
} else {
// No branch. Fall through to the next block.
- if (!Desc.isReturn() && llvm::next(i) != e)
+ if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e)
BB.addSucc(&llvm::next(i)->second);
}
}
diff --git a/tools/llvm-objdump/MCFunction.h b/tools/llvm-objdump/MCFunction.h
index f156e94011..023ca39183 100644
--- a/tools/llvm-objdump/MCFunction.h
+++ b/tools/llvm-objdump/MCFunction.h
@@ -20,7 +20,7 @@
namespace llvm {
class MCDisassembler;
-class MCInstrInfo;
+class MCInstrAnalysis;
class MemoryObject;
class raw_ostream;
@@ -68,7 +68,7 @@ public:
static MCFunction
createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
const MemoryObject &Region, uint64_t Start, uint64_t End,
- const MCInstrInfo *InstrInfo, raw_ostream &DebugOut);
+ const MCInstrAnalysis *Ana, raw_ostream &DebugOut);
typedef MapTy::iterator iterator;
iterator begin() { return Blocks.begin(); }
diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp
index 2458af478f..4804f596b2 100644
--- a/tools/llvm-objdump/llvm-objdump.cpp
+++ b/tools/llvm-objdump/llvm-objdump.cpp
@@ -165,6 +165,8 @@ static void DisassembleInput(const StringRef &Filename) {
return;
}
const MCInstrInfo *InstrInfo = TheTarget->createMCInstrInfo();
+ OwningPtr<MCInstrAnalysis>
+ InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo));
outs() << '\n';
outs() << Filename
@@ -270,8 +272,8 @@ static void DisassembleInput(const StringRef &Filename) {
// Create CFG and use it for disassembly.
MCFunction f =
MCFunction::createFunctionFromMC(Symbols[si].second, DisAsm.get(),
- memoryObject, Start, End, InstrInfo,
- DebugOut);
+ memoryObject, Start, End,
+ InstrAnalysis.get(), DebugOut);
for (MCFunction::iterator fi = f.begin(), fe = f.end(); fi != fe; ++fi){
bool hasPreds = false;