1 //===-- MCFunction.h ------------------------------------------------------===//
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 data structures to hold a CFG reconstructed from
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECTDUMP_MCFUNCTION_H
16 #define LLVM_OBJECTDUMP_MCFUNCTION_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/MC/MCInst.h"
26 class MCInstrAnalysis;
30 /// MCDecodedInst - Small container to hold an MCInst and associated info like
32 struct MCDecodedInst {
38 MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst)
39 : Address(Address), Size(Size), Inst(Inst) {}
41 bool operator<(const MCDecodedInst &RHS) const {
42 return Address < RHS.Address;
46 /// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing
49 std::vector<MCDecodedInst> Insts;
50 typedef DenseSet<uint64_t> SetTy;
53 ArrayRef<MCDecodedInst> getInsts() const { return Insts; }
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(); }
59 bool contains(uint64_t Addr) const { return Succs.count(Addr); }
61 void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); }
62 void addSucc(uint64_t Addr) { Succs.insert(Addr); }
64 bool operator<(const MCBasicBlock &RHS) const {
65 return Insts.size() < RHS.Insts.size();
69 /// MCFunction - Represents a named function in machine code, containing
70 /// multiple MCBasicBlocks.
73 // Keep BBs sorted by address.
74 typedef std::vector<std::pair<uint64_t, MCBasicBlock> > MapTy;
77 MCFunction(StringRef Name) : Name(Name) {}
79 // Create an MCFunction from a region of binary machine code.
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);
86 typedef MapTy::const_iterator iterator;
87 iterator begin() const { return Blocks.begin(); }
88 iterator end() const { return Blocks.end(); }
90 StringRef getName() const { return Name; }
92 MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) {
93 Blocks.push_back(std::make_pair(Address, BB));
94 return Blocks.back().second;