1 //===- MCDwarf.h - Machine Code Dwarf support -------------------*- 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 MCDwarfFile to support the dwarf
11 // .file directive and the .loc directive.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_MC_MCDWARF_H
16 #define LLVM_MC_MCDWARF_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/CodeGen/MachineLocation.h" // FIXME
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/Dwarf.h"
33 class MCObjectStreamer;
36 /// MCDwarfFile - Instances of this class represent the name of the dwarf
37 /// .file directive and its associated dwarf file number in the MC file,
38 /// and MCDwarfFile's are created and unique'd by the MCContext class where
39 /// the file number for each is its index into the vector of DwarfFiles (note
40 /// index 0 is not used and not a valid dwarf file number).
42 // Name - the base name of the file without its directory path.
43 // The StringRef references memory allocated in the MCContext.
46 // DirIndex - the index into the list of directory names for this file name.
49 private: // MCContext creates and uniques these.
50 friend class MCContext;
51 MCDwarfFile(StringRef name, unsigned dirIndex)
52 : Name(name), DirIndex(dirIndex) {}
54 MCDwarfFile(const MCDwarfFile&); // DO NOT IMPLEMENT
55 void operator=(const MCDwarfFile&); // DO NOT IMPLEMENT
57 /// getName - Get the base name of this MCDwarfFile.
58 StringRef getName() const { return Name; }
60 /// getDirIndex - Get the dirIndex of this MCDwarfFile.
61 unsigned getDirIndex() const { return DirIndex; }
64 /// print - Print the value to the stream \arg OS.
65 void print(raw_ostream &OS) const;
67 /// dump - Print the value to stderr.
71 inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
76 /// MCDwarfLoc - Instances of this class represent the information from a
77 /// dwarf .loc directive.
79 // FileNum - the file number.
81 // Line - the line number.
83 // Column - the column position.
85 // Flags (see #define's below)
90 unsigned Discriminator;
92 // Flag that indicates the initial value of the is_stmt_start flag.
93 #define DWARF2_LINE_DEFAULT_IS_STMT 1
95 #define DWARF2_FLAG_IS_STMT (1 << 0)
96 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
97 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
98 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
100 private: // MCContext manages these
101 friend class MCContext;
102 friend class MCLineEntry;
103 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
104 unsigned isa, unsigned discriminator)
105 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
106 Discriminator(discriminator) {}
108 // Allow the default copy constructor and assignment operator to be used
109 // for an MCDwarfLoc object.
112 /// getFileNum - Get the FileNum of this MCDwarfLoc.
113 unsigned getFileNum() const { return FileNum; }
115 /// getLine - Get the Line of this MCDwarfLoc.
116 unsigned getLine() const { return Line; }
118 /// getColumn - Get the Column of this MCDwarfLoc.
119 unsigned getColumn() const { return Column; }
121 /// getFlags - Get the Flags of this MCDwarfLoc.
122 unsigned getFlags() const { return Flags; }
124 /// getIsa - Get the Isa of this MCDwarfLoc.
125 unsigned getIsa() const { return Isa; }
127 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
128 unsigned getDiscriminator() const { return Discriminator; }
130 /// setFileNum - Set the FileNum of this MCDwarfLoc.
131 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
133 /// setLine - Set the Line of this MCDwarfLoc.
134 void setLine(unsigned line) { Line = line; }
136 /// setColumn - Set the Column of this MCDwarfLoc.
137 void setColumn(unsigned column) { Column = column; }
139 /// setFlags - Set the Flags of this MCDwarfLoc.
140 void setFlags(unsigned flags) { Flags = flags; }
142 /// setIsa - Set the Isa of this MCDwarfLoc.
143 void setIsa(unsigned isa) { Isa = isa; }
145 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
146 void setDiscriminator(unsigned discriminator) {
147 Discriminator = discriminator;
151 /// MCLineEntry - Instances of this class represent the line information for
152 /// the dwarf line table entries. Which is created after a machine
153 /// instruction is assembled and uses an address from a temporary label
154 /// created at the current address in the current section and the info from
155 /// the last .loc directive seen as stored in the context.
156 class MCLineEntry : public MCDwarfLoc {
160 // Allow the default copy constructor and assignment operator to be used
161 // for an MCLineEntry object.
164 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
165 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
168 MCSymbol *getLabel() const { return Label; }
170 // This is called when an instruction is assembled into the specified
171 // section and if there is information from the last .loc directive that
172 // has yet to have a line entry made for it is made.
173 static void Make(MCStreamer *MCOS, const MCSection *Section);
176 /// MCLineSection - Instances of this class represent the line information
177 /// for a section where machine instructions have been assembled after seeing
178 /// .loc directives. This is the information used to build the dwarf line
179 /// table for a section.
180 class MCLineSection {
183 MCLineSection(const MCLineSection&); // DO NOT IMPLEMENT
184 void operator=(const MCLineSection&); // DO NOT IMPLEMENT
187 // Constructor to create an MCLineSection with an empty MCLineEntries
191 // addLineEntry - adds an entry to this MCLineSection's line entries
192 void addLineEntry(const MCLineEntry &LineEntry) {
193 MCLineEntries.push_back(LineEntry);
196 typedef std::vector<MCLineEntry> MCLineEntryCollection;
197 typedef MCLineEntryCollection::iterator iterator;
198 typedef MCLineEntryCollection::const_iterator const_iterator;
201 MCLineEntryCollection MCLineEntries;
204 const MCLineEntryCollection *getMCLineEntries() const {
205 return &MCLineEntries;
209 class MCDwarfFileTable {
212 // This emits the Dwarf file and the line tables.
214 static void Emit(MCStreamer *MCOS);
217 class MCDwarfLineAddr {
219 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
220 static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
222 /// Utility function to emit the encoding to a streamer.
223 static void Emit(MCStreamer *MCOS,
224 int64_t LineDelta,uint64_t AddrDelta);
226 /// Utility function to write the encoding to an object writer.
227 static void Write(MCObjectWriter *OW,
228 int64_t LineDelta, uint64_t AddrDelta);
231 class MCCFIInstruction {
233 enum OpType { Remember, Restore, Move };
237 // Move to & from location.
238 MachineLocation Destination;
239 MachineLocation Source;
241 MCCFIInstruction(OpType Op, MCSymbol *L)
242 : Operation(Op), Label(L) {
243 assert(Op == Remember || Op == Restore);
245 MCCFIInstruction(MCSymbol *L, const MachineLocation &D,
246 const MachineLocation &S)
247 : Operation(Move), Label(L), Destination(D), Source(S) {
249 OpType getOperation() const { return Operation; }
250 MCSymbol *getLabel() const { return Label; }
251 const MachineLocation &getDestination() const { return Destination; }
252 const MachineLocation &getSource() const { return Source; }
255 struct MCDwarfFrameInfo {
256 MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
257 Instructions(), PersonalityEncoding(0),
261 const MCSymbol *Personality;
262 const MCSymbol *Lsda;
263 std::vector<MCCFIInstruction> Instructions;
264 unsigned PersonalityEncoding;
265 unsigned LsdaEncoding;
268 class MCDwarfFrameEmitter {
271 // This emits the frame info section.
273 static void Emit(MCStreamer &streamer);
274 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
275 static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
277 } // end namespace llvm