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/ADT/StringMap.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Dwarf.h"
23 #include "llvm/Support/raw_ostream.h"
38 /// MCDwarfFile - Instances of this class represent the name of the dwarf
39 /// .file directive and its associated dwarf file number in the MC file,
40 /// and MCDwarfFile's are created and unique'd by the MCContext class where
41 /// the file number for each is its index into the vector of DwarfFiles (note
42 /// index 0 is not used and not a valid dwarf file number).
44 // Name - the base name of the file without its directory path.
45 // The StringRef references memory allocated in the MCContext.
48 // DirIndex - the index into the list of directory names for this file name.
52 /// MCDwarfLoc - Instances of this class represent the information from a
53 /// dwarf .loc directive.
55 // FileNum - the file number.
57 // Line - the line number.
59 // Column - the column position.
61 // Flags (see #define's below)
66 unsigned Discriminator;
68 // Flag that indicates the initial value of the is_stmt_start flag.
69 #define DWARF2_LINE_DEFAULT_IS_STMT 1
71 #define DWARF2_FLAG_IS_STMT (1 << 0)
72 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
73 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
74 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
76 private: // MCContext manages these
77 friend class MCContext;
78 friend class MCLineEntry;
79 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
80 unsigned isa, unsigned discriminator)
81 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
82 Discriminator(discriminator) {}
84 // Allow the default copy constructor and assignment operator to be used
85 // for an MCDwarfLoc object.
88 /// getFileNum - Get the FileNum of this MCDwarfLoc.
89 unsigned getFileNum() const { return FileNum; }
91 /// getLine - Get the Line of this MCDwarfLoc.
92 unsigned getLine() const { return Line; }
94 /// getColumn - Get the Column of this MCDwarfLoc.
95 unsigned getColumn() const { return Column; }
97 /// getFlags - Get the Flags of this MCDwarfLoc.
98 unsigned getFlags() const { return Flags; }
100 /// getIsa - Get the Isa of this MCDwarfLoc.
101 unsigned getIsa() const { return Isa; }
103 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
104 unsigned getDiscriminator() const { return Discriminator; }
106 /// setFileNum - Set the FileNum of this MCDwarfLoc.
107 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
109 /// setLine - Set the Line of this MCDwarfLoc.
110 void setLine(unsigned line) { Line = line; }
112 /// setColumn - Set the Column of this MCDwarfLoc.
113 void setColumn(unsigned column) { Column = column; }
115 /// setFlags - Set the Flags of this MCDwarfLoc.
116 void setFlags(unsigned flags) { Flags = flags; }
118 /// setIsa - Set the Isa of this MCDwarfLoc.
119 void setIsa(unsigned isa) { Isa = isa; }
121 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
122 void setDiscriminator(unsigned discriminator) {
123 Discriminator = discriminator;
127 /// MCLineEntry - Instances of this class represent the line information for
128 /// the dwarf line table entries. Which is created after a machine
129 /// instruction is assembled and uses an address from a temporary label
130 /// created at the current address in the current section and the info from
131 /// the last .loc directive seen as stored in the context.
132 class MCLineEntry : public MCDwarfLoc {
136 // Allow the default copy constructor and assignment operator to be used
137 // for an MCLineEntry object.
140 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
141 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
142 : MCDwarfLoc(loc), Label(label) {}
144 MCSymbol *getLabel() const { return Label; }
146 // This is called when an instruction is assembled into the specified
147 // section and if there is information from the last .loc directive that
148 // has yet to have a line entry made for it is made.
149 static void Make(MCStreamer *MCOS, const MCSection *Section);
152 /// MCLineSection - Instances of this class represent the line information
153 /// for a compile unit where machine instructions have been assembled after seeing
154 /// .loc directives. This is the information used to build the dwarf line
155 /// table for a section.
156 class MCLineSection {
158 // addLineEntry - adds an entry to this MCLineSection's line entries
159 void addLineEntry(const MCLineEntry &LineEntry, const MCSection *Sec) {
160 MCLineDivisions[Sec].push_back(LineEntry);
163 typedef std::vector<MCLineEntry> MCLineEntryCollection;
164 typedef MCLineEntryCollection::iterator iterator;
165 typedef MCLineEntryCollection::const_iterator const_iterator;
166 typedef MapVector<const MCSection *, MCLineEntryCollection> MCLineDivisionMap;
169 // A collection of MCLineEntry for each section.
170 MCLineDivisionMap MCLineDivisions;
173 // Returns the collection of MCLineEntry for a given Compile Unit ID.
174 const MCLineDivisionMap &getMCLineEntries() const {
175 return MCLineDivisions;
179 struct MCDwarfLineTableHeader {
181 SmallVector<std::string, 3> MCDwarfDirs;
182 SmallVector<MCDwarfFile, 3> MCDwarfFiles;
183 StringMap<unsigned> SourceIdMap;
184 MCDwarfLineTableHeader() : Label(nullptr) {}
185 unsigned getFile(StringRef &Directory, StringRef &FileName,
186 unsigned FileNumber = 0);
187 std::pair<MCSymbol *, MCSymbol *> Emit(MCStreamer *MCOS) const;
190 class MCDwarfLineTable {
191 MCDwarfLineTableHeader Header;
192 MCLineSection MCLineSections;
195 // This emits the Dwarf file and the line tables for all Compile Units.
196 static const MCSymbol *Emit(MCStreamer *MCOS);
198 // This emits the Dwarf file and the line tables for a given Compile Unit.
199 const MCSymbol *EmitCU(MCStreamer *MCOS) const;
201 unsigned getFile(StringRef &Directory, StringRef &FileName,
202 unsigned FileNumber = 0);
204 MCSymbol *getLabel() const {
208 void setLabel(MCSymbol *Label) {
209 Header.Label = Label;
212 const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
213 return Header.MCDwarfDirs;
216 SmallVectorImpl<std::string> &getMCDwarfDirs() {
217 return Header.MCDwarfDirs;
220 const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
221 return Header.MCDwarfFiles;
224 SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
225 return Header.MCDwarfFiles;
228 const MCLineSection &getMCLineSections() const {
229 return MCLineSections;
231 MCLineSection &getMCLineSections() {
232 return MCLineSections;
236 class MCDwarfLineAddr {
238 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
239 static void Encode(MCContext &Context, int64_t LineDelta, uint64_t AddrDelta,
242 /// Utility function to emit the encoding to a streamer.
243 static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta);
246 class MCGenDwarfInfo {
249 // When generating dwarf for assembly source files this emits the Dwarf
252 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
255 // When generating dwarf for assembly source files this is the info that is
256 // needed to be gathered for each symbol that will have a dwarf label.
257 class MCGenDwarfLabelEntry {
259 // Name of the symbol without a leading underbar, if any.
261 // The dwarf file number this symbol is in.
263 // The line number this symbol is at.
265 // The low_pc for the dwarf label is taken from this symbol.
269 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
271 : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
274 StringRef getName() const { return Name; }
275 unsigned getFileNumber() const { return FileNumber; }
276 unsigned getLineNumber() const { return LineNumber; }
277 MCSymbol *getLabel() const { return Label; }
279 // This is called when label is created when we are generating dwarf for
280 // assembly source files.
281 static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
285 class MCCFIInstruction {
312 std::vector<char> Values;
314 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
315 : Operation(Op), Label(L), Register(R), Offset(O),
316 Values(V.begin(), V.end()) {
317 assert(Op != OpRegister);
320 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
321 : Operation(Op), Label(L), Register(R1), Register2(R2) {
322 assert(Op == OpRegister);
326 /// \brief .cfi_def_cfa defines a rule for computing CFA as: take address from
327 /// Register and add Offset to it.
328 static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
330 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
333 /// \brief .cfi_def_cfa_register modifies a rule for computing CFA. From now
334 /// on Register will be used instead of the old one. Offset remains the same.
335 static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
336 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
339 /// \brief .cfi_def_cfa_offset modifies a rule for computing CFA. Register
340 /// remains the same, but offset is new. Note that it is the absolute offset
341 /// that will be added to a defined register to the compute CFA address.
342 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
343 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
346 /// \brief .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
347 /// Offset is a relative value that is added/subtracted from the previous
349 static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
350 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
353 /// \brief .cfi_offset Previous value of Register is saved at offset Offset
355 static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
357 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
360 /// \brief .cfi_rel_offset Previous value of Register is saved at offset
361 /// Offset from the current CFA register. This is transformed to .cfi_offset
362 /// using the known displacement of the CFA register from the CFA.
363 static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
365 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
368 /// \brief .cfi_register Previous value of Register1 is saved in
369 /// register Register2.
370 static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
371 unsigned Register2) {
372 return MCCFIInstruction(OpRegister, L, Register1, Register2);
375 /// \brief .cfi_window_save SPARC register window is saved.
376 static MCCFIInstruction createWindowSave(MCSymbol *L) {
377 return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
380 /// \brief .cfi_restore says that the rule for Register is now the same as it
381 /// was at the beginning of the function, after all initial instructions added
382 /// by .cfi_startproc were executed.
383 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
384 return MCCFIInstruction(OpRestore, L, Register, 0, "");
387 /// \brief .cfi_undefined From now on the previous value of Register can't be
388 /// restored anymore.
389 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
390 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
393 /// \brief .cfi_same_value Current value of Register is the same as in the
394 /// previous frame. I.e., no restoration is needed.
395 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
396 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
399 /// \brief .cfi_remember_state Save all current rules for all registers.
400 static MCCFIInstruction createRememberState(MCSymbol *L) {
401 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
404 /// \brief .cfi_restore_state Restore the previously saved state.
405 static MCCFIInstruction createRestoreState(MCSymbol *L) {
406 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
409 /// \brief .cfi_escape Allows the user to add arbitrary bytes to the unwind
411 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
412 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
415 OpType getOperation() const { return Operation; }
416 MCSymbol *getLabel() const { return Label; }
418 unsigned getRegister() const {
419 assert(Operation == OpDefCfa || Operation == OpOffset ||
420 Operation == OpRestore || Operation == OpUndefined ||
421 Operation == OpSameValue || Operation == OpDefCfaRegister ||
422 Operation == OpRelOffset || Operation == OpRegister);
426 unsigned getRegister2() const {
427 assert(Operation == OpRegister);
431 int getOffset() const {
432 assert(Operation == OpDefCfa || Operation == OpOffset ||
433 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
434 Operation == OpAdjustCfaOffset);
438 const StringRef getValues() const {
439 assert(Operation == OpEscape);
440 return StringRef(&Values[0], Values.size());
444 struct MCDwarfFrameInfo {
446 : Begin(0), End(0), Personality(0), Lsda(0), Function(0), Instructions(),
447 PersonalityEncoding(), LsdaEncoding(0), CompactUnwindEncoding(0),
448 IsSignalFrame(false), IsSimple(false) {}
451 const MCSymbol *Personality;
452 const MCSymbol *Lsda;
453 const MCSymbol *Function;
454 std::vector<MCCFIInstruction> Instructions;
455 unsigned PersonalityEncoding;
456 unsigned LsdaEncoding;
457 uint32_t CompactUnwindEncoding;
462 class MCDwarfFrameEmitter {
465 // This emits the frame info section.
467 static void Emit(MCStreamer &streamer, MCAsmBackend *MAB,
468 bool usingCFI, bool isEH);
469 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
470 static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
473 } // end namespace llvm