1 //===-- llvm/MC/MCAtom.h ----------------------------------------*- C++ -*-===//
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 // This file contains the declaration of the MCAtom class, which is used to
11 // represent a contiguous region in a decoded object that is uniformly data or
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_MC_MCATOM_H
17 #define LLVM_MC_MCATOM_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/Support/DataTypes.h"
32 /// \brief Represents a contiguous range of either instructions (a TextAtom)
33 /// or data (a DataAtom). Address ranges are expressed as _closed_ intervals.
38 enum AtomKind { TextAtom, DataAtom };
39 AtomKind getKind() const { return Kind; }
41 /// \brief Get the start address of the atom.
42 uint64_t getBeginAddr() const { return Begin; }
43 /// \brief Get the end address, i.e. the last one inside the atom.
44 uint64_t getEndAddr() const { return End; }
46 /// \name Atom modification methods:
47 /// When modifying a TextAtom, keep instruction boundaries in mind.
48 /// For instance, split must me given the start address of an instruction.
51 /// \brief Splits the atom in two at a given address.
52 /// \param SplitPt Address at which to start a new atom, splitting this one.
53 /// \returns The newly created atom starting at \p SplitPt.
54 virtual MCAtom *split(uint64_t SplitPt) = 0;
56 /// \brief Truncates an atom, discarding everything after \p TruncPt.
57 /// \param TruncPt Last byte address to be contained in this atom.
58 virtual void truncate(uint64_t TruncPt) = 0;
63 /// This is mostly for display purposes, and may contain anything that hints
64 /// at what the atom contains: section or symbol name, BB start address, ..
66 StringRef getName() const { return Name; }
67 void setName(StringRef NewName) { Name = NewName.str(); }
76 friend class MCModule;
77 MCAtom(AtomKind K, MCModule *P, uint64_t B, uint64_t E)
78 : Kind(K), Name("(unknown)"), Parent(P), Begin(B), End(E) { }
80 /// \name Atom remapping helpers
83 /// \brief Remap the atom, using the given range, updating Begin/End.
84 /// One or both of the bounds can remain the same, but overlapping with other
85 /// atoms in the module is still forbidden.
86 void remap(uint64_t NewBegin, uint64_t NewEnd);
88 /// \brief Remap the atom to prepare for a truncation at TruncPt.
92 /// remap(Begin, TruncPt);
94 void remapForTruncate(uint64_t TruncPt);
96 /// \brief Remap the atom to prepare for a split at SplitPt.
97 /// The bounds for the resulting atoms are returned in {L,R}{Begin,End}.
98 /// The current atom is truncated to \p LEnd.
99 void remapForSplit(uint64_t SplitPt,
100 uint64_t &LBegin, uint64_t &LEnd,
101 uint64_t &RBegin, uint64_t &REnd);
108 /// \brief An entry in an MCTextAtom: a disassembled instruction.
109 /// NOTE: Both the Address and Size field are actually redundant when taken in
110 /// the context of the text atom, and may better be exposed in an iterator
111 /// instead of stored in the atom, which would replace this class.
112 class MCDecodedInst {
117 MCDecodedInst(const MCInst &Inst, uint64_t Address, uint64_t Size)
118 : Inst(Inst), Address(Address), Size(Size) {}
121 /// \brief An atom consisting of disassembled instructions.
122 class MCTextAtom : public MCAtom {
124 typedef std::vector<MCDecodedInst> InstListTy;
127 /// \brief The address of the next appended instruction, i.e., the
128 /// address immediately after the last instruction in the atom.
129 uint64_t NextInstAddress;
131 /// Append an instruction, expanding the atom if necessary.
132 void addInst(const MCInst &Inst, uint64_t Size);
134 /// \name Instruction list access
136 typedef InstListTy::const_iterator const_iterator;
137 const_iterator begin() const { return Insts.begin(); }
138 const_iterator end() const { return Insts.end(); }
140 const MCDecodedInst &back() const { return Insts.back(); }
141 const MCDecodedInst &at(size_t n) const { return Insts.at(n); }
142 size_t size() const { return Insts.size(); }
145 /// \name Atom type specific split/truncate logic.
147 MCTextAtom *split(uint64_t SplitPt) LLVM_OVERRIDE;
148 void truncate(uint64_t TruncPt) LLVM_OVERRIDE;
152 static bool classof(const MCAtom *A) { return A->getKind() == TextAtom; }
154 friend class MCModule;
155 // Private constructor - only callable by MCModule
156 MCTextAtom(MCModule *P, uint64_t Begin, uint64_t End)
157 : MCAtom(TextAtom, P, Begin, End), NextInstAddress(Begin) {}
164 /// \brief An entry in an MCDataAtom.
165 // NOTE: This may change to a more complex type in the future.
166 typedef uint8_t MCData;
168 /// \brief An atom consising of a sequence of bytes.
169 class MCDataAtom : public MCAtom {
170 std::vector<MCData> Data;
173 /// Append a data entry, expanding the atom if necessary.
174 void addData(const MCData &D);
176 /// Get a reference to the data in this atom.
177 ArrayRef<MCData> getData() const { return Data; }
179 /// \name Atom type specific split/truncate logic.
181 MCDataAtom *split(uint64_t SplitPt) LLVM_OVERRIDE;
182 void truncate(uint64_t TruncPt) LLVM_OVERRIDE;
186 static bool classof(const MCAtom *A) { return A->getKind() == DataAtom; }
188 friend class MCModule;
189 // Private constructor - only callable by MCModule
190 MCDataAtom(MCModule *P, uint64_t Begin, uint64_t End)
191 : MCAtom(DataAtom, P, Begin, End) {
192 Data.reserve(End + 1 - Begin);