1//===-- MCFunction.h ------------------------------------------------------===// 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 data structures to hold a CFG reconstructed from 11// machine code. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_OBJECTDUMP_MCFUNCTION_H 16#define LLVM_OBJECTDUMP_MCFUNCTION_H 17 18#include "llvm/ADT/ArrayRef.h" 19#include "llvm/ADT/DenseSet.h" 20#include "llvm/MC/MCInst.h" 21#include <map> 22 23namespace llvm { 24 25class MCDisassembler; 26class MCInstrAnalysis; 27class MemoryObject; 28class raw_ostream; 29 30/// MCDecodedInst - Small container to hold an MCInst and associated info like 31/// address and size. 32struct MCDecodedInst { 33 uint64_t Address; 34 uint64_t Size; 35 MCInst Inst; 36 37 MCDecodedInst() {} 38 MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst) 39 : Address(Address), Size(Size), Inst(Inst) {} 40 41 bool operator<(const MCDecodedInst &RHS) const { 42 return Address < RHS.Address; 43 } 44}; 45 46/// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing 47/// MCBasicBlocks. 48class MCBasicBlock { 49 std::vector<MCDecodedInst> Insts; 50 typedef DenseSet<uint64_t> SetTy; 51 SetTy Succs; 52public: 53 ArrayRef<MCDecodedInst> getInsts() const { return Insts; } 54 55 typedef SetTy::const_iterator succ_iterator; 56 succ_iterator succ_begin() const { return Succs.begin(); } 57 succ_iterator succ_end() const { return Succs.end(); } 58 59 bool contains(uint64_t Addr) const { return Succs.count(Addr); } 60 61 void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); } 62 void addSucc(uint64_t Addr) { Succs.insert(Addr); } 63 64 bool operator<(const MCBasicBlock &RHS) const { 65 return Insts.size() < RHS.Insts.size(); 66 } 67}; 68 69/// MCFunction - Represents a named function in machine code, containing 70/// multiple MCBasicBlocks. 71class MCFunction { 72 const StringRef Name; 73 // Keep BBs sorted by address. 74 typedef std::vector<std::pair<uint64_t, MCBasicBlock> > MapTy; 75 MapTy Blocks; 76public: 77 MCFunction(StringRef Name) : Name(Name) {} 78 79 // Create an MCFunction from a region of binary machine code. 80 static MCFunction 81 createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, 82 const MemoryObject &Region, uint64_t Start, uint64_t End, 83 const MCInstrAnalysis *Ana, raw_ostream &DebugOut, 84 SmallVectorImpl<uint64_t> &Calls); 85 86 typedef MapTy::const_iterator iterator; 87 iterator begin() const { return Blocks.begin(); } 88 iterator end() const { return Blocks.end(); } 89 90 StringRef getName() const { return Name; } 91 92 MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) { 93 Blocks.push_back(std::make_pair(Address, BB)); 94 return Blocks.back().second; 95 } 96}; 97 98} 99 100#endif 101