1 //===- lib/MC/MCAtom.cpp - MCAtom implementation --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCAtom.h"
11 #include "llvm/MC/MCModule.h"
12 #include "llvm/Support/ErrorHandling.h"
17 void MCAtom::remap(uint64_t NewBegin, uint64_t NewEnd) {
18 Parent->remap(this, NewBegin, NewEnd);
21 void MCAtom::remapForTruncate(uint64_t TruncPt) {
22 assert((TruncPt >= Begin && TruncPt < End) &&
23 "Truncation point not contained in atom!");
24 remap(Begin, TruncPt);
27 void MCAtom::remapForSplit(uint64_t SplitPt,
28 uint64_t &LBegin, uint64_t &LEnd,
29 uint64_t &RBegin, uint64_t &REnd) {
30 assert((SplitPt > Begin && SplitPt <= End) &&
31 "Splitting at point not contained in atom!");
33 // Compute the new begin/end points.
39 // Remap this atom to become the lower of the two new ones.
45 void MCDataAtom::addData(const MCData &D) {
47 if (Data.size() > Begin - End - 1)
48 remap(Begin, End + 1);
51 void MCDataAtom::truncate(uint64_t TruncPt) {
52 remapForTruncate(TruncPt);
54 Data.resize(TruncPt - Begin + 1);
57 MCDataAtom *MCDataAtom::split(uint64_t SplitPt) {
58 uint64_t LBegin, LEnd, RBegin, REnd;
59 remapForSplit(SplitPt, LBegin, LEnd, RBegin, REnd);
61 MCDataAtom *RightAtom = Parent->createDataAtom(RBegin, REnd);
62 RightAtom->setName(getName());
64 std::vector<MCData>::iterator I = Data.begin() + (RBegin - LBegin);
65 assert(I != Data.end() && "Split point not found in range!");
67 std::copy(I, Data.end(), std::back_inserter(RightAtom->Data));
68 Data.erase(I, Data.end());
74 void MCTextAtom::addInst(const MCInst &I, uint64_t Size) {
75 if (NextInstAddress + Size - 1 > End)
76 remap(Begin, NextInstAddress + Size - 1);
77 Insts.push_back(MCDecodedInst(I, NextInstAddress, Size));
78 NextInstAddress += Size;
81 void MCTextAtom::truncate(uint64_t TruncPt) {
82 remapForTruncate(TruncPt);
84 InstListTy::iterator I = Insts.begin();
85 while (I != Insts.end() && I->Address <= TruncPt) ++I;
87 assert(I != Insts.end() && "Truncation point not found in disassembly!");
88 assert(I->Address == TruncPt + 1 &&
89 "Truncation point does not fall on instruction boundary");
91 Insts.erase(I, Insts.end());
94 MCTextAtom *MCTextAtom::split(uint64_t SplitPt) {
95 uint64_t LBegin, LEnd, RBegin, REnd;
96 remapForSplit(SplitPt, LBegin, LEnd, RBegin, REnd);
98 MCTextAtom *RightAtom = Parent->createTextAtom(RBegin, REnd);
99 RightAtom->setName(getName());
101 InstListTy::iterator I = Insts.begin();
102 while (I != Insts.end() && I->Address < SplitPt) ++I;
103 assert(I != Insts.end() && "Split point not found in disassembly!");
104 assert(I->Address == SplitPt &&
105 "Split point does not fall on instruction boundary!");
107 std::copy(I, Insts.end(), std::back_inserter(RightAtom->Insts));
108 Insts.erase(I, Insts.end());
109 Parent->splitBasicBlocksForAtom(this, RightAtom);