blob: 5890b4bd028b583760581b2502d46522fd5fc30b [file] [log] [blame]
Jim Grosbach946227d2011-11-15 16:46:22 +00001//===- lib/MC/MCModule.cpp - MCModule implementation ----------------------===//
Owen Anderson124e1822011-09-22 22:32:22 +00002//
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 Anderson124e1822011-09-22 22:32:22 +000010#include "llvm/MC/MCModule.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000011#include "llvm/MC/MCAtom.h"
12#include "llvm/MC/MCFunction.h"
13#include <algorithm>
Owen Anderson124e1822011-09-22 22:32:22 +000014
15using namespace llvm;
16
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000017static bool AtomComp(const MCAtom *L, uint64_t Addr) {
18 return L->getEndAddr() < Addr;
Ahmed Bougachaef993562013-05-24 01:07:04 +000019}
20
21void MCModule::map(MCAtom *NewAtom) {
Daniel Jasper35b2a7a2013-05-24 06:26:18 +000022 uint64_t Begin = NewAtom->Begin;
Ahmed Bougachaef993562013-05-24 01:07:04 +000023
Ahmed Bougacha7ab184a2013-06-19 20:18:59 +000024 assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?");
Owen Anderson124e1822011-09-22 22:32:22 +000025
26 // Check for atoms already covering this range.
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000027 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
28 Begin, AtomComp);
Daniel Jasper35b2a7a2013-05-24 06:26:18 +000029 assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End)
Ahmed Bougachaef993562013-05-24 01:07:04 +000030 && "Offset range already occupied!");
Owen Anderson124e1822011-09-22 22:32:22 +000031
Ahmed Bougachaef993562013-05-24 01:07:04 +000032 // Insert the new atom to the list.
33 Atoms.insert(I, NewAtom);
34}
35
36MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) {
37 MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End);
38 map(NewAtom);
39 return NewAtom;
40}
41
42MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) {
43 MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End);
44 map(NewAtom);
Owen Anderson124e1822011-09-22 22:32:22 +000045 return NewAtom;
46}
47
48// remap - Update the interval mapping for an atom.
49void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) {
50 // Find and erase the old mapping.
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000051 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
52 Atom->Begin, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000053 assert(I != atom_end() && "Atom offset not found in module!");
Owen Anderson124e1822011-09-22 22:32:22 +000054 assert(*I == Atom && "Previous atom mapping was invalid!");
Ahmed Bougachaef993562013-05-24 01:07:04 +000055 Atoms.erase(I);
Owen Anderson124e1822011-09-22 22:32:22 +000056
57 // Insert the new mapping.
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000058 AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(),
59 NewBegin, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000060 Atoms.insert(NewI, Atom);
Owen Anderson124e1822011-09-22 22:32:22 +000061
62 // Update the atom internal bounds.
63 Atom->Begin = NewBegin;
64 Atom->End = NewEnd;
65}
66
Ahmed Bougachaef993562013-05-24 01:07:04 +000067const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000068 AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(),
69 Addr, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000070 if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
71 return *I;
72 return 0;
73}
74
75MCAtom *MCModule::findAtomContaining(uint64_t Addr) {
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000076 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
77 Addr, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000078 if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
79 return *I;
80 return 0;
81}
82
83MCFunction *MCModule::createFunction(const StringRef &Name) {
84 Functions.push_back(new MCFunction(Name));
85 return Functions.back();
86}
87
88MCModule::~MCModule() {
89 for (AtomListTy::iterator AI = atom_begin(),
90 AE = atom_end();
91 AI != AE; ++AI)
92 delete *AI;
93 for (FunctionListTy::iterator FI = func_begin(),
94 FE = func_end();
95 FI != FE; ++FI)
96 delete *FI;
97}