#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"
MCFunction
MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
const MemoryObject &Region, uint64_t Start,
- uint64_t End, const MCInstrInfo *InstrInfo,
- raw_ostream &DebugOut) {
+ uint64_t End, const MCInstrAnalysis *Ana,
+ raw_ostream &DebugOut,
+ SmallVectorImpl<uint64_t> &Calls) {
+ std::vector<MCDecodedInst> Instructions;
std::set<uint64_t> Splits;
Splits.insert(Start);
- std::vector<MCDecodedInst> Instructions;
uint64_t Size;
+ MCFunction f(Name);
+
+ {
+ DenseSet<uint64_t> VisitedInsts;
+ SmallVector<uint64_t, 16> WorkList;
+ WorkList.push_back(Start);
// Disassemble code and gather basic block split points.
- for (uint64_t Index = Start; Index < End; Index += Size) {
- MCInst Inst;
+ while (!WorkList.empty()) {
+ uint64_t Index = WorkList.pop_back_val();
+ if (VisitedInsts.find(Index) != VisitedInsts.end())
+ continue; // Already visited this location.
- 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);
+ for (;Index < End; Index += Size) {
+ VisitedInsts.insert(Index);
+
+ MCInst Inst;
+ if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut, nulls())){
+ Instructions.push_back(MCDecodedInst(Index, Size, Inst));
+ if (Ana->isBranch(Inst)) {
+ uint64_t targ = Ana->evaluateBranch(Inst, Index, Size);
+ if (targ != -1ULL && targ == Index+Size)
+ continue; // Skip nop jumps.
+
+ // If we could determine the branch target, make a note to start a
+ // new basic block there and add the target to the worklist.
+ if (targ != -1ULL) {
+ Splits.insert(targ);
+ WorkList.push_back(targ);
+ WorkList.push_back(Index+Size);
}
+ Splits.insert(Index+Size);
+ break;
+ } else if (Ana->isReturn(Inst)) {
+ // Return instruction. This basic block ends here.
+ Splits.insert(Index+Size);
+ break;
+ } else if (Ana->isCall(Inst)) {
+ uint64_t targ = Ana->evaluateBranch(Inst, Index, Size);
+ // Add the call to the call list if the destination is known.
+ if (targ != -1ULL && targ != Index+Size)
+ Calls.push_back(targ);
}
- Splits.insert(Index+Size);
+ } else {
+ errs().write_hex(Index) << ": warning: invalid instruction encoding\n";
+ if (Size == 0)
+ Size = 1; // skip illegible bytes
}
-
- Instructions.push_back(MCDecodedInst(Index, Size, Inst));
- } else {
- errs() << "warning: invalid instruction encoding\n";
- if (Size == 0)
- Size = 1; // skip illegible bytes
}
-
+ }
}
- MCFunction f(Name);
+ // Make sure the instruction list is sorted.
+ std::sort(Instructions.begin(), Instructions.end());
// Create basic blocks.
unsigned ii = 0, ie = Instructions.size();
for (std::set<uint64_t>::iterator spi = Splits.begin(),
- spe = Splits.end(); spi != spe; ++spi) {
+ spe = llvm::prior(Splits.end()); spi != spe; ++spi) {
MCBasicBlock BB;
- uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi);
+ uint64_t BlockEnd = *llvm::next(spi);
// Add instructions to the BB.
for (; ii != ie; ++ii) {
if (Instructions[ii].Address < *spi ||
f.addBlock(*spi, BB);
}
+ std::sort(f.Blocks.begin(), f.Blocks.end());
+
// Calculate successors of each block.
for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
- MCBasicBlock &BB = i->second;
+ MCBasicBlock &BB = const_cast<MCBasicBlock&>(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);
- }
+ BB.addSucc(i->first);
+ } else if (targ != Inst.Address+Inst.Size)
+ BB.addSucc(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)->first);
} else {
// No branch. Fall through to the next block.
- if (!Desc.isReturn() && llvm::next(i) != e)
- BB.addSucc(&llvm::next(i)->second);
+ if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e)
+ BB.addSucc(llvm::next(i)->first);
}
}