Add MCInstrAnalysis class. This allows the targets to specify own versions of MCInstr...
[oota-llvm.git] / tools / llvm-objdump / MCFunction.cpp
1 //===-- MCFunction.cpp ----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
26 #include <set>
27 using namespace llvm;
28
29 MCFunction
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;
35   Splits.insert(Start);
36   std::vector<MCDecodedInst> Instructions;
37   uint64_t Size;
38
39   // Disassemble code and gather basic block split points.
40   for (uint64_t Index = Start; Index < End; Index += Size) {
41     MCInst Inst;
42
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.
50         }
51         if (targ != -1ULL)
52           Splits.insert(targ);
53         Splits.insert(Index+Size);
54       } else if (Ana->isReturn(Inst)) {
55         Splits.insert(Index+Size);
56       }
57
58       Instructions.push_back(MCDecodedInst(Index, Size, Inst));
59     } else {
60       errs() << "warning: invalid instruction encoding\n";
61       if (Size == 0)
62         Size = 1; // skip illegible bytes
63     }
64
65   }
66
67   MCFunction f(Name);
68
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) {
73     MCBasicBlock BB;
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)
79         break;
80       BB.addInst(Instructions[ii]);
81     }
82     f.addBlock(*spi, BB);
83   }
84
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();
90
91     if (Ana->isBranch(Inst.Inst)) {
92       uint64_t targ = Ana->evaluateBranch(Inst.Inst, Inst.Address, Inst.Size);
93       if (targ == -1ULL) {
94         // Indirect branch. Bail and add all blocks of the function as a
95         // successor.
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);
103     } else {
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);
107     }
108   }
109
110   return f;
111 }