Start stubbing out MCModule and MCAtom, which provide an API for accessing the rich...
[oota-llvm.git] / lib / MC / MCAtom.cpp
1 //===- lib/MC/MCAtom.cpp - MCAtom implementation --------------------------===//
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
10 #include "llvm/MC/MCAtom.h"
11 #include "llvm/MC/MCModule.h"
12 #include "llvm/Support/ErrorHandling.h"
13
14 using namespace llvm;
15
16 MCAtom *MCAtom::split(uint64_t SplitPt) {
17   assert((SplitPt > Begin && SplitPt <= End) &&
18          "Splitting at point not contained in atom!");
19
20   // Compute the new begin/end points.
21   uint64_t LeftBegin = Begin;
22   uint64_t LeftEnd = SplitPt - 1;
23   uint64_t RightBegin = SplitPt;
24   uint64_t RightEnd = End;
25
26   // Remap this atom to become the lower of the two new ones.
27   Parent->remap(this, LeftBegin, LeftEnd);
28
29   // Create a new atom for the higher atom.
30   MCAtom *RightAtom = Parent->createAtom(Type, RightBegin, RightEnd);
31
32   // Split the contents of the original atom between it and the new one.  The
33   // precise method depends on whether this is a data or a text atom.
34   if (isDataAtom()) {
35     std::vector<MCData>::iterator I = Data.begin() + (RightBegin - LeftBegin);
36
37     assert(I != Data.end() && "Split point not found in range!");
38
39     std::copy(I, Data.end(), RightAtom->Data.end());
40     Data.erase(I, Data.end());
41   } else if (isTextAtom()) {
42     std::vector<std::pair<uint64_t, MCInst> >::iterator I = Text.begin();
43
44     while (I != Text.end() && I->first < SplitPt) ++I;
45
46     assert(I != Text.end() && "Split point not found in disassembly!");
47     assert(I->first == SplitPt &&
48            "Split point does not fall on instruction boundary!");
49
50     std::copy(I, Text.end(), RightAtom->Text.end());
51     Text.erase(I, Text.end());
52   } else
53     llvm_unreachable("Unknown atom type!");
54
55   return RightAtom;
56 }
57
58 void MCAtom::truncate(uint64_t TruncPt) {
59   assert((TruncPt >= Begin && TruncPt < End) &&
60          "Truncation point not contained in atom!");
61
62   Parent->remap(this, Begin, TruncPt);
63
64   if (isDataAtom()) {
65     Data.resize(TruncPt - Begin + 1);
66   } else if (isTextAtom()) {
67     std::vector<std::pair<uint64_t, MCInst> >::iterator I = Text.begin();
68
69     while (I != Text.end() && I->first <= TruncPt) ++I;
70
71     assert(I != Text.end() && "Truncation point not found in disassembly!");
72     assert(I->first == TruncPt+1 &&
73            "Truncation point does not fall on instruction boundary");
74
75     Text.erase(I, Text.end());
76   } else
77     llvm_unreachable("Unknown atom type!");
78 }
79