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 | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 17 | static bool AtomComp(const MCAtom *L, uint64_t Addr) { |
| 18 | return L->getEndAddr() < Addr; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Ahmed Bougacha | 46d353f | 2013-08-21 07:28:17 +0000 | [diff] [blame^] | 21 | static bool AtomCompInv(uint64_t Addr, const MCAtom *R) { |
| 22 | return Addr < R->getEndAddr(); |
| 23 | } |
| 24 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 25 | void MCModule::map(MCAtom *NewAtom) { |
Daniel Jasper | 35b2a7a | 2013-05-24 06:26:18 +0000 | [diff] [blame] | 26 | uint64_t Begin = NewAtom->Begin; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 27 | |
Ahmed Bougacha | 7ab184a | 2013-06-19 20:18:59 +0000 | [diff] [blame] | 28 | assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 29 | |
| 30 | // Check for atoms already covering this range. |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 31 | AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(), |
| 32 | Begin, AtomComp); |
Daniel Jasper | 35b2a7a | 2013-05-24 06:26:18 +0000 | [diff] [blame] | 33 | assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End) |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 34 | && "Offset range already occupied!"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 35 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 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); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 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. |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 55 | AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(), |
| 56 | Atom->Begin, AtomComp); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 57 | assert(I != atom_end() && "Atom offset not found in module!"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 58 | assert(*I == Atom && "Previous atom mapping was invalid!"); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 59 | Atoms.erase(I); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 60 | |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 61 | // FIXME: special case NewBegin == Atom->Begin |
| 62 | |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 63 | // Insert the new mapping. |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 64 | AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(), |
| 65 | NewBegin, AtomComp); |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 66 | assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End) |
| 67 | && "Offset range already occupied!"); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 68 | Atoms.insert(NewI, Atom); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 69 | |
| 70 | // Update the atom internal bounds. |
| 71 | Atom->Begin = NewBegin; |
| 72 | Atom->End = NewEnd; |
| 73 | } |
| 74 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 75 | const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const { |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 76 | AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(), |
| 77 | Addr, AtomComp); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 78 | if (I != atom_end() && (*I)->getBeginAddr() <= Addr) |
| 79 | return *I; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | MCAtom *MCModule::findAtomContaining(uint64_t Addr) { |
Ahmed Bougacha | 46d353f | 2013-08-21 07:28:17 +0000 | [diff] [blame^] | 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()) |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 92 | return *I; |
| 93 | return 0; |
| 94 | } |
| 95 | |
Ahmed Bougacha | 46d353f | 2013-08-21 07:28:17 +0000 | [diff] [blame^] | 96 | MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) { |
| 97 | return const_cast<MCAtom*>( |
| 98 | const_cast<const MCModule *>(this)->findFirstAtomAfter(Addr)); |
| 99 | } |
| 100 | |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 101 | MCFunction *MCModule::createFunction(StringRef Name) { |
| 102 | Functions.push_back(new MCFunction(Name, this)); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 103 | return Functions.back(); |
| 104 | } |
| 105 | |
| 106 | MCModule::~MCModule() { |
| 107 | for (AtomListTy::iterator AI = atom_begin(), |
| 108 | AE = atom_end(); |
| 109 | AI != AE; ++AI) |
| 110 | delete *AI; |
| 111 | for (FunctionListTy::iterator FI = func_begin(), |
| 112 | FE = func_end(); |
| 113 | FI != FE; ++FI) |
| 114 | delete *FI; |
| 115 | } |