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/MapVector.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Dwarf.h"
22 #include "llvm/Support/raw_ostream.h"
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.
51 /// MCDwarfLoc - Instances of this class represent the information from a
52 /// dwarf .loc directive.
54 // FileNum - the file number.
56 // Line - the line number.
58 // Column - the column position.
60 // Flags (see #define's below)
65 unsigned Discriminator;
67 // Flag that indicates the initial value of the is_stmt_start flag.
68 #define DWARF2_LINE_DEFAULT_IS_STMT 1
70 #define DWARF2_FLAG_IS_STMT (1 << 0)
71 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
72 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
73 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
75 private: // MCContext manages these
76 friend class MCContext;
77 friend class MCLineEntry;
78 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
79 unsigned isa, unsigned discriminator)
80 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
81 Discriminator(discriminator) {}
83 // Allow the default copy constructor and assignment operator to be used
84 // for an MCDwarfLoc object.
87 /// getFileNum - Get the FileNum of this MCDwarfLoc.
88 unsigned getFileNum() const { return FileNum; }
90 /// getLine - Get the Line of this MCDwarfLoc.
91 unsigned getLine() const { return Line; }
93 /// getColumn - Get the Column of this MCDwarfLoc.
94 unsigned getColumn() const { return Column; }
96 /// getFlags - Get the Flags of this MCDwarfLoc.
97 unsigned getFlags() const { return Flags; }
99 /// getIsa - Get the Isa of this MCDwarfLoc.
100 unsigned getIsa() const { return Isa; }
102 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
103 unsigned getDiscriminator() const { return Discriminator; }
105 /// setFileNum - Set the FileNum of this MCDwarfLoc.
106 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
108 /// setLine - Set the Line of this MCDwarfLoc.
109 void setLine(unsigned line) { Line = line; }
111 /// setColumn - Set the Column of this MCDwarfLoc.
112 void setColumn(unsigned column) { Column = column; }
114 /// setFlags - Set the Flags of this MCDwarfLoc.
115 void setFlags(unsigned flags) { Flags = flags; }
117 /// setIsa - Set the Isa of this MCDwarfLoc.
118 void setIsa(unsigned isa) { Isa = isa; }
120 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
121 void setDiscriminator(unsigned discriminator) {
122 Discriminator = discriminator;
126 /// MCLineEntry - Instances of this class represent the line information for
127 /// the dwarf line table entries. Which is created after a machine
128 /// instruction is assembled and uses an address from a temporary label
129 /// created at the current address in the current section and the info from
130 /// the last .loc directive seen as stored in the context.
131 class MCLineEntry : public MCDwarfLoc {
135 // Allow the default copy constructor and assignment operator to be used
136 // for an MCLineEntry object.
139 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
140 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
141 : MCDwarfLoc(loc), Label(label) {}
143 MCSymbol *getLabel() const { return Label; }
145 // This is called when an instruction is assembled into the specified
146 // section and if there is information from the last .loc directive that
147 // has yet to have a line entry made for it is made.
148 static void Make(MCStreamer *MCOS, const MCSection *Section);
151 /// MCLineSection - Instances of this class represent the line information
152 /// for a compile unit where machine instructions have been assembled after seeing
153 /// .loc directives. This is the information used to build the dwarf line
154 /// table for a section.
155 class MCLineSection {
157 // addLineEntry - adds an entry to this MCLineSection's line entries
158 void addLineEntry(const MCLineEntry &LineEntry, const MCSection *Sec) {
159 MCLineDivisions[Sec].push_back(LineEntry);
162 typedef std::vector<MCLineEntry> MCLineEntryCollection;
163 typedef MCLineEntryCollection::iterator iterator;
164 typedef MCLineEntryCollection::const_iterator const_iterator;
165 typedef MapVector<const MCSection *, MCLineEntryCollection> MCLineDivisionMap;
168 // A collection of MCLineEntry for each section.
169 MCLineDivisionMap MCLineDivisions;
172 // Returns the collection of MCLineEntry for a given Compile Unit ID.
173 const MCLineDivisionMap &getMCLineEntries() const {
174 return MCLineDivisions;
178 struct MCDwarfLineTableHeader {
180 SmallVector<std::string, 3> MCDwarfDirs;
181 SmallVector<MCDwarfFile, 3> MCDwarfFiles;
182 MCDwarfLineTableHeader() : Label(nullptr) {}
183 unsigned getFile(StringRef Directory, StringRef FileName, unsigned FileNumber);
184 std::pair<MCSymbol *, MCSymbol *> Emit(MCStreamer *MCOS) const;
187 class MCDwarfLineTable {
188 MCDwarfLineTableHeader Header;
189 MCLineSection MCLineSections;
192 // This emits the Dwarf file and the line tables for all Compile Units.
193 static const MCSymbol *Emit(MCStreamer *MCOS);
195 // This emits the Dwarf file and the line tables for a given Compile Unit.
196 const MCSymbol *EmitCU(MCStreamer *MCOS) const;
198 unsigned getFile(StringRef Directory, StringRef FileName, unsigned FileNumber);
200 MCSymbol *getLabel() const {
204 void setLabel(MCSymbol *Label) {
205 Header.Label = Label;
208 const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
209 return Header.MCDwarfDirs;
212 SmallVectorImpl<std::string> &getMCDwarfDirs() {
213 return Header.MCDwarfDirs;
216 const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
217 return Header.MCDwarfFiles;
220 SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
221 return Header.MCDwarfFiles;
224 const MCLineSection &getMCLineSections() const {
225 return MCLineSections;
227 MCLineSection &getMCLineSections() {
228 return MCLineSections;
232 class MCDwarfLineAddr {
234 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
235 static void Encode(MCContext &Context, int64_t LineDelta, uint64_t AddrDelta,
238 /// Utility function to emit the encoding to a streamer.
239 static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta);
242 class MCGenDwarfInfo {
245 // When generating dwarf for assembly source files this emits the Dwarf
248 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
251 // When generating dwarf for assembly source files this is the info that is
252 // needed to be gathered for each symbol that will have a dwarf label.
253 class MCGenDwarfLabelEntry {
255 // Name of the symbol without a leading underbar, if any.
257 // The dwarf file number this symbol is in.
259 // The line number this symbol is at.
261 // The low_pc for the dwarf label is taken from this symbol.
265 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
267 : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
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 {
308 std::vector<char> Values;
310 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
311 : Operation(Op), Label(L), Register(R), Offset(O),
312 Values(V.begin(), V.end()) {
313 assert(Op != OpRegister);
316 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
317 : Operation(Op), Label(L), Register(R1), Register2(R2) {
318 assert(Op == OpRegister);
322 /// \brief .cfi_def_cfa defines a rule for computing CFA as: take address from
323 /// Register and add Offset to it.
324 static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
326 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
329 /// \brief .cfi_def_cfa_register modifies a rule for computing CFA. From now
330 /// on Register will be used instead of the old one. Offset remains the same.
331 static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
332 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
335 /// \brief .cfi_def_cfa_offset modifies a rule for computing CFA. Register
336 /// remains the same, but offset is new. Note that it is the absolute offset
337 /// that will be added to a defined register to the compute CFA address.
338 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
339 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
342 /// \brief .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
343 /// Offset is a relative value that is added/subtracted from the previous
345 static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
346 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
349 /// \brief .cfi_offset Previous value of Register is saved at offset Offset
351 static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
353 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
356 /// \brief .cfi_rel_offset Previous value of Register is saved at offset
357 /// Offset from the current CFA register. This is transformed to .cfi_offset
358 /// using the known displacement of the CFA register from the CFA.
359 static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
361 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
364 /// \brief .cfi_register Previous value of Register1 is saved in
365 /// register Register2.
366 static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
367 unsigned Register2) {
368 return MCCFIInstruction(OpRegister, L, Register1, Register2);
371 /// \brief .cfi_window_save SPARC register window is saved.
372 static MCCFIInstruction createWindowSave(MCSymbol *L) {
373 return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
376 /// \brief .cfi_restore says that the rule for Register is now the same as it
377 /// was at the beginning of the function, after all initial instructions added
378 /// by .cfi_startproc were executed.
379 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
380 return MCCFIInstruction(OpRestore, L, Register, 0, "");
383 /// \brief .cfi_undefined From now on the previous value of Register can't be
384 /// restored anymore.
385 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
386 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
389 /// \brief .cfi_same_value Current value of Register is the same as in the
390 /// previous frame. I.e., no restoration is needed.
391 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
392 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
395 /// \brief .cfi_remember_state Save all current rules for all registers.
396 static MCCFIInstruction createRememberState(MCSymbol *L) {
397 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
400 /// \brief .cfi_restore_state Restore the previously saved state.
401 static MCCFIInstruction createRestoreState(MCSymbol *L) {
402 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
405 /// \brief .cfi_escape Allows the user to add arbitrary bytes to the unwind
407 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
408 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
411 OpType getOperation() const { return Operation; }
412 MCSymbol *getLabel() const { return Label; }
414 unsigned getRegister() const {
415 assert(Operation == OpDefCfa || Operation == OpOffset ||
416 Operation == OpRestore || Operation == OpUndefined ||
417 Operation == OpSameValue || Operation == OpDefCfaRegister ||
418 Operation == OpRelOffset || Operation == OpRegister);
422 unsigned getRegister2() const {
423 assert(Operation == OpRegister);
427 int getOffset() const {
428 assert(Operation == OpDefCfa || Operation == OpOffset ||
429 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
430 Operation == OpAdjustCfaOffset);
434 const StringRef getValues() const {
435 assert(Operation == OpEscape);
436 return StringRef(&Values[0], Values.size());
440 struct MCDwarfFrameInfo {
442 : Begin(0), End(0), Personality(0), Lsda(0), Function(0), Instructions(),
443 PersonalityEncoding(), LsdaEncoding(0), CompactUnwindEncoding(0),
444 IsSignalFrame(false), IsSimple(false) {}
447 const MCSymbol *Personality;
448 const MCSymbol *Lsda;
449 const MCSymbol *Function;
450 std::vector<MCCFIInstruction> Instructions;
451 unsigned PersonalityEncoding;
452 unsigned LsdaEncoding;
453 uint32_t CompactUnwindEncoding;
458 class MCDwarfFrameEmitter {
461 // This emits the frame info section.
463 static void Emit(MCStreamer &streamer, MCAsmBackend *MAB,
464 bool usingCFI, bool isEH);
465 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
466 static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
469 } // end namespace llvm