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/MC/MachineLocation.h"
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/Dwarf.h"
32 class MCObjectStreamer;
37 /// MCDwarfFile - Instances of this class represent the name of the dwarf
38 /// .file directive and its associated dwarf file number in the MC file,
39 /// and MCDwarfFile's are created and unique'd by the MCContext class where
40 /// the file number for each is its index into the vector of DwarfFiles (note
41 /// index 0 is not used and not a valid dwarf file number).
43 // Name - the base name of the file without its directory path.
44 // The StringRef references memory allocated in the MCContext.
47 // DirIndex - the index into the list of directory names for this file name.
50 private: // MCContext creates and uniques these.
51 friend class MCContext;
52 MCDwarfFile(StringRef name, unsigned dirIndex)
53 : Name(name), DirIndex(dirIndex) {}
55 MCDwarfFile(const MCDwarfFile&); // DO NOT IMPLEMENT
56 void operator=(const MCDwarfFile&); // DO NOT IMPLEMENT
58 /// getName - Get the base name of this MCDwarfFile.
59 StringRef getName() const { return Name; }
61 /// getDirIndex - Get the dirIndex of this MCDwarfFile.
62 unsigned getDirIndex() const { return DirIndex; }
65 /// print - Print the value to the stream \arg OS.
66 void print(raw_ostream &OS) const;
68 /// dump - Print the value to stderr.
72 inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
77 /// MCDwarfLoc - Instances of this class represent the information from a
78 /// dwarf .loc directive.
80 // FileNum - the file number.
82 // Line - the line number.
84 // Column - the column position.
86 // Flags (see #define's below)
91 unsigned Discriminator;
93 // Flag that indicates the initial value of the is_stmt_start flag.
94 #define DWARF2_LINE_DEFAULT_IS_STMT 1
96 #define DWARF2_FLAG_IS_STMT (1 << 0)
97 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
98 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
99 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
101 private: // MCContext manages these
102 friend class MCContext;
103 friend class MCLineEntry;
104 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
105 unsigned isa, unsigned discriminator)
106 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
107 Discriminator(discriminator) {}
109 // Allow the default copy constructor and assignment operator to be used
110 // for an MCDwarfLoc object.
113 /// getFileNum - Get the FileNum of this MCDwarfLoc.
114 unsigned getFileNum() const { return FileNum; }
116 /// getLine - Get the Line of this MCDwarfLoc.
117 unsigned getLine() const { return Line; }
119 /// getColumn - Get the Column of this MCDwarfLoc.
120 unsigned getColumn() const { return Column; }
122 /// getFlags - Get the Flags of this MCDwarfLoc.
123 unsigned getFlags() const { return Flags; }
125 /// getIsa - Get the Isa of this MCDwarfLoc.
126 unsigned getIsa() const { return Isa; }
128 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
129 unsigned getDiscriminator() const { return Discriminator; }
131 /// setFileNum - Set the FileNum of this MCDwarfLoc.
132 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
134 /// setLine - Set the Line of this MCDwarfLoc.
135 void setLine(unsigned line) { Line = line; }
137 /// setColumn - Set the Column of this MCDwarfLoc.
138 void setColumn(unsigned column) { Column = column; }
140 /// setFlags - Set the Flags of this MCDwarfLoc.
141 void setFlags(unsigned flags) { Flags = flags; }
143 /// setIsa - Set the Isa of this MCDwarfLoc.
144 void setIsa(unsigned isa) { Isa = isa; }
146 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
147 void setDiscriminator(unsigned discriminator) {
148 Discriminator = discriminator;
152 /// MCLineEntry - Instances of this class represent the line information for
153 /// the dwarf line table entries. Which is created after a machine
154 /// instruction is assembled and uses an address from a temporary label
155 /// created at the current address in the current section and the info from
156 /// the last .loc directive seen as stored in the context.
157 class MCLineEntry : public MCDwarfLoc {
161 // Allow the default copy constructor and assignment operator to be used
162 // for an MCLineEntry object.
165 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
166 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
169 MCSymbol *getLabel() const { return Label; }
171 // This is called when an instruction is assembled into the specified
172 // section and if there is information from the last .loc directive that
173 // has yet to have a line entry made for it is made.
174 static void Make(MCStreamer *MCOS, const MCSection *Section);
177 /// MCLineSection - Instances of this class represent the line information
178 /// for a section where machine instructions have been assembled after seeing
179 /// .loc directives. This is the information used to build the dwarf line
180 /// table for a section.
181 class MCLineSection {
184 MCLineSection(const MCLineSection&); // DO NOT IMPLEMENT
185 void operator=(const MCLineSection&); // DO NOT IMPLEMENT
188 // Constructor to create an MCLineSection with an empty MCLineEntries
192 // addLineEntry - adds an entry to this MCLineSection's line entries
193 void addLineEntry(const MCLineEntry &LineEntry) {
194 MCLineEntries.push_back(LineEntry);
197 typedef std::vector<MCLineEntry> MCLineEntryCollection;
198 typedef MCLineEntryCollection::iterator iterator;
199 typedef MCLineEntryCollection::const_iterator const_iterator;
202 MCLineEntryCollection MCLineEntries;
205 const MCLineEntryCollection *getMCLineEntries() const {
206 return &MCLineEntries;
210 class MCDwarfFileTable {
213 // This emits the Dwarf file and the line tables.
215 static const MCSymbol *Emit(MCStreamer *MCOS);
218 class MCDwarfLineAddr {
220 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
221 static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
223 /// Utility function to emit the encoding to a streamer.
224 static void Emit(MCStreamer *MCOS,
225 int64_t LineDelta,uint64_t AddrDelta);
227 /// Utility function to write the encoding to an object writer.
228 static void Write(MCObjectWriter *OW,
229 int64_t LineDelta, uint64_t AddrDelta);
232 class MCGenDwarfInfo {
235 // When generating dwarf for assembly source files this emits the Dwarf
238 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
241 // When generating dwarf for assembly source files this is the info that is
242 // needed to be gathered for each symbol that will have a dwarf label.
243 class MCGenDwarfLabelEntry {
245 // Name of the symbol without a leading underbar, if any.
247 // The dwarf file number this symbol is in.
249 // The line number this symbol is at.
251 // The low_pc for the dwarf label is taken from this symbol.
255 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber,
256 unsigned lineNumber, MCSymbol *label) :
257 Name(name), FileNumber(fileNumber), LineNumber(lineNumber), Label(label){}
259 StringRef getName() const { return Name; }
260 unsigned getFileNumber() const { return FileNumber; }
261 unsigned getLineNumber() const { return LineNumber; }
262 MCSymbol *getLabel() const { return Label; }
264 // This is called when label is created when we are generating dwarf for
265 // assembly source files.
266 static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
270 class MCCFIInstruction {
272 enum OpType { SameValue, RememberState, RestoreState, Move, RelMove, Escape,
277 // Move to & from location.
278 MachineLocation Destination;
279 MachineLocation Source;
280 std::vector<char> Values;
282 MCCFIInstruction(OpType Op, MCSymbol *L)
283 : Operation(Op), Label(L) {
284 assert(Op == RememberState || Op == RestoreState);
286 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned Register)
287 : Operation(Op), Label(L), Destination(Register) {
288 assert(Op == SameValue || Op == Restore);
290 MCCFIInstruction(MCSymbol *L, const MachineLocation &D,
291 const MachineLocation &S)
292 : Operation(Move), Label(L), Destination(D), Source(S) {
294 MCCFIInstruction(OpType Op, MCSymbol *L, const MachineLocation &D,
295 const MachineLocation &S)
296 : Operation(Op), Label(L), Destination(D), Source(S) {
297 assert(Op == RelMove);
299 MCCFIInstruction(OpType Op, MCSymbol *L, StringRef Vals)
300 : Operation(Op), Label(L), Values(Vals.begin(), Vals.end()) {
301 assert(Op == Escape);
303 OpType getOperation() const { return Operation; }
304 MCSymbol *getLabel() const { return Label; }
305 const MachineLocation &getDestination() const { return Destination; }
306 const MachineLocation &getSource() const { return Source; }
307 const StringRef getValues() const {
308 return StringRef(&Values[0], Values.size());
312 struct MCDwarfFrameInfo {
313 MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
314 Function(0), Instructions(), PersonalityEncoding(),
315 LsdaEncoding(0), CompactUnwindEncoding(0),
316 IsSignalFrame(false) {}
319 const MCSymbol *Personality;
320 const MCSymbol *Lsda;
321 const MCSymbol *Function;
322 std::vector<MCCFIInstruction> Instructions;
323 unsigned PersonalityEncoding;
324 unsigned LsdaEncoding;
325 uint32_t CompactUnwindEncoding;
329 class MCDwarfFrameEmitter {
332 // This emits the frame info section.
334 static void Emit(MCStreamer &streamer, bool usingCFI,
336 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
337 static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
339 } // end namespace llvm