blob: 4a4f9d5e50a0cc882a288ec29c54a6428fd14bf8 [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) {
Benjamin Kramer83a162e2011-07-22 18:35:09 +000049 if (Index+Imm+Size >= End) {
50 Instructions.push_back(MCDecodedInst(Index, Size, Inst));
51 continue; // Skip branches that leave the function.
52 }
Benjamin Kramer685a2502011-07-20 19:37:35 +000053 Splits.insert(Index+Imm+Size);
54 }
55 }
56 Splits.insert(Index+Size);
57 }
58
59 Instructions.push_back(MCDecodedInst(Index, Size, Inst));
60 } else {
61 errs() << "warning: invalid instruction encoding\n";
62 if (Size == 0)
63 Size = 1; // skip illegible bytes
64 }
65
66 }
67
68 MCFunction f(Name);
69
70 // Create basic blocks.
71 unsigned ii = 0, ie = Instructions.size();
72 for (std::set<uint64_t>::iterator spi = Splits.begin(),
73 spe = Splits.end(); spi != spe; ++spi) {
74 MCBasicBlock BB;
75 uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi);
76 // Add instructions to the BB.
77 for (; ii != ie; ++ii) {
78 if (Instructions[ii].Address < *spi ||
79 Instructions[ii].Address >= BlockEnd)
80 break;
81 BB.addInst(Instructions[ii]);
82 }
83 f.addBlock(*spi, BB);
84 }
85
86 // Calculate successors of each block.
87 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
88 MCBasicBlock &BB = i->second;
89 if (BB.getInsts().empty()) continue;
90 const MCDecodedInst &Inst = BB.getInsts().back();
91 const MCInstrDesc &Desc = InstrInfo->get(Inst.Inst.getOpcode());
92
93 if (Desc.isBranch()) {
94 // PCRel branch, we know the destination.
95 if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
96 int64_t Imm = Inst.Inst.getOperand(0).getImm();
97 if (Imm != 0)
98 BB.addSucc(&f.getBlockAtAddress(Inst.Address+Inst.Size+Imm));
99 // Conditional branches can also fall through to the next block.
100 if (Desc.isConditionalBranch() && llvm::next(i) != e)
Francois Pichetc3d48ce2011-07-20 21:35:29 +0000101 BB.addSucc(&llvm::next(i)->second);
Benjamin Kramer685a2502011-07-20 19:37:35 +0000102 } else {
103 // Indirect branch. Bail and add all blocks of the function as a
104 // successor.
105 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i)
106 BB.addSucc(&i->second);
107 }
108 } else {
109 // No branch. Fall through to the next block.
Francois Pichetc3d48ce2011-07-20 21:35:29 +0000110 if (!Desc.isReturn() && llvm::next(i) != e)
111 BB.addSucc(&llvm::next(i)->second);
Benjamin Kramer685a2502011-07-20 19:37:35 +0000112 }
113 }
114
115 return f;
116}