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 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/MC/MCInst.h"
27 /// MCDecodedInst - Small container to hold an MCInst and associated info like
29 struct MCDecodedInst {
34 MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst)
35 : Address(Address), Size(Size), Inst(Inst) {}
38 /// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing
41 SmallVector<MCDecodedInst, 8> Insts;
42 typedef SmallPtrSet<MCBasicBlock*, 8> SetTy;
45 ArrayRef<MCDecodedInst> getInsts() const { return Insts; }
47 typedef SetTy::const_iterator succ_iterator;
48 succ_iterator succ_begin() const { return Succs.begin(); }
49 succ_iterator succ_end() const { return Succs.end(); }
51 bool contains(MCBasicBlock *BB) const { return Succs.count(BB); }
53 void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); }
54 void addSucc(MCBasicBlock *BB) { Succs.insert(BB); }
57 /// MCFunction - Represents a named function in machine code, containing
58 /// multiple MCBasicBlocks.
61 // Keep BBs sorted by address.
62 typedef std::map<uint64_t, MCBasicBlock> MapTy;
65 MCFunction(StringRef Name) : Name(Name) {}
67 // Create an MCFunction from a region of binary machine code.
69 createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
70 const MemoryObject &Region, uint64_t Start, uint64_t End,
71 const MCInstrInfo *InstrInfo, raw_ostream &DebugOut);
73 typedef MapTy::iterator iterator;
74 iterator begin() { return Blocks.begin(); }
75 iterator end() { return Blocks.end(); }
77 StringRef getName() const { return Name; }
79 MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) {
80 assert(!Blocks.count(Address) && "Already a BB at address.");
81 return Blocks[Address] = BB;
84 MCBasicBlock &getBlockAtAddress(uint64_t Address) {
85 assert(Blocks.count(Address) && "No BB at address.");
86 return Blocks[Address];