LLVM API Documentation
00001 //===- lib/MC/MCModule.cpp - MCModule implementation ----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/MC/MCModule.h" 00011 #include "llvm/MC/MCAtom.h" 00012 #include "llvm/MC/MCFunction.h" 00013 #include <algorithm> 00014 00015 using namespace llvm; 00016 00017 static bool AtomComp(const MCAtom *L, uint64_t Addr) { 00018 return L->getEndAddr() < Addr; 00019 } 00020 00021 void MCModule::map(MCAtom *NewAtom) { 00022 uint64_t Begin = NewAtom->Begin; 00023 00024 assert(Begin < NewAtom->End && "Creating MCAtom with endpoints reversed?"); 00025 00026 // Check for atoms already covering this range. 00027 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(), 00028 Begin, AtomComp); 00029 assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End) 00030 && "Offset range already occupied!"); 00031 00032 // Insert the new atom to the list. 00033 Atoms.insert(I, NewAtom); 00034 } 00035 00036 MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) { 00037 MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End); 00038 map(NewAtom); 00039 return NewAtom; 00040 } 00041 00042 MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) { 00043 MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End); 00044 map(NewAtom); 00045 return NewAtom; 00046 } 00047 00048 // remap - Update the interval mapping for an atom. 00049 void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) { 00050 // Find and erase the old mapping. 00051 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(), 00052 Atom->Begin, AtomComp); 00053 assert(I != atom_end() && "Atom offset not found in module!"); 00054 assert(*I == Atom && "Previous atom mapping was invalid!"); 00055 Atoms.erase(I); 00056 00057 // Insert the new mapping. 00058 AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(), 00059 NewBegin, AtomComp); 00060 Atoms.insert(NewI, Atom); 00061 00062 // Update the atom internal bounds. 00063 Atom->Begin = NewBegin; 00064 Atom->End = NewEnd; 00065 } 00066 00067 const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const { 00068 AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(), 00069 Addr, AtomComp); 00070 if (I != atom_end() && (*I)->getBeginAddr() <= Addr) 00071 return *I; 00072 return 0; 00073 } 00074 00075 MCAtom *MCModule::findAtomContaining(uint64_t Addr) { 00076 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(), 00077 Addr, AtomComp); 00078 if (I != atom_end() && (*I)->getBeginAddr() <= Addr) 00079 return *I; 00080 return 0; 00081 } 00082 00083 MCFunction *MCModule::createFunction(const StringRef &Name) { 00084 Functions.push_back(new MCFunction(Name)); 00085 return Functions.back(); 00086 } 00087 00088 MCModule::~MCModule() { 00089 for (AtomListTy::iterator AI = atom_begin(), 00090 AE = atom_end(); 00091 AI != AE; ++AI) 00092 delete *AI; 00093 for (FunctionListTy::iterator FI = func_begin(), 00094 FE = func_end(); 00095 FI != FE; ++FI) 00096 delete *FI; 00097 }