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/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/raw_ostream.h"
39 /// MCDwarfFile - Instances of this class represent the name of the dwarf
40 /// .file directive and its associated dwarf file number in the MC file,
41 /// and MCDwarfFile's are created and unique'd by the MCContext class where
42 /// the file number for each is its index into the vector of DwarfFiles (note
43 /// index 0 is not used and not a valid dwarf file number).
45 // Name - the base name of the file without its directory path.
46 // The StringRef references memory allocated in the MCContext.
49 // DirIndex - the index into the list of directory names for this file name.
53 /// MCDwarfLoc - Instances of this class represent the information from a
54 /// dwarf .loc directive.
56 // FileNum - the file number.
58 // Line - the line number.
60 // Column - the column position.
62 // Flags (see #define's below)
67 unsigned Discriminator;
69 // Flag that indicates the initial value of the is_stmt_start flag.
70 #define DWARF2_LINE_DEFAULT_IS_STMT 1
72 #define DWARF2_FLAG_IS_STMT (1 << 0)
73 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
74 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
75 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
77 private: // MCContext manages these
78 friend class MCContext;
79 friend class MCLineEntry;
80 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
81 unsigned isa, unsigned discriminator)
82 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
83 Discriminator(discriminator) {}
85 // Allow the default copy constructor and assignment operator to be used
86 // for an MCDwarfLoc object.
89 /// getFileNum - Get the FileNum of this MCDwarfLoc.
90 unsigned getFileNum() const { return FileNum; }
92 /// getLine - Get the Line of this MCDwarfLoc.
93 unsigned getLine() const { return Line; }
95 /// getColumn - Get the Column of this MCDwarfLoc.
96 unsigned getColumn() const { return Column; }
98 /// getFlags - Get the Flags of this MCDwarfLoc.
99 unsigned getFlags() const { return Flags; }
101 /// getIsa - Get the Isa of this MCDwarfLoc.
102 unsigned getIsa() const { return Isa; }
104 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
105 unsigned getDiscriminator() const { return Discriminator; }
107 /// setFileNum - Set the FileNum of this MCDwarfLoc.
108 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
110 /// setLine - Set the Line of this MCDwarfLoc.
111 void setLine(unsigned line) { Line = line; }
113 /// setColumn - Set the Column of this MCDwarfLoc.
114 void setColumn(unsigned column) { Column = column; }
116 /// setFlags - Set the Flags of this MCDwarfLoc.
117 void setFlags(unsigned flags) { Flags = flags; }
119 /// setIsa - Set the Isa of this MCDwarfLoc.
120 void setIsa(unsigned isa) { Isa = isa; }
122 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
123 void setDiscriminator(unsigned discriminator) {
124 Discriminator = discriminator;
128 /// MCLineEntry - Instances of this class represent the line information for
129 /// the dwarf line table entries. Which is created after a machine
130 /// instruction is assembled and uses an address from a temporary label
131 /// created at the current address in the current section and the info from
132 /// the last .loc directive seen as stored in the context.
133 class MCLineEntry : public MCDwarfLoc {
137 // Allow the default copy constructor and assignment operator to be used
138 // for an MCLineEntry object.
141 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
142 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
143 : MCDwarfLoc(loc), Label(label) {}
145 MCSymbol *getLabel() const { return Label; }
147 // This is called when an instruction is assembled into the specified
148 // section and if there is information from the last .loc directive that
149 // has yet to have a line entry made for it is made.
150 static void Make(MCStreamer *MCOS, const MCSection *Section);
153 /// MCLineSection - Instances of this class represent the line information
154 /// for a compile unit where machine instructions have been assembled after seeing
155 /// .loc directives. This is the information used to build the dwarf line
156 /// table for a section.
157 class MCLineSection {
159 // addLineEntry - adds an entry to this MCLineSection's line entries
160 void addLineEntry(const MCLineEntry &LineEntry, const MCSection *Sec) {
161 MCLineDivisions[Sec].push_back(LineEntry);
164 typedef std::vector<MCLineEntry> MCLineEntryCollection;
165 typedef MCLineEntryCollection::iterator iterator;
166 typedef MCLineEntryCollection::const_iterator const_iterator;
167 typedef MapVector<const MCSection *, MCLineEntryCollection> MCLineDivisionMap;
170 // A collection of MCLineEntry for each section.
171 MCLineDivisionMap MCLineDivisions;
174 // Returns the collection of MCLineEntry for a given Compile Unit ID.
175 const MCLineDivisionMap &getMCLineEntries() const {
176 return MCLineDivisions;
180 struct MCDwarfLineTableHeader {
182 SmallVector<std::string, 3> MCDwarfDirs;
183 SmallVector<MCDwarfFile, 3> MCDwarfFiles;
184 StringMap<unsigned> SourceIdMap;
185 StringRef CompilationDir;
187 MCDwarfLineTableHeader() : Label(nullptr) {}
188 unsigned getFile(StringRef &Directory, StringRef &FileName,
189 unsigned FileNumber = 0);
190 std::pair<MCSymbol *, MCSymbol *> Emit(MCStreamer *MCOS) const;
191 std::pair<MCSymbol *, MCSymbol *>
192 Emit(MCStreamer *MCOS, ArrayRef<char> SpecialOpcodeLengths) const;
195 class MCDwarfDwoLineTable {
196 MCDwarfLineTableHeader Header;
198 unsigned getFile(StringRef Directory, StringRef FileName) {
199 return Header.getFile(Directory, FileName);
201 void Emit(MCStreamer &MCOS) const;
204 class MCDwarfLineTable {
205 MCDwarfLineTableHeader Header;
206 MCLineSection MCLineSections;
209 // This emits the Dwarf file and the line tables for all Compile Units.
210 static const MCSymbol *Emit(MCStreamer *MCOS);
212 // This emits the Dwarf file and the line tables for a given Compile Unit.
213 const MCSymbol *EmitCU(MCStreamer *MCOS) const;
215 unsigned getFile(StringRef &Directory, StringRef &FileName,
216 unsigned FileNumber = 0);
218 MCSymbol *getLabel() const {
222 void setLabel(MCSymbol *Label) {
223 Header.Label = Label;
226 void setCompilationDir(StringRef CompilationDir) {
227 Header.CompilationDir = CompilationDir;
230 const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
231 return Header.MCDwarfDirs;
234 SmallVectorImpl<std::string> &getMCDwarfDirs() {
235 return Header.MCDwarfDirs;
238 const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
239 return Header.MCDwarfFiles;
242 SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
243 return Header.MCDwarfFiles;
246 const MCLineSection &getMCLineSections() const {
247 return MCLineSections;
249 MCLineSection &getMCLineSections() {
250 return MCLineSections;
254 class MCDwarfLineAddr {
256 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
257 static void Encode(MCContext &Context, int64_t LineDelta, uint64_t AddrDelta,
260 /// Utility function to emit the encoding to a streamer.
261 static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta);
264 class MCGenDwarfInfo {
267 // When generating dwarf for assembly source files this emits the Dwarf
270 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
273 // When generating dwarf for assembly source files this is the info that is
274 // needed to be gathered for each symbol that will have a dwarf label.
275 class MCGenDwarfLabelEntry {
277 // Name of the symbol without a leading underbar, if any.
279 // The dwarf file number this symbol is in.
281 // The line number this symbol is at.
283 // The low_pc for the dwarf label is taken from this symbol.
287 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
289 : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
292 StringRef getName() const { return Name; }
293 unsigned getFileNumber() const { return FileNumber; }
294 unsigned getLineNumber() const { return LineNumber; }
295 MCSymbol *getLabel() const { return Label; }
297 // This is called when label is created when we are generating dwarf for
298 // assembly source files.
299 static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
303 class MCCFIInstruction {
330 std::vector<char> Values;
332 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
333 : Operation(Op), Label(L), Register(R), Offset(O),
334 Values(V.begin(), V.end()) {
335 assert(Op != OpRegister);
338 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
339 : Operation(Op), Label(L), Register(R1), Register2(R2) {
340 assert(Op == OpRegister);
344 /// \brief .cfi_def_cfa defines a rule for computing CFA as: take address from
345 /// Register and add Offset to it.
346 static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
348 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
351 /// \brief .cfi_def_cfa_register modifies a rule for computing CFA. From now
352 /// on Register will be used instead of the old one. Offset remains the same.
353 static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
354 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
357 /// \brief .cfi_def_cfa_offset modifies a rule for computing CFA. Register
358 /// remains the same, but offset is new. Note that it is the absolute offset
359 /// that will be added to a defined register to the compute CFA address.
360 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
361 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
364 /// \brief .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
365 /// Offset is a relative value that is added/subtracted from the previous
367 static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
368 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
371 /// \brief .cfi_offset Previous value of Register is saved at offset Offset
373 static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
375 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
378 /// \brief .cfi_rel_offset Previous value of Register is saved at offset
379 /// Offset from the current CFA register. This is transformed to .cfi_offset
380 /// using the known displacement of the CFA register from the CFA.
381 static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
383 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
386 /// \brief .cfi_register Previous value of Register1 is saved in
387 /// register Register2.
388 static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
389 unsigned Register2) {
390 return MCCFIInstruction(OpRegister, L, Register1, Register2);
393 /// \brief .cfi_window_save SPARC register window is saved.
394 static MCCFIInstruction createWindowSave(MCSymbol *L) {
395 return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
398 /// \brief .cfi_restore says that the rule for Register is now the same as it
399 /// was at the beginning of the function, after all initial instructions added
400 /// by .cfi_startproc were executed.
401 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
402 return MCCFIInstruction(OpRestore, L, Register, 0, "");
405 /// \brief .cfi_undefined From now on the previous value of Register can't be
406 /// restored anymore.
407 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
408 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
411 /// \brief .cfi_same_value Current value of Register is the same as in the
412 /// previous frame. I.e., no restoration is needed.
413 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
414 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
417 /// \brief .cfi_remember_state Save all current rules for all registers.
418 static MCCFIInstruction createRememberState(MCSymbol *L) {
419 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
422 /// \brief .cfi_restore_state Restore the previously saved state.
423 static MCCFIInstruction createRestoreState(MCSymbol *L) {
424 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
427 /// \brief .cfi_escape Allows the user to add arbitrary bytes to the unwind
429 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
430 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
433 OpType getOperation() const { return Operation; }
434 MCSymbol *getLabel() const { return Label; }
436 unsigned getRegister() const {
437 assert(Operation == OpDefCfa || Operation == OpOffset ||
438 Operation == OpRestore || Operation == OpUndefined ||
439 Operation == OpSameValue || Operation == OpDefCfaRegister ||
440 Operation == OpRelOffset || Operation == OpRegister);
444 unsigned getRegister2() const {
445 assert(Operation == OpRegister);
449 int getOffset() const {
450 assert(Operation == OpDefCfa || Operation == OpOffset ||
451 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
452 Operation == OpAdjustCfaOffset);
456 const StringRef getValues() const {
457 assert(Operation == OpEscape);
458 return StringRef(&Values[0], Values.size());
462 struct MCDwarfFrameInfo {
464 : Begin(0), End(0), Personality(0), Lsda(0), Function(0), Instructions(),
465 PersonalityEncoding(), LsdaEncoding(0), CompactUnwindEncoding(0),
466 IsSignalFrame(false), IsSimple(false) {}
469 const MCSymbol *Personality;
470 const MCSymbol *Lsda;
471 const MCSymbol *Function;
472 std::vector<MCCFIInstruction> Instructions;
473 unsigned PersonalityEncoding;
474 unsigned LsdaEncoding;
475 uint32_t CompactUnwindEncoding;
480 class MCDwarfFrameEmitter {
483 // This emits the frame info section.
485 static void Emit(MCStreamer &streamer, MCAsmBackend *MAB,
486 bool usingCFI, bool isEH);
487 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
488 static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
491 } // end namespace llvm