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/MapVector.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/raw_ostream.h"
33 class MCObjectStreamer;
40 /// \brief Instances of this class represent the name of the dwarf
41 /// .file directive and its associated dwarf file number in the MC file,
42 /// and MCDwarfFile's are created and uniqued by the MCContext class where
43 /// the file number for each is its index into the vector of DwarfFiles (note
44 /// index 0 is not used and not a valid dwarf file number).
46 // \brief The base name of the file without its directory path.
47 // The StringRef references memory allocated in the MCContext.
50 // \brief The index into the list of directory names for this file name.
54 /// \brief Instances of this class represent the information from a
55 /// dwarf .loc directive.
60 // Flags (see #define's below)
63 uint32_t Discriminator;
65 // Flag that indicates the initial value of the is_stmt_start flag.
66 #define DWARF2_LINE_DEFAULT_IS_STMT 1
68 #define DWARF2_FLAG_IS_STMT (1 << 0)
69 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
70 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
71 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
73 private: // MCContext manages these
74 friend class MCContext;
75 friend class MCLineEntry;
76 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
77 unsigned isa, unsigned discriminator)
78 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
79 Discriminator(discriminator) {}
81 // Allow the default copy constructor and assignment operator to be used
82 // for an MCDwarfLoc object.
85 /// \brief Get the FileNum of this MCDwarfLoc.
86 unsigned getFileNum() const { return FileNum; }
88 /// \brief Get the Line of this MCDwarfLoc.
89 unsigned getLine() const { return Line; }
91 /// \brief Get the Column of this MCDwarfLoc.
92 unsigned getColumn() const { return Column; }
94 /// \brief Get the Flags of this MCDwarfLoc.
95 unsigned getFlags() const { return Flags; }
97 /// \brief Get the Isa of this MCDwarfLoc.
98 unsigned getIsa() const { return Isa; }
100 /// \brief Get the Discriminator of this MCDwarfLoc.
101 unsigned getDiscriminator() const { return Discriminator; }
103 /// \brief Set the FileNum of this MCDwarfLoc.
104 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
106 /// \brief Set the Line of this MCDwarfLoc.
107 void setLine(unsigned line) { Line = line; }
109 /// \brief Set the Column of this MCDwarfLoc.
110 void setColumn(unsigned column) {
111 assert(column <= UINT16_MAX);
115 /// \brief Set the Flags of this MCDwarfLoc.
116 void setFlags(unsigned flags) {
117 assert(flags <= UINT8_MAX);
121 /// \brief Set the Isa of this MCDwarfLoc.
122 void setIsa(unsigned isa) {
123 assert(isa <= UINT8_MAX);
127 /// \brief Set the Discriminator of this MCDwarfLoc.
128 void setDiscriminator(unsigned discriminator) {
129 Discriminator = discriminator;
133 /// \brief Instances of this class represent the line information for
134 /// the dwarf line table entries. Which is created after a machine
135 /// instruction is assembled and uses an address from a temporary label
136 /// created at the current address in the current section and the info from
137 /// the last .loc directive seen as stored in the context.
138 class MCLineEntry : public MCDwarfLoc {
142 // Allow the default copy constructor and assignment operator to be used
143 // for an MCLineEntry object.
146 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
147 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
148 : MCDwarfLoc(loc), Label(label) {}
150 MCSymbol *getLabel() const { return Label; }
152 // This is called when an instruction is assembled into the specified
153 // section and if there is information from the last .loc directive that
154 // has yet to have a line entry made for it is made.
155 static void Make(MCObjectStreamer *MCOS, MCSection *Section);
158 /// \brief Instances of this class represent the line information for a compile
159 /// unit where machine instructions have been assembled after seeing .loc
160 /// directives. This is the information used to build the dwarf line
161 /// table for a section.
162 class MCLineSection {
164 // \brief Add an entry to this MCLineSection's line entries.
165 void addLineEntry(const MCLineEntry &LineEntry, MCSection *Sec) {
166 MCLineDivisions[Sec].push_back(LineEntry);
169 typedef std::vector<MCLineEntry> MCLineEntryCollection;
170 typedef MCLineEntryCollection::iterator iterator;
171 typedef MCLineEntryCollection::const_iterator const_iterator;
172 typedef MapVector<MCSection *, MCLineEntryCollection> MCLineDivisionMap;
175 // A collection of MCLineEntry for each section.
176 MCLineDivisionMap MCLineDivisions;
179 // Returns the collection of MCLineEntry for a given Compile Unit ID.
180 const MCLineDivisionMap &getMCLineEntries() const {
181 return MCLineDivisions;
185 struct MCDwarfLineTableParams {
186 /// First special line opcode - leave room for the standard opcodes.
187 /// Note: If you want to change this, you'll have to update the
188 /// "StandardOpcodeLengths" table that is emitted in
190 uint8_t DWARF2LineOpcodeBase = 13;
191 /// Minimum line offset in a special line info. opcode. The value
192 /// -5 was chosen to give a reasonable range of values.
193 int8_t DWARF2LineBase = -5;
194 /// Range of line offsets in a special line info. opcode.
195 uint8_t DWARF2LineRange = 14;
198 struct MCDwarfLineTableHeader {
200 SmallVector<std::string, 3> MCDwarfDirs;
201 SmallVector<MCDwarfFile, 3> MCDwarfFiles;
202 StringMap<unsigned> SourceIdMap;
203 StringRef CompilationDir;
205 MCDwarfLineTableHeader() : Label(nullptr) {}
206 unsigned getFile(StringRef &Directory, StringRef &FileName,
207 unsigned FileNumber = 0);
208 std::pair<MCSymbol *, MCSymbol *> Emit(MCStreamer *MCOS,
209 MCDwarfLineTableParams Params) const;
210 std::pair<MCSymbol *, MCSymbol *>
211 Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
212 ArrayRef<char> SpecialOpcodeLengths) const;
215 class MCDwarfDwoLineTable {
216 MCDwarfLineTableHeader Header;
218 void setCompilationDir(StringRef CompilationDir) {
219 Header.CompilationDir = CompilationDir;
221 unsigned getFile(StringRef Directory, StringRef FileName) {
222 return Header.getFile(Directory, FileName);
224 void Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params) const;
227 class MCDwarfLineTable {
228 MCDwarfLineTableHeader Header;
229 MCLineSection MCLineSections;
232 // This emits the Dwarf file and the line tables for all Compile Units.
233 static void Emit(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params);
235 // This emits the Dwarf file and the line tables for a given Compile Unit.
236 void EmitCU(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params) const;
238 unsigned getFile(StringRef &Directory, StringRef &FileName,
239 unsigned FileNumber = 0);
241 MCSymbol *getLabel() const {
245 void setLabel(MCSymbol *Label) {
246 Header.Label = Label;
249 void setCompilationDir(StringRef CompilationDir) {
250 Header.CompilationDir = CompilationDir;
253 const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
254 return Header.MCDwarfDirs;
257 SmallVectorImpl<std::string> &getMCDwarfDirs() {
258 return Header.MCDwarfDirs;
261 const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
262 return Header.MCDwarfFiles;
265 SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
266 return Header.MCDwarfFiles;
269 const MCLineSection &getMCLineSections() const {
270 return MCLineSections;
272 MCLineSection &getMCLineSections() {
273 return MCLineSections;
277 class MCDwarfLineAddr {
279 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
280 static void Encode(MCContext &Context, MCDwarfLineTableParams Params,
281 int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
283 /// Utility function to emit the encoding to a streamer.
284 static void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
285 int64_t LineDelta, uint64_t AddrDelta);
288 class MCGenDwarfInfo {
291 // When generating dwarf for assembly source files this emits the Dwarf
294 static void Emit(MCStreamer *MCOS);
297 // When generating dwarf for assembly source files this is the info that is
298 // needed to be gathered for each symbol that will have a dwarf label.
299 class MCGenDwarfLabelEntry {
301 // Name of the symbol without a leading underbar, if any.
303 // The dwarf file number this symbol is in.
305 // The line number this symbol is at.
307 // The low_pc for the dwarf label is taken from this symbol.
311 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
313 : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
316 StringRef getName() const { return Name; }
317 unsigned getFileNumber() const { return FileNumber; }
318 unsigned getLineNumber() const { return LineNumber; }
319 MCSymbol *getLabel() const { return Label; }
321 // This is called when label is created when we are generating dwarf for
322 // assembly source files.
323 static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
327 class MCCFIInstruction {
354 std::vector<char> Values;
356 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
357 : Operation(Op), Label(L), Register(R), Offset(O),
358 Values(V.begin(), V.end()) {
359 assert(Op != OpRegister);
362 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
363 : Operation(Op), Label(L), Register(R1), Register2(R2) {
364 assert(Op == OpRegister);
368 /// \brief .cfi_def_cfa defines a rule for computing CFA as: take address from
369 /// Register and add Offset to it.
370 static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
372 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
375 /// \brief .cfi_def_cfa_register modifies a rule for computing CFA. From now
376 /// on Register will be used instead of the old one. Offset remains the same.
377 static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
378 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
381 /// \brief .cfi_def_cfa_offset modifies a rule for computing CFA. Register
382 /// remains the same, but offset is new. Note that it is the absolute offset
383 /// that will be added to a defined register to the compute CFA address.
384 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
385 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
388 /// \brief .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
389 /// Offset is a relative value that is added/subtracted from the previous
391 static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
392 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
395 /// \brief .cfi_offset Previous value of Register is saved at offset Offset
397 static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
399 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
402 /// \brief .cfi_rel_offset Previous value of Register is saved at offset
403 /// Offset from the current CFA register. This is transformed to .cfi_offset
404 /// using the known displacement of the CFA register from the CFA.
405 static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
407 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
410 /// \brief .cfi_register Previous value of Register1 is saved in
411 /// register Register2.
412 static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
413 unsigned Register2) {
414 return MCCFIInstruction(OpRegister, L, Register1, Register2);
417 /// \brief .cfi_window_save SPARC register window is saved.
418 static MCCFIInstruction createWindowSave(MCSymbol *L) {
419 return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
422 /// \brief .cfi_restore says that the rule for Register is now the same as it
423 /// was at the beginning of the function, after all initial instructions added
424 /// by .cfi_startproc were executed.
425 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
426 return MCCFIInstruction(OpRestore, L, Register, 0, "");
429 /// \brief .cfi_undefined From now on the previous value of Register can't be
430 /// restored anymore.
431 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
432 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
435 /// \brief .cfi_same_value Current value of Register is the same as in the
436 /// previous frame. I.e., no restoration is needed.
437 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
438 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
441 /// \brief .cfi_remember_state Save all current rules for all registers.
442 static MCCFIInstruction createRememberState(MCSymbol *L) {
443 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
446 /// \brief .cfi_restore_state Restore the previously saved state.
447 static MCCFIInstruction createRestoreState(MCSymbol *L) {
448 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
451 /// \brief .cfi_escape Allows the user to add arbitrary bytes to the unwind
453 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
454 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
457 OpType getOperation() const { return Operation; }
458 MCSymbol *getLabel() const { return Label; }
460 unsigned getRegister() const {
461 assert(Operation == OpDefCfa || Operation == OpOffset ||
462 Operation == OpRestore || Operation == OpUndefined ||
463 Operation == OpSameValue || Operation == OpDefCfaRegister ||
464 Operation == OpRelOffset || Operation == OpRegister);
468 unsigned getRegister2() const {
469 assert(Operation == OpRegister);
473 int getOffset() const {
474 assert(Operation == OpDefCfa || Operation == OpOffset ||
475 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
476 Operation == OpAdjustCfaOffset);
480 StringRef getValues() const {
481 assert(Operation == OpEscape);
482 return StringRef(&Values[0], Values.size());
486 struct MCDwarfFrameInfo {
488 : Begin(nullptr), End(nullptr), Personality(nullptr), Lsda(nullptr),
489 Instructions(), CurrentCfaRegister(0), PersonalityEncoding(),
490 LsdaEncoding(0), CompactUnwindEncoding(0), IsSignalFrame(false),
494 const MCSymbol *Personality;
495 const MCSymbol *Lsda;
496 std::vector<MCCFIInstruction> Instructions;
497 unsigned CurrentCfaRegister;
498 unsigned PersonalityEncoding;
499 unsigned LsdaEncoding;
500 uint32_t CompactUnwindEncoding;
505 class MCDwarfFrameEmitter {
508 // This emits the frame info section.
510 static void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH);
511 static void EmitAdvanceLoc(MCObjectStreamer &Streamer, uint64_t AddrDelta);
512 static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
515 } // end namespace llvm