blob: dd31402ddc66f8a66606383568b3e01db41a28c0 [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"
20#include "llvm/MC/MCInstrDesc.h"
21#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/Support/MemoryObject.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Support/system_error.h"
25#include <set>
26using namespace llvm;
27
28MCFunction
29MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
30 const MemoryObject &Region, uint64_t Start,
31 uint64_t End, const MCInstrInfo *InstrInfo,
32 raw_ostream &DebugOut) {
33 std::set<uint64_t> Splits;
34 Splits.insert(Start);
35 std::vector<MCDecodedInst> Instructions;
36 uint64_t Size;
37
38 // Disassemble code and gather basic block split points.
39 for (uint64_t Index = Start; Index < End; Index += Size) {
40 MCInst Inst;
41
42 if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut)) {
43 const MCInstrDesc &Desc = InstrInfo->get(Inst.getOpcode());
44 if (Desc.isBranch()) {
45 if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
46 int64_t Imm = Inst.getOperand(0).getImm();
47 // FIXME: Distinguish relocations from nop jumps.
48 if (Imm != 0) {
49 assert(Index+Imm+Size < End && "Branch out of function.");
50 Splits.insert(Index+Imm+Size);
51 }
52 }
53 Splits.insert(Index+Size);
54 }
55
56 Instructions.push_back(MCDecodedInst(Index, Size, Inst));
57 } else {
58 errs() << "warning: invalid instruction encoding\n";
59 if (Size == 0)
60 Size = 1; // skip illegible bytes
61 }
62
63 }
64
65 MCFunction f(Name);
66
67 // Create basic blocks.
68 unsigned ii = 0, ie = Instructions.size();
69 for (std::set<uint64_t>::iterator spi = Splits.begin(),
70 spe = Splits.end(); spi != spe; ++spi) {
71 MCBasicBlock BB;
72 uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi);
73 // Add instructions to the BB.
74 for (; ii != ie; ++ii) {
75 if (Instructions[ii].Address < *spi ||
76 Instructions[ii].Address >= BlockEnd)
77 break;
78 BB.addInst(Instructions[ii]);
79 }
80 f.addBlock(*spi, BB);
81 }
82
83 // Calculate successors of each block.
84 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
85 MCBasicBlock &BB = i->second;
86 if (BB.getInsts().empty()) continue;
87 const MCDecodedInst &Inst = BB.getInsts().back();
88 const MCInstrDesc &Desc = InstrInfo->get(Inst.Inst.getOpcode());
89
90 if (Desc.isBranch()) {
91 // PCRel branch, we know the destination.
92 if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
93 int64_t Imm = Inst.Inst.getOperand(0).getImm();
94 if (Imm != 0)
95 BB.addSucc(&f.getBlockAtAddress(Inst.Address+Inst.Size+Imm));
96 // Conditional branches can also fall through to the next block.
97 if (Desc.isConditionalBranch() && llvm::next(i) != e)
98 BB.addSucc(&next(i)->second);
99 } else {
100 // Indirect branch. Bail and add all blocks of the function as a
101 // successor.
102 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i)
103 BB.addSucc(&i->second);
104 }
105 } else {
106 // No branch. Fall through to the next block.
107 if (!Desc.isReturn() && next(i) != e)
108 BB.addSucc(&next(i)->second);
109 }
110 }
111
112 return f;
113}