1 //===-- MCFunction.cpp ----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the algorithm to break down a region of machine code
11 // into basic blocks and try to reconstruct a CFG from it.
13 //===----------------------------------------------------------------------===//
15 #include "MCFunction.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/MC/MCDisassembler.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstPrinter.h"
20 #include "llvm/MC/MCInstrAnalysis.h"
21 #include "llvm/MC/MCInstrDesc.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/Support/MemoryObject.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
30 MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
31 const MemoryObject &Region, uint64_t Start,
32 uint64_t End, const MCInstrAnalysis *Ana,
33 raw_ostream &DebugOut) {
34 std::set<uint64_t> Splits;
36 std::vector<MCDecodedInst> Instructions;
39 // Disassemble code and gather basic block split points.
40 for (uint64_t Index = Start; Index < End; Index += Size) {
43 if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut)) {
44 if (Ana->isBranch(Inst)) {
45 uint64_t targ = Ana->evaluateBranch(Inst, Index, Size);
46 // FIXME: Distinguish relocations from nop jumps.
47 if (targ != -1ULL && (targ == Index+Size || targ >= End)) {
48 Instructions.push_back(MCDecodedInst(Index, Size, Inst));
49 continue; // Skip branches that leave the function.
53 Splits.insert(Index+Size);
54 } else if (Ana->isReturn(Inst)) {
55 Splits.insert(Index+Size);
58 Instructions.push_back(MCDecodedInst(Index, Size, Inst));
60 errs() << "warning: invalid instruction encoding\n";
62 Size = 1; // skip illegible bytes
69 // Create basic blocks.
70 unsigned ii = 0, ie = Instructions.size();
71 for (std::set<uint64_t>::iterator spi = Splits.begin(),
72 spe = Splits.end(); spi != spe; ++spi) {
74 uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi);
75 // Add instructions to the BB.
76 for (; ii != ie; ++ii) {
77 if (Instructions[ii].Address < *spi ||
78 Instructions[ii].Address >= BlockEnd)
80 BB.addInst(Instructions[ii]);
85 // Calculate successors of each block.
86 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
87 MCBasicBlock &BB = i->second;
88 if (BB.getInsts().empty()) continue;
89 const MCDecodedInst &Inst = BB.getInsts().back();
91 if (Ana->isBranch(Inst.Inst)) {
92 uint64_t targ = Ana->evaluateBranch(Inst.Inst, Inst.Address, Inst.Size);
94 // Indirect branch. Bail and add all blocks of the function as a
96 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i)
97 BB.addSucc(&i->second);
98 } else if (targ != Inst.Address+Inst.Size)
99 BB.addSucc(&f.getBlockAtAddress(targ));
100 // Conditional branches can also fall through to the next block.
101 if (Ana->isConditionalBranch(Inst.Inst) && llvm::next(i) != e)
102 BB.addSucc(&llvm::next(i)->second);
104 // No branch. Fall through to the next block.
105 if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e)
106 BB.addSucc(&llvm::next(i)->second);