LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCAtom.cpp
Go to the documentation of this file.
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"
13 #include <iterator>
14 
15 using namespace llvm;
16 
17 // Pin the vtable to this file.
18 void MCAtom::anchor() {}
19 
20 void MCAtom::remap(uint64_t NewBegin, uint64_t NewEnd) {
21  Parent->remap(this, NewBegin, NewEnd);
22 }
23 
24 void MCAtom::remapForTruncate(uint64_t TruncPt) {
25  assert((TruncPt >= Begin && TruncPt < End) &&
26  "Truncation point not contained in atom!");
27  remap(Begin, TruncPt);
28 }
29 
30 void MCAtom::remapForSplit(uint64_t SplitPt,
31  uint64_t &LBegin, uint64_t &LEnd,
32  uint64_t &RBegin, uint64_t &REnd) {
33  assert((SplitPt > Begin && SplitPt <= End) &&
34  "Splitting at point not contained in atom!");
35 
36  // Compute the new begin/end points.
37  LBegin = Begin;
38  LEnd = SplitPt - 1;
39  RBegin = SplitPt;
40  REnd = End;
41 
42  // Remap this atom to become the lower of the two new ones.
43  remap(LBegin, LEnd);
44 }
45 
46 // MCDataAtom
47 
48 void MCDataAtom::addData(const MCData &D) {
49  Data.push_back(D);
50  if (Data.size() > End + 1 - Begin)
51  remap(Begin, End + 1);
52 }
53 
54 void MCDataAtom::truncate(uint64_t TruncPt) {
55  remapForTruncate(TruncPt);
56 
57  Data.resize(TruncPt - Begin + 1);
58 }
59 
60 MCDataAtom *MCDataAtom::split(uint64_t SplitPt) {
61  uint64_t LBegin, LEnd, RBegin, REnd;
62  remapForSplit(SplitPt, LBegin, LEnd, RBegin, REnd);
63 
64  MCDataAtom *RightAtom = Parent->createDataAtom(RBegin, REnd);
65  RightAtom->setName(getName());
66 
67  std::vector<MCData>::iterator I = Data.begin() + (RBegin - LBegin);
68  assert(I != Data.end() && "Split point not found in range!");
69 
70  std::copy(I, Data.end(), std::back_inserter(RightAtom->Data));
71  Data.erase(I, Data.end());
72  return RightAtom;
73 }
74 
75 // MCTextAtom
76 
77 void MCTextAtom::addInst(const MCInst &I, uint64_t Size) {
78  if (NextInstAddress + Size - 1 > End)
79  remap(Begin, NextInstAddress + Size - 1);
80  Insts.push_back(MCDecodedInst(I, NextInstAddress, Size));
81  NextInstAddress += Size;
82 }
83 
84 void MCTextAtom::truncate(uint64_t TruncPt) {
85  remapForTruncate(TruncPt);
86 
87  InstListTy::iterator I = Insts.begin();
88  while (I != Insts.end() && I->Address <= TruncPt) ++I;
89 
90  assert(I != Insts.end() && "Truncation point not found in disassembly!");
91  assert(I->Address == TruncPt + 1 &&
92  "Truncation point does not fall on instruction boundary");
93 
94  Insts.erase(I, Insts.end());
95 }
96 
97 MCTextAtom *MCTextAtom::split(uint64_t SplitPt) {
98  uint64_t LBegin, LEnd, RBegin, REnd;
99  remapForSplit(SplitPt, LBegin, LEnd, RBegin, REnd);
100 
101  MCTextAtom *RightAtom = Parent->createTextAtom(RBegin, REnd);
102  RightAtom->setName(getName());
103 
104  InstListTy::iterator I = Insts.begin();
105  while (I != Insts.end() && I->Address < SplitPt) ++I;
106  assert(I != Insts.end() && "Split point not found in disassembly!");
107  assert(I->Address == SplitPt &&
108  "Split point does not fall on instruction boundary!");
109 
110  std::copy(I, Insts.end(), std::back_inserter(RightAtom->Insts));
111  Insts.erase(I, Insts.end());
112  Parent->splitBasicBlocksForAtom(this, RightAtom);
113  return RightAtom;
114 }
void truncate(uint64_t TruncPt) LLVM_OVERRIDE
Truncates an atom, discarding everything after TruncPt.
Definition: MCAtom.cpp:84
An atom consisting of disassembled instructions.
Definition: MCAtom.h:123
StringRef getName() const
Definition: MCAtom.h:67
MCDataAtom * split(uint64_t SplitPt) LLVM_OVERRIDE
Splits the atom in two at a given address.
Definition: MCAtom.cpp:60
uint8_t MCData
An entry in an MCDataAtom.
Definition: MCAtom.h:167
void addInst(const MCInst &Inst, uint64_t Size)
Append an instruction, expanding the atom if necessary.
Definition: MCAtom.cpp:77
void remapForSplit(uint64_t SplitPt, uint64_t &LBegin, uint64_t &LEnd, uint64_t &RBegin, uint64_t &REnd)
Remap the atom to prepare for a split at SplitPt. The bounds for the resulting atoms are returned in ...
Definition: MCAtom.cpp:30
An atom consising of a sequence of bytes.
Definition: MCAtom.h:170
uint64_t Begin
Definition: MCAtom.h:75
An entry in an MCTextAtom: a disassembled instruction. NOTE: Both the Address and Size field are actu...
Definition: MCAtom.h:113
MCDataAtom * createDataAtom(uint64_t Begin, uint64_t End)
Definition: MCModule.cpp:46
MCTextAtom * createTextAtom(uint64_t Begin, uint64_t End)
Definition: MCModule.cpp:40
MCModule * Parent
Definition: MCAtom.h:74
void remap(uint64_t NewBegin, uint64_t NewEnd)
Remap the atom, using the given range, updating Begin/End. One or both of the bounds can remain the s...
Definition: MCAtom.cpp:20
uint64_t End
Definition: MCAtom.h:75
void setName(StringRef NewName)
Definition: MCAtom.h:68
void remapForTruncate(uint64_t TruncPt)
Remap the atom to prepare for a truncation at TruncPt. Equivalent to:
Definition: MCAtom.cpp:24
#define I(x, y, z)
Definition: MD5.cpp:54
void truncate(uint64_t TruncPt) LLVM_OVERRIDE
Truncates an atom, discarding everything after TruncPt.
Definition: MCAtom.cpp:54
MCTextAtom * split(uint64_t SplitPt) LLVM_OVERRIDE
Splits the atom in two at a given address.
Definition: MCAtom.cpp:97
void addData(const MCData &D)
Append a data entry, expanding the atom if necessary.
Definition: MCAtom.cpp:48