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/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"
29 MCFunction::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;
35 std::vector<MCDecodedInst> Instructions;
38 // Disassemble code and gather basic block split points.
39 for (uint64_t Index = Start; Index < End; Index += Size) {
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.
49 if (Index+Imm+Size >= End) {
50 Instructions.push_back(MCDecodedInst(Index, Size, Inst));
51 continue; // Skip branches that leave the function.
53 Splits.insert(Index+Imm+Size);
56 Splits.insert(Index+Size);
57 } else if (Desc.isReturn()) {
58 Splits.insert(Index+Size);
61 Instructions.push_back(MCDecodedInst(Index, Size, Inst));
63 errs() << "warning: invalid instruction encoding\n";
65 Size = 1; // skip illegible bytes
72 // Create basic blocks.
73 unsigned ii = 0, ie = Instructions.size();
74 for (std::set<uint64_t>::iterator spi = Splits.begin(),
75 spe = Splits.end(); spi != spe; ++spi) {
77 uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi);
78 // Add instructions to the BB.
79 for (; ii != ie; ++ii) {
80 if (Instructions[ii].Address < *spi ||
81 Instructions[ii].Address >= BlockEnd)
83 BB.addInst(Instructions[ii]);
88 // Calculate successors of each block.
89 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
90 MCBasicBlock &BB = i->second;
91 if (BB.getInsts().empty()) continue;
92 const MCDecodedInst &Inst = BB.getInsts().back();
93 const MCInstrDesc &Desc = InstrInfo->get(Inst.Inst.getOpcode());
95 if (Desc.isBranch()) {
96 // PCRel branch, we know the destination.
97 if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
98 int64_t Imm = Inst.Inst.getOperand(0).getImm();
100 BB.addSucc(&f.getBlockAtAddress(Inst.Address+Inst.Size+Imm));
101 // Conditional branches can also fall through to the next block.
102 if (Desc.isConditionalBranch() && llvm::next(i) != e)
103 BB.addSucc(&llvm::next(i)->second);
105 // Indirect branch. Bail and add all blocks of the function as a
107 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i)
108 BB.addSucc(&i->second);
111 // No branch. Fall through to the next block.
112 if (!Desc.isReturn() && llvm::next(i) != e)
113 BB.addSucc(&llvm::next(i)->second);