blob: 418e9d050c1a2f6a8fc084c37c16c71096d233a6 [file] [log] [blame]
Benjamin Kramer685a2502011-07-20 19:37:35 +00001//===-- 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"
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000020#include "llvm/MC/MCInstrAnalysis.h"
Benjamin Kramer685a2502011-07-20 19:37:35 +000021#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>
27using namespace llvm;
28
29MCFunction
30MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
31 const MemoryObject &Region, uint64_t Start,
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000032 uint64_t End, const MCInstrAnalysis *Ana,
Benjamin Kramer685a2502011-07-20 19:37:35 +000033 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)) {
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000044 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.
Benjamin Kramer685a2502011-07-20 19:37:35 +000050 }
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000051 if (targ != -1ULL)
52 Splits.insert(targ);
Benjamin Kramer685a2502011-07-20 19:37:35 +000053 Splits.insert(Index+Size);
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000054 } else if (Ana->isReturn(Inst)) {
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000055 Splits.insert(Index+Size);
Benjamin Kramer685a2502011-07-20 19:37:35 +000056 }
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();
Benjamin Kramer685a2502011-07-20 19:37:35 +000090
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000091 if (Ana->isBranch(Inst.Inst)) {
92 uint64_t targ = Ana->evaluateBranch(Inst.Inst, Inst.Address, Inst.Size);
93 if (targ == -1ULL) {
Benjamin Kramer685a2502011-07-20 19:37:35 +000094 // 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);
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000098 } 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);
Benjamin Kramer685a2502011-07-20 19:37:35 +0000103 } else {
104 // No branch. Fall through to the next block.
Benjamin Kramer41ab14b2011-08-08 18:56:44 +0000105 if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e)
Francois Pichetc3d48ce2011-07-20 21:35:29 +0000106 BB.addSucc(&llvm::next(i)->second);
Benjamin Kramer685a2502011-07-20 19:37:35 +0000107 }
108 }
109
110 return f;
111}