LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCModule.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCModule.cpp - MCModule implementation ----------------------===//
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 #include "llvm/MC/MCModule.h"
11 #include "llvm/MC/MCAtom.h"
12 #include "llvm/MC/MCFunction.h"
13 #include <algorithm>
14 
15 using namespace llvm;
16 
17 static bool AtomComp(const MCAtom *L, uint64_t Addr) {
18  return L->getEndAddr() < Addr;
19 }
20 
21 static bool AtomCompInv(uint64_t Addr, const MCAtom *R) {
22  return Addr < R->getEndAddr();
23 }
24 
25 void MCModule::map(MCAtom *NewAtom) {
26  uint64_t Begin = NewAtom->Begin;
27 
28  assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?");
29 
30  // Check for atoms already covering this range.
31  AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
32  Begin, AtomComp);
33  assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End)
34  && "Offset range already occupied!");
35 
36  // Insert the new atom to the list.
37  Atoms.insert(I, NewAtom);
38 }
39 
40 MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) {
41  MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End);
42  map(NewAtom);
43  return NewAtom;
44 }
45 
46 MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) {
47  MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End);
48  map(NewAtom);
49  return NewAtom;
50 }
51 
52 // remap - Update the interval mapping for an atom.
53 void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) {
54  // Find and erase the old mapping.
55  AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
56  Atom->Begin, AtomComp);
57  assert(I != atom_end() && "Atom offset not found in module!");
58  assert(*I == Atom && "Previous atom mapping was invalid!");
59  Atoms.erase(I);
60 
61  // FIXME: special case NewBegin == Atom->Begin
62 
63  // Insert the new mapping.
64  AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(),
65  NewBegin, AtomComp);
66  assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End)
67  && "Offset range already occupied!");
68  Atoms.insert(NewI, Atom);
69 
70  // Update the atom internal bounds.
71  Atom->Begin = NewBegin;
72  Atom->End = NewEnd;
73 }
74 
75 const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
76  AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(),
77  Addr, AtomComp);
78  if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
79  return *I;
80  return 0;
81 }
82 
84  return const_cast<MCAtom*>(
85  const_cast<const MCModule *>(this)->findAtomContaining(Addr));
86 }
87 
88 const MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) const {
89  AtomListTy::const_iterator I = std::upper_bound(atom_begin(), atom_end(),
90  Addr, AtomCompInv);
91  if (I != atom_end())
92  return *I;
93  return 0;
94 }
95 
97  return const_cast<MCAtom*>(
98  const_cast<const MCModule *>(this)->findFirstAtomAfter(Addr));
99 }
100 
102  Functions.push_back(new MCFunction(Name, this));
103  return Functions.back();
104 }
105 
106 static bool CompBBToAtom(MCBasicBlock *BB, const MCTextAtom *Atom) {
107  return BB->getInsts() < Atom;
108 }
109 
110 void MCModule::splitBasicBlocksForAtom(const MCTextAtom *TA,
111  const MCTextAtom *NewTA) {
112  BBsByAtomTy::iterator
113  I = std::lower_bound(BBsByAtom.begin(), BBsByAtom.end(),
114  TA, CompBBToAtom);
115  for (; I != BBsByAtom.end() && (*I)->getInsts() == TA; ++I) {
116  MCBasicBlock *BB = *I;
117  MCBasicBlock *NewBB = &BB->getParent()->createBlock(*NewTA);
118  BB->splitBasicBlock(NewBB);
119  }
120 }
121 
122 void MCModule::trackBBForAtom(const MCTextAtom *Atom, MCBasicBlock *BB) {
123  assert(Atom == BB->getInsts() && "Text atom doesn't back the basic block!");
124  BBsByAtomTy::iterator I = std::lower_bound(BBsByAtom.begin(),
125  BBsByAtom.end(),
126  Atom, CompBBToAtom);
127  for (; I != BBsByAtom.end() && (*I)->getInsts() == Atom; ++I)
128  if (*I == BB)
129  return;
130  BBsByAtom.insert(I, BB);
131 }
132 
134  for (AtomListTy::iterator AI = atom_begin(),
135  AE = atom_end();
136  AI != AE; ++AI)
137  delete *AI;
138  for (FunctionListTy::iterator FI = func_begin(),
139  FE = func_end();
140  FI != FE; ++FI)
141  delete *FI;
142 }
const MCAtom * findFirstAtomAfter(uint64_t Addr) const
Definition: MCModule.cpp:88
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
static bool CompBBToAtom(MCBasicBlock *BB, const MCTextAtom *Atom)
Definition: MCModule.cpp:106
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
const MCAtom * findAtomContaining(uint64_t Addr) const
Definition: MCModule.cpp:75
An atom consising of a sequence of bytes.
Definition: MCAtom.h:170
uint64_t Begin
Definition: MCAtom.h:75
MCFunction * createFunction(StringRef Name)
Create a new MCFunction.
Definition: MCModule.cpp:101
MCDataAtom * createDataAtom(uint64_t Begin, uint64_t End)
Definition: MCModule.cpp:46
Represents a function in machine code, containing MCBasicBlocks. MCFunctions are created by MCModule...
Definition: MCFunction.h:85
MCTextAtom * createTextAtom(uint64_t Begin, uint64_t End)
Definition: MCModule.cpp:40
const_atom_iterator atom_end() const
Definition: MCModule.h:110
const_func_iterator func_begin() const
Definition: MCModule.h:121
const_func_iterator func_end() const
Definition: MCModule.h:123
uint64_t End
Definition: MCAtom.h:75
friend class MCTextAtom
Definition: MCModule.h:66
Basic block containing a sequence of disassembled instructions. The basic block is backed by an MCTex...
Definition: MCFunction.h:33
Represents a contiguous range of either instructions (a TextAtom) or data (a DataAtom). Address ranges are expressed as closed intervals.
Definition: MCAtom.h:34
#define I(x, y, z)
Definition: MD5.cpp:54
static bool AtomComp(const MCAtom *L, uint64_t Addr)
Definition: MCModule.cpp:17
uint64_t getEndAddr() const
Get the end address, i.e. the last one inside the atom.
Definition: MCAtom.h:45
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
const MCFunction * getParent() const
Definition: MCFunction.h:54
static bool AtomCompInv(uint64_t Addr, const MCAtom *R)
Definition: MCModule.cpp:21
const_atom_iterator atom_begin() const
Definition: MCModule.h:108