blob: 9a9d90e5b6a71027d09888f49c0008ae9480393b [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
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +000057 // FIXME: special case NewBegin == Atom->Begin
58
Owen Anderson124e1822011-09-22 22:32:22 +000059 // Insert the new mapping.
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000060 AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(),
61 NewBegin, AtomComp);
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +000062 assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End)
63 && "Offset range already occupied!");
Ahmed Bougachaef993562013-05-24 01:07:04 +000064 Atoms.insert(NewI, Atom);
Owen Anderson124e1822011-09-22 22:32:22 +000065
66 // Update the atom internal bounds.
67 Atom->Begin = NewBegin;
68 Atom->End = NewEnd;
69}
70
Ahmed Bougachaef993562013-05-24 01:07:04 +000071const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000072 AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(),
73 Addr, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000074 if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
75 return *I;
76 return 0;
77}
78
79MCAtom *MCModule::findAtomContaining(uint64_t Addr) {
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000080 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
81 Addr, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000082 if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
83 return *I;
84 return 0;
85}
86
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +000087MCFunction *MCModule::createFunction(StringRef Name) {
88 Functions.push_back(new MCFunction(Name, this));
Ahmed Bougachaef993562013-05-24 01:07:04 +000089 return Functions.back();
90}
91
92MCModule::~MCModule() {
93 for (AtomListTy::iterator AI = atom_begin(),
94 AE = atom_end();
95 AI != AE; ++AI)
96 delete *AI;
97 for (FunctionListTy::iterator FI = func_begin(),
98 FE = func_end();
99 FI != FE; ++FI)
100 delete *FI;
101}