LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCFunction.h
Go to the documentation of this file.
1 //===-- llvm/MC/MCFunction.h ------------------------------------*- C++ -*-===//
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_MC_MCFUNCTION_H
16 #define LLVM_MC_MCFUNCTION_H
17 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/MCInst.h"
20 #include <string>
21 #include <vector>
22 
23 namespace llvm {
24 
25 class MCFunction;
26 class MCModule;
27 class MCTextAtom;
28 
29 /// \brief Basic block containing a sequence of disassembled instructions.
30 /// The basic block is backed by an MCTextAtom, which holds the instructions,
31 /// and the address range it covers.
32 /// Create a basic block using MCFunction::createBlock.
33 class MCBasicBlock {
34  const MCTextAtom *Insts;
35 
36  // MCFunction owns the basic block.
37  MCFunction *Parent;
38  friend class MCFunction;
39  MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent);
40 
41  /// \name Predecessors/Successors, to represent the CFG.
42  /// @{
43  typedef std::vector<const MCBasicBlock *> BasicBlockListTy;
44  BasicBlockListTy Successors;
45  BasicBlockListTy Predecessors;
46  /// @}
47 public:
48 
49  /// \brief Get the backing MCTextAtom, containing the instruction sequence.
50  const MCTextAtom *getInsts() const { return Insts; }
51 
52  /// \name Get the owning MCFunction.
53  /// @{
54  const MCFunction *getParent() const { return Parent; }
55  MCFunction *getParent() { return Parent; }
56  /// @}
57 
58  /// MC CFG access: Predecessors/Successors.
59  /// @{
60  typedef BasicBlockListTy::const_iterator succ_const_iterator;
61  succ_const_iterator succ_begin() const { return Successors.begin(); }
62  succ_const_iterator succ_end() const { return Successors.end(); }
63 
64  typedef BasicBlockListTy::const_iterator pred_const_iterator;
65  pred_const_iterator pred_begin() const { return Predecessors.begin(); }
66  pred_const_iterator pred_end() const { return Predecessors.end(); }
67 
68  void addSuccessor(const MCBasicBlock *MCBB);
69  bool isSuccessor(const MCBasicBlock *MCBB) const;
70 
71  void addPredecessor(const MCBasicBlock *MCBB);
72  bool isPredecessor(const MCBasicBlock *MCBB) const;
73 
74  /// \brief Split block, mirrorring NewAtom = Insts->split(..).
75  /// This moves all successors to \p SplitBB, and
76  /// adds a fallthrough to it.
77  /// \p SplitBB The result of splitting Insts, a basic block directly following
78  /// this basic block.
79  void splitBasicBlock(MCBasicBlock *SplitBB);
80  /// @}
81 };
82 
83 /// \brief Represents a function in machine code, containing MCBasicBlocks.
84 /// MCFunctions are created by MCModule.
85 class MCFunction {
87  MCFunction& operator=(const MCFunction&) LLVM_DELETED_FUNCTION;
88 
89  std::string Name;
90  MCModule *ParentModule;
91  typedef std::vector<MCBasicBlock*> BasicBlockListTy;
92  BasicBlockListTy Blocks;
93 
94  // MCModule owns the function.
95  friend class MCModule;
97  ~MCFunction();
98 
99 public:
100  /// \brief Create an MCBasicBlock backed by Insts and add it to this function.
101  /// \param Insts Sequence of straight-line code backing the basic block.
102  /// \returns The newly created basic block.
103  MCBasicBlock &createBlock(const MCTextAtom &Insts);
104 
105  StringRef getName() const { return Name; }
106 
107  /// \name Get the owning MC Module.
108  /// @{
109  const MCModule *getParent() const { return ParentModule; }
110  MCModule *getParent() { return ParentModule; }
111  /// @}
112 
113  /// \name Access to the function's basic blocks. No ordering is enforced,
114  /// except that the first block is the entry block.
115  /// @{
116  /// \brief Get the entry point basic block.
117  const MCBasicBlock *getEntryBlock() const { return front(); }
119 
120  bool empty() const { return Blocks.empty(); }
121 
122  typedef BasicBlockListTy::const_iterator const_iterator;
123  typedef BasicBlockListTy:: iterator iterator;
124  const_iterator begin() const { return Blocks.begin(); }
125  iterator begin() { return Blocks.begin(); }
126  const_iterator end() const { return Blocks.end(); }
127  iterator end() { return Blocks.end(); }
128 
129  const MCBasicBlock* front() const { return Blocks.front(); }
130  MCBasicBlock* front() { return Blocks.front(); }
131  const MCBasicBlock* back() const { return Blocks.back(); }
132  MCBasicBlock* back() { return Blocks.back(); }
133 
134  /// \brief Find the basic block, if any, that starts at \p StartAddr.
135  const MCBasicBlock *find(uint64_t StartAddr) const;
136  MCBasicBlock *find(uint64_t StartAddr);
137  /// @}
138 };
139 
140 }
141 
142 #endif
MCBasicBlock * front()
Definition: MCFunction.h:130
succ_const_iterator succ_begin() const
Definition: MCFunction.h:61
MCBasicBlock * back()
Definition: MCFunction.h:132
succ_const_iterator succ_end() const
Definition: MCFunction.h:62
const MCBasicBlock * front() const
Definition: MCFunction.h:129
bool isPredecessor(const MCBasicBlock *MCBB) const
Definition: MCFunction.cpp:67
MCBasicBlock & createBlock(const MCTextAtom &Insts)
Create an MCBasicBlock backed by Insts and add it to this function.
Definition: MCFunction.cpp:28
An atom consisting of disassembled instructions.
Definition: MCAtom.h:123
MCBasicBlock * getEntryBlock()
Definition: MCFunction.h:118
const_iterator begin() const
Definition: MCFunction.h:124
const MCBasicBlock * back() const
Definition: MCFunction.h:131
void addPredecessor(const MCBasicBlock *MCBB)
Definition: MCFunction.cpp:62
MCModule * getParent()
Definition: MCFunction.h:110
void addSuccessor(const MCBasicBlock *MCBB)
Definition: MCFunction.cpp:52
void splitBasicBlock(MCBasicBlock *SplitBB)
Split block, mirrorring NewAtom = Insts->split(..). This moves all successors to SplitBB, and adds a fallthrough to it. SplitBB The result of splitting Insts, a basic block directly following this basic block.
Definition: MCFunction.cpp:72
pred_const_iterator pred_end() const
Definition: MCFunction.h:66
pred_const_iterator pred_begin() const
Definition: MCFunction.h:65
bool empty() const
Definition: MCFunction.h:120
Represents a function in machine code, containing MCBasicBlocks. MCFunctions are created by MCModule...
Definition: MCFunction.h:85
iterator begin()
Definition: MCFunction.h:125
BasicBlockListTy::const_iterator pred_const_iterator
Definition: MCFunction.h:64
MCFunction * getParent()
Definition: MCFunction.h:55
StringRef getName() const
Definition: MCFunction.h:105
BasicBlockListTy::const_iterator succ_const_iterator
Definition: MCFunction.h:60
const MCModule * getParent() const
Definition: MCFunction.h:109
bool isSuccessor(const MCBasicBlock *MCBB) const
Definition: MCFunction.cpp:57
iterator end()
Definition: MCFunction.h:127
Basic block containing a sequence of disassembled instructions. The basic block is backed by an MCTex...
Definition: MCFunction.h:33
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
const_iterator end() const
Definition: MCFunction.h:126
const MCBasicBlock * getEntryBlock() const
Definition: MCFunction.h:117
A completely disassembled object file or executable. It comprises a list of MCAtom's, each representing a contiguous range of either instructions or data. An MCModule is created using MCObjectDisassembler::buildModule.
Definition: MCModule.h:36
const MCTextAtom * getInsts() const
Get the backing MCTextAtom, containing the instruction sequence.
Definition: MCFunction.h:50
BasicBlockListTy::iterator iterator
Definition: MCFunction.h:123
const MCBasicBlock * find(uint64_t StartAddr) const
Find the basic block, if any, that starts at StartAddr.
Definition: MCFunction.cpp:41
BasicBlockListTy::const_iterator const_iterator
Definition: MCFunction.h:122
const MCFunction * getParent() const
Definition: MCFunction.h:54