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 | 5679d7d | 2013-07-04 23:20:12 +0000 | [diff] [blame] | 17 | static bool AtomComp(const MCAtom *LHS, const MCAtom *RHS) { |
| 18 | return LHS->getEndAddr() < RHS->getEndAddr(); |
| 19 | } |
| 20 | |
| 21 | MCModule::const_atom_iterator MCModule::atom_lower_bound(uint64_t Addr) const { |
| 22 | // This is a dummy atom, because VS 2008 doesn't like asymmetric comparators. |
| 23 | MCDataAtom AddrAtom(0, Addr, Addr); |
| 24 | return std::lower_bound(atom_begin(), atom_end(), &AddrAtom, AtomComp); |
| 25 | } |
| 26 | |
| 27 | MCModule::atom_iterator MCModule::atom_lower_bound(uint64_t Addr) { |
| 28 | MCDataAtom AddrAtom(0, Addr, Addr); |
| 29 | return std::lower_bound(atom_begin(), atom_end(), &AddrAtom, AtomComp); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void MCModule::map(MCAtom *NewAtom) { |
Daniel Jasper | 35b2a7a | 2013-05-24 06:26:18 +0000 | [diff] [blame] | 33 | uint64_t Begin = NewAtom->Begin; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 34 | |
Ahmed Bougacha | 7ab184a | 2013-06-19 20:18:59 +0000 | [diff] [blame] | 35 | assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 36 | |
| 37 | // Check for atoms already covering this range. |
Ahmed Bougacha | 5679d7d | 2013-07-04 23:20:12 +0000 | [diff] [blame] | 38 | AtomListTy::iterator I = atom_lower_bound(Begin); |
Daniel Jasper | 35b2a7a | 2013-05-24 06:26:18 +0000 | [diff] [blame] | 39 | assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End) |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 40 | && "Offset range already occupied!"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 41 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 42 | // Insert the new atom to the list. |
| 43 | Atoms.insert(I, NewAtom); |
| 44 | } |
| 45 | |
| 46 | MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) { |
| 47 | MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End); |
| 48 | map(NewAtom); |
| 49 | return NewAtom; |
| 50 | } |
| 51 | |
| 52 | MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) { |
| 53 | MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End); |
| 54 | map(NewAtom); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 55 | return NewAtom; |
| 56 | } |
| 57 | |
| 58 | // remap - Update the interval mapping for an atom. |
| 59 | void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) { |
| 60 | // Find and erase the old mapping. |
Ahmed Bougacha | 5679d7d | 2013-07-04 23:20:12 +0000 | [diff] [blame] | 61 | AtomListTy::iterator I = atom_lower_bound(Atom->Begin); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 62 | assert(I != atom_end() && "Atom offset not found in module!"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 63 | assert(*I == Atom && "Previous atom mapping was invalid!"); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 64 | Atoms.erase(I); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 65 | |
| 66 | // Insert the new mapping. |
Ahmed Bougacha | 5679d7d | 2013-07-04 23:20:12 +0000 | [diff] [blame] | 67 | AtomListTy::iterator NewI = atom_lower_bound(NewBegin); |
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 | 5679d7d | 2013-07-04 23:20:12 +0000 | [diff] [blame] | 76 | AtomListTy::const_iterator I = atom_lower_bound(Addr); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 77 | if (I != atom_end() && (*I)->getBeginAddr() <= Addr) |
| 78 | return *I; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | MCAtom *MCModule::findAtomContaining(uint64_t Addr) { |
Ahmed Bougacha | 5679d7d | 2013-07-04 23:20:12 +0000 | [diff] [blame] | 83 | AtomListTy::iterator I = atom_lower_bound(Addr); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 84 | if (I != atom_end() && (*I)->getBeginAddr() <= Addr) |
| 85 | return *I; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | MCFunction *MCModule::createFunction(const StringRef &Name) { |
| 90 | Functions.push_back(new MCFunction(Name)); |
| 91 | return Functions.back(); |
| 92 | } |
| 93 | |
| 94 | MCModule::~MCModule() { |
| 95 | for (AtomListTy::iterator AI = atom_begin(), |
| 96 | AE = atom_end(); |
| 97 | AI != AE; ++AI) |
| 98 | delete *AI; |
| 99 | for (FunctionListTy::iterator FI = func_begin(), |
| 100 | FE = func_end(); |
| 101 | FI != FE; ++FI) |
| 102 | delete *FI; |
| 103 | } |