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 void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); }
52 void addSucc(MCBasicBlock *BB) { Succs.insert(BB); }
55 /// MCFunction - Represents a named function in machine code, containing
56 /// multiple MCBasicBlocks.
59 // Keep BBs sorted by address.
60 typedef std::map<uint64_t, MCBasicBlock> MapTy;
63 MCFunction(StringRef Name) : Name(Name) {}
65 // Create an MCFunction from a region of binary machine code.
67 createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
68 const MemoryObject &Region, uint64_t Start, uint64_t End,
69 const MCInstrInfo *InstrInfo, raw_ostream &DebugOut);
71 typedef MapTy::iterator iterator;
72 iterator begin() { return Blocks.begin(); }
73 iterator end() { return Blocks.end(); }
75 StringRef getName() const { return Name; }
77 MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) {
78 assert(!Blocks.count(Address) && "Already a BB at address.");
79 return Blocks[Address] = BB;
82 MCBasicBlock &getBlockAtAddress(uint64_t Address) {
83 assert(Blocks.count(Address) && "No BB at address.");
84 return Blocks[Address];