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/Support/Compiler.h"
20 #include "llvm/Support/Dwarf.h"
21 #include "llvm/Support/raw_ostream.h"
34 /// MCDwarfFile - Instances of this class represent the name of the dwarf
35 /// .file directive and its associated dwarf file number in the MC file,
36 /// and MCDwarfFile's are created and unique'd by the MCContext class where
37 /// the file number for each is its index into the vector of DwarfFiles (note
38 /// index 0 is not used and not a valid dwarf file number).
40 // Name - the base name of the file without its directory path.
41 // The StringRef references memory allocated in the MCContext.
44 // DirIndex - the index into the list of directory names for this file name.
47 private: // MCContext creates and uniques these.
48 friend class MCContext;
49 MCDwarfFile(StringRef name, unsigned dirIndex)
50 : Name(name), DirIndex(dirIndex) {}
52 MCDwarfFile(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
53 void operator=(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
55 /// getName - Get the base name of this MCDwarfFile.
56 StringRef getName() const { return Name; }
58 /// getDirIndex - Get the dirIndex of this MCDwarfFile.
59 unsigned getDirIndex() const { return DirIndex; }
62 /// print - Print the value to the stream \p OS.
63 void print(raw_ostream &OS) const;
65 /// dump - Print the value to stderr.
69 inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
74 /// MCDwarfLoc - Instances of this class represent the information from a
75 /// dwarf .loc directive.
77 // FileNum - the file number.
79 // Line - the line number.
81 // Column - the column position.
83 // Flags (see #define's below)
88 unsigned Discriminator;
90 // Flag that indicates the initial value of the is_stmt_start flag.
91 #define DWARF2_LINE_DEFAULT_IS_STMT 1
93 #define DWARF2_FLAG_IS_STMT (1 << 0)
94 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
95 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
96 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
98 private: // MCContext manages these
99 friend class MCContext;
100 friend class MCLineEntry;
101 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
102 unsigned isa, unsigned discriminator)
103 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
104 Discriminator(discriminator) {}
106 // Allow the default copy constructor and assignment operator to be used
107 // for an MCDwarfLoc object.
110 /// getFileNum - Get the FileNum of this MCDwarfLoc.
111 unsigned getFileNum() const { return FileNum; }
113 /// getLine - Get the Line of this MCDwarfLoc.
114 unsigned getLine() const { return Line; }
116 /// getColumn - Get the Column of this MCDwarfLoc.
117 unsigned getColumn() const { return Column; }
119 /// getFlags - Get the Flags of this MCDwarfLoc.
120 unsigned getFlags() const { return Flags; }
122 /// getIsa - Get the Isa of this MCDwarfLoc.
123 unsigned getIsa() const { return Isa; }
125 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
126 unsigned getDiscriminator() const { return Discriminator; }
128 /// setFileNum - Set the FileNum of this MCDwarfLoc.
129 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
131 /// setLine - Set the Line of this MCDwarfLoc.
132 void setLine(unsigned line) { Line = line; }
134 /// setColumn - Set the Column of this MCDwarfLoc.
135 void setColumn(unsigned column) { Column = column; }
137 /// setFlags - Set the Flags of this MCDwarfLoc.
138 void setFlags(unsigned flags) { Flags = flags; }
140 /// setIsa - Set the Isa of this MCDwarfLoc.
141 void setIsa(unsigned isa) { Isa = isa; }
143 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
144 void setDiscriminator(unsigned discriminator) {
145 Discriminator = discriminator;
149 /// MCLineEntry - Instances of this class represent the line information for
150 /// the dwarf line table entries. Which is created after a machine
151 /// instruction is assembled and uses an address from a temporary label
152 /// created at the current address in the current section and the info from
153 /// the last .loc directive seen as stored in the context.
154 class MCLineEntry : public MCDwarfLoc {
158 // Allow the default copy constructor and assignment operator to be used
159 // for an MCLineEntry object.
162 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
163 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
166 MCSymbol *getLabel() const { return Label; }
168 // This is called when an instruction is assembled into the specified
169 // section and if there is information from the last .loc directive that
170 // has yet to have a line entry made for it is made.
171 static void Make(MCStreamer *MCOS, const MCSection *Section);
174 /// MCLineSection - Instances of this class represent the line information
175 /// for a section where machine instructions have been assembled after seeing
176 /// .loc directives. This is the information used to build the dwarf line
177 /// table for a section.
178 class MCLineSection {
181 MCLineSection(const MCLineSection&) LLVM_DELETED_FUNCTION;
182 void operator=(const MCLineSection&) LLVM_DELETED_FUNCTION;
185 // Constructor to create an MCLineSection with an empty MCLineEntries
189 // addLineEntry - adds an entry to this MCLineSection's line entries
190 void addLineEntry(const MCLineEntry &LineEntry, unsigned CUID) {
191 MCLineDivisions[CUID].push_back(LineEntry);
194 typedef std::vector<MCLineEntry> MCLineEntryCollection;
195 typedef MCLineEntryCollection::iterator iterator;
196 typedef MCLineEntryCollection::const_iterator const_iterator;
197 typedef std::map<unsigned, MCLineEntryCollection> MCLineDivisionMap;
200 // A collection of MCLineEntry for each Compile Unit ID.
201 MCLineDivisionMap MCLineDivisions;
204 // Returns whether MCLineSection contains entries for a given Compile
206 bool containEntriesForID(unsigned CUID) const {
207 return MCLineDivisions.count(CUID);
209 // Returns the collection of MCLineEntry for a given Compile Unit ID.
210 const MCLineEntryCollection &getMCLineEntries(unsigned CUID) const {
211 MCLineDivisionMap::const_iterator CIter = MCLineDivisions.find(CUID);
212 assert(CIter != MCLineDivisions.end());
213 return CIter->second;
217 class MCDwarfFileTable {
220 // This emits the Dwarf file and the line tables for all Compile Units.
222 static const MCSymbol *Emit(MCStreamer *MCOS);
224 // This emits the Dwarf file and the line tables for a given Compile Unit.
226 static const MCSymbol *EmitCU(MCStreamer *MCOS, unsigned ID);
229 class MCDwarfLineAddr {
231 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
232 static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
234 /// Utility function to emit the encoding to a streamer.
235 static void Emit(MCStreamer *MCOS,
236 int64_t LineDelta,uint64_t AddrDelta);
238 /// Utility function to write the encoding to an object writer.
239 static void Write(MCObjectWriter *OW,
240 int64_t LineDelta, uint64_t AddrDelta);
243 class MCGenDwarfInfo {
246 // When generating dwarf for assembly source files this emits the Dwarf
249 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
252 // When generating dwarf for assembly source files this is the info that is
253 // needed to be gathered for each symbol that will have a dwarf label.
254 class MCGenDwarfLabelEntry {
256 // Name of the symbol without a leading underbar, if any.
258 // The dwarf file number this symbol is in.
260 // The line number this symbol is at.
262 // The low_pc for the dwarf label is taken from this symbol.
266 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber,
267 unsigned lineNumber, MCSymbol *label) :
268 Name(name), FileNumber(fileNumber), LineNumber(lineNumber), Label(label){}
270 StringRef getName() const { return Name; }
271 unsigned getFileNumber() const { return FileNumber; }
272 unsigned getLineNumber() const { return LineNumber; }
273 MCSymbol *getLabel() const { return Label; }
275 // This is called when label is created when we are generating dwarf for
276 // assembly source files.
277 static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
281 class MCCFIInstruction {
283 enum OpType { OpSameValue, OpRememberState, OpRestoreState, OpOffset,
284 OpDefCfaRegister, OpDefCfaOffset, OpDefCfa, OpRelOffset,
285 OpAdjustCfaOffset, OpEscape, OpRestore, OpUndefined,
295 std::vector<char> Values;
297 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V) :
298 Operation(Op), Label(L), Register(R), Offset(O),
299 Values(V.begin(), V.end()) {
300 assert(Op != OpRegister);
303 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2) :
304 Operation(Op), Label(L), Register(R1), Register2(R2) {
305 assert(Op == OpRegister);
309 static MCCFIInstruction
310 createOffset(MCSymbol *L, unsigned Register, int Offset) {
311 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
314 static MCCFIInstruction
315 createDefCfaRegister(MCSymbol *L, unsigned Register) {
316 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
319 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
320 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
323 static MCCFIInstruction
324 createDefCfa(MCSymbol *L, unsigned Register, int Offset) {
325 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
328 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
329 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
332 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
333 return MCCFIInstruction(OpRestore, L, Register, 0, "");
336 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
337 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
340 static MCCFIInstruction createRestoreState(MCSymbol *L) {
341 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
344 static MCCFIInstruction createRememberState(MCSymbol *L) {
345 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
348 static MCCFIInstruction
349 createRelOffset(MCSymbol *L, unsigned Register, int Offset) {
350 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
353 static MCCFIInstruction
354 createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
355 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
358 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
359 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
362 static MCCFIInstruction
363 createRegister(MCSymbol *L, unsigned Register1, unsigned Register2) {
364 return MCCFIInstruction(OpRegister, L, Register1, Register2);
367 OpType getOperation() const { return Operation; }
368 MCSymbol *getLabel() const { return Label; }
370 unsigned getRegister() const {
371 assert(Operation == OpDefCfa || Operation == OpOffset ||
372 Operation == OpRestore || Operation == OpUndefined ||
373 Operation == OpSameValue || Operation == OpDefCfaRegister ||
374 Operation == OpRelOffset || Operation == OpRegister);
378 unsigned getRegister2() const {
379 assert(Operation == OpRegister);
383 int getOffset() const {
384 assert(Operation == OpDefCfa || Operation == OpOffset ||
385 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
386 Operation == OpAdjustCfaOffset);
390 const StringRef getValues() const {
391 assert(Operation == OpEscape);
392 return StringRef(&Values[0], Values.size());
396 struct MCDwarfFrameInfo {
397 MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
398 Function(0), Instructions(), PersonalityEncoding(),
399 LsdaEncoding(0), CompactUnwindEncoding(0),
400 IsSignalFrame(false) {}
403 const MCSymbol *Personality;
404 const MCSymbol *Lsda;
405 const MCSymbol *Function;
406 std::vector<MCCFIInstruction> Instructions;
407 unsigned PersonalityEncoding;
408 unsigned LsdaEncoding;
409 uint32_t CompactUnwindEncoding;
413 class MCDwarfFrameEmitter {
416 // This emits the frame info section.
418 static void Emit(MCStreamer &streamer, bool usingCFI,
420 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
421 static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
423 } // end namespace llvm