1 //===- lib/MC/MCModule.cpp - MCModule implementation ----------------------===//
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 #include "llvm/MC/MCModule.h"
11 #include "llvm/MC/MCAtom.h"
12 #include "llvm/MC/MCFunction.h"
17 static bool AtomComp(const MCAtom *L, uint64_t Addr) {
18 return L->getEndAddr() < Addr;
21 static bool AtomCompInv(uint64_t Addr, const MCAtom *R) {
22 return Addr < R->getEndAddr();
25 void MCModule::map(MCAtom *NewAtom) {
26 uint64_t Begin = NewAtom->Begin;
28 assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?");
30 // Check for atoms already covering this range.
31 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
33 assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End)
34 && "Offset range already occupied!");
36 // Insert the new atom to the list.
37 Atoms.insert(I, NewAtom);
40 MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) {
41 MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End);
46 MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) {
47 MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End);
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!");
61 // FIXME: special case NewBegin == Atom->Begin
63 // Insert the new mapping.
64 AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(),
66 assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End)
67 && "Offset range already occupied!");
68 Atoms.insert(NewI, Atom);
70 // Update the atom internal bounds.
71 Atom->Begin = NewBegin;
75 const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
76 AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(),
78 if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
83 MCAtom *MCModule::findAtomContaining(uint64_t Addr) {
84 return const_cast<MCAtom*>(
85 const_cast<const MCModule *>(this)->findAtomContaining(Addr));
88 const MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) const {
89 AtomListTy::const_iterator I = std::upper_bound(atom_begin(), atom_end(),
96 MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) {
97 return const_cast<MCAtom*>(
98 const_cast<const MCModule *>(this)->findFirstAtomAfter(Addr));
101 MCFunction *MCModule::createFunction(StringRef Name) {
102 Functions.push_back(new MCFunction(Name, this));
103 return Functions.back();
106 static bool CompBBToAtom(MCBasicBlock *BB, const MCTextAtom *Atom) {
107 return BB->getInsts() < Atom;
110 void MCModule::splitBasicBlocksForAtom(const MCTextAtom *TA,
111 const MCTextAtom *NewTA) {
112 BBsByAtomTy::iterator
113 I = std::lower_bound(BBsByAtom.begin(), BBsByAtom.end(),
115 for (; I != BBsByAtom.end() && (*I)->getInsts() == TA; ++I) {
116 MCBasicBlock *BB = *I;
117 MCBasicBlock *NewBB = &BB->getParent()->createBlock(*NewTA);
118 BB->splitBasicBlock(NewBB);
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(),
127 for (; I != BBsByAtom.end() && (*I)->getInsts() == Atom; ++I)
130 BBsByAtom.insert(I, BB);
133 MCModule::~MCModule() {
134 for (AtomListTy::iterator AI = atom_begin(),
138 for (FunctionListTy::iterator FI = func_begin(),