9a9d90e5b6a71027d09888f49c0008ae9480393b
[oota-llvm.git] / lib / MC / MCModule.cpp
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 void MCModule::map(MCAtom *NewAtom) {
22   uint64_t Begin = NewAtom->Begin;
23
24   assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?");
25
26   // Check for atoms already covering this range.
27   AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
28                                             Begin, AtomComp);
29   assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End)
30          && "Offset range already occupied!");
31
32   // Insert the new atom to the list.
33   Atoms.insert(I, NewAtom);
34 }
35
36 MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) {
37   MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End);
38   map(NewAtom);
39   return NewAtom;
40 }
41
42 MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) {
43   MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End);
44   map(NewAtom);
45   return NewAtom;
46 }
47
48 // remap - Update the interval mapping for an atom.
49 void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) {
50   // Find and erase the old mapping.
51   AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
52                                             Atom->Begin, AtomComp);
53   assert(I != atom_end() && "Atom offset not found in module!");
54   assert(*I == Atom && "Previous atom mapping was invalid!");
55   Atoms.erase(I);
56
57   // FIXME: special case NewBegin == Atom->Begin
58
59   // Insert the new mapping.
60   AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(),
61                                                NewBegin, AtomComp);
62   assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End)
63          && "Offset range already occupied!");
64   Atoms.insert(NewI, Atom);
65
66   // Update the atom internal bounds.
67   Atom->Begin = NewBegin;
68   Atom->End = NewEnd;
69 }
70
71 const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
72   AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(),
73                                                   Addr, AtomComp);
74   if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
75     return *I;
76   return 0;
77 }
78
79 MCAtom *MCModule::findAtomContaining(uint64_t Addr) {
80   AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
81                                             Addr, AtomComp);
82   if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
83     return *I;
84   return 0;
85 }
86
87 MCFunction *MCModule::createFunction(StringRef Name) {
88   Functions.push_back(new MCFunction(Name, this));
89   return Functions.back();
90 }
91
92 MCModule::~MCModule() {
93   for (AtomListTy::iterator AI = atom_begin(),
94                             AE = atom_end();
95                             AI != AE; ++AI)
96     delete *AI;
97   for (FunctionListTy::iterator FI = func_begin(),
98                                 FE = func_end();
99                                 FI != FE; ++FI)
100     delete *FI;
101 }