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