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"
35 /// MCDwarfFile - Instances of this class represent the name of the dwarf
36 /// .file directive and its associated dwarf file number in the MC file,
37 /// and MCDwarfFile's are created and unique'd by the MCContext class where
38 /// the file number for each is its index into the vector of DwarfFiles (note
39 /// index 0 is not used and not a valid dwarf file number).
41 // Name - the base name of the file without its directory path.
42 // The StringRef references memory allocated in the MCContext.
45 // DirIndex - the index into the list of directory names for this file name.
48 private: // MCContext creates and uniques these.
49 friend class MCContext;
50 MCDwarfFile(StringRef name, unsigned dirIndex)
51 : Name(name), DirIndex(dirIndex) {}
53 MCDwarfFile(const MCDwarfFile &) LLVM_DELETED_FUNCTION;
54 void operator=(const MCDwarfFile &) LLVM_DELETED_FUNCTION;
57 /// getName - Get the base name of this MCDwarfFile.
58 StringRef getName() const { return Name; }
60 /// getDirIndex - Get the dirIndex of this MCDwarfFile.
61 unsigned getDirIndex() const { return DirIndex; }
63 /// print - Print the value to the stream \p OS.
64 void print(raw_ostream &OS) const;
66 /// dump - Print the value to stderr.
70 inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile) {
75 /// MCDwarfLoc - Instances of this class represent the information from a
76 /// dwarf .loc directive.
78 // FileNum - the file number.
80 // Line - the line number.
82 // Column - the column position.
84 // Flags (see #define's below)
89 unsigned Discriminator;
91 // Flag that indicates the initial value of the is_stmt_start flag.
92 #define DWARF2_LINE_DEFAULT_IS_STMT 1
94 #define DWARF2_FLAG_IS_STMT (1 << 0)
95 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
96 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
97 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
99 private: // MCContext manages these
100 friend class MCContext;
101 friend class MCLineEntry;
102 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
103 unsigned isa, unsigned discriminator)
104 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
105 Discriminator(discriminator) {}
107 // Allow the default copy constructor and assignment operator to be used
108 // for an MCDwarfLoc object.
111 /// getFileNum - Get the FileNum of this MCDwarfLoc.
112 unsigned getFileNum() const { return FileNum; }
114 /// getLine - Get the Line of this MCDwarfLoc.
115 unsigned getLine() const { return Line; }
117 /// getColumn - Get the Column of this MCDwarfLoc.
118 unsigned getColumn() const { return Column; }
120 /// getFlags - Get the Flags of this MCDwarfLoc.
121 unsigned getFlags() const { return Flags; }
123 /// getIsa - Get the Isa of this MCDwarfLoc.
124 unsigned getIsa() const { return Isa; }
126 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
127 unsigned getDiscriminator() const { return Discriminator; }
129 /// setFileNum - Set the FileNum of this MCDwarfLoc.
130 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
132 /// setLine - Set the Line of this MCDwarfLoc.
133 void setLine(unsigned line) { Line = line; }
135 /// setColumn - Set the Column of this MCDwarfLoc.
136 void setColumn(unsigned column) { Column = column; }
138 /// setFlags - Set the Flags of this MCDwarfLoc.
139 void setFlags(unsigned flags) { Flags = flags; }
141 /// setIsa - Set the Isa of this MCDwarfLoc.
142 void setIsa(unsigned isa) { Isa = isa; }
144 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
145 void setDiscriminator(unsigned discriminator) {
146 Discriminator = discriminator;
150 /// MCLineEntry - Instances of this class represent the line information for
151 /// the dwarf line table entries. Which is created after a machine
152 /// instruction is assembled and uses an address from a temporary label
153 /// created at the current address in the current section and the info from
154 /// the last .loc directive seen as stored in the context.
155 class MCLineEntry : public MCDwarfLoc {
159 // Allow the default copy constructor and assignment operator to be used
160 // for an MCLineEntry object.
163 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
164 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
165 : MCDwarfLoc(loc), Label(label) {}
167 MCSymbol *getLabel() const { return Label; }
169 // This is called when an instruction is assembled into the specified
170 // section and if there is information from the last .loc directive that
171 // has yet to have a line entry made for it is made.
172 static void Make(MCStreamer *MCOS, const MCSection *Section);
175 /// MCLineSection - Instances of this class represent the line information
176 /// for a compile unit where machine instructions have been assembled after seeing
177 /// .loc directives. This is the information used to build the dwarf line
178 /// table for a section.
179 class MCLineSection {
181 // addLineEntry - adds an entry to this MCLineSection's line entries
182 void addLineEntry(const MCLineEntry &LineEntry, const MCSection *Sec) {
183 MCLineDivisions[Sec].push_back(LineEntry);
186 typedef std::vector<MCLineEntry> MCLineEntryCollection;
187 typedef MCLineEntryCollection::iterator iterator;
188 typedef MCLineEntryCollection::const_iterator const_iterator;
189 typedef MapVector<const MCSection *, MCLineEntryCollection> MCLineDivisionMap;
192 // A collection of MCLineEntry for each section.
193 MCLineDivisionMap MCLineDivisions;
196 // Returns the collection of MCLineEntry for a given Compile Unit ID.
197 const MCLineDivisionMap &getMCLineEntries() const {
198 return MCLineDivisions;
202 class MCDwarfFileTable {
204 SmallVector<StringRef, 3> MCDwarfDirs;
205 SmallVector<MCDwarfFile *, 3> MCDwarfFiles;
206 MCLineSection MCLineSections;
210 // This emits the Dwarf file and the line tables for all Compile Units.
212 static const MCSymbol *Emit(MCStreamer *MCOS);
214 // This emits the Dwarf file and the line tables for a given Compile Unit.
216 const MCSymbol *EmitCU(MCStreamer *MCOS) const;
218 const SmallVectorImpl<StringRef> &getMCDwarfDirs() const {
222 SmallVectorImpl<StringRef> &getMCDwarfDirs() {
226 const SmallVectorImpl<MCDwarfFile *> &getMCDwarfFiles() const {
230 SmallVectorImpl<MCDwarfFile *> &getMCDwarfFiles() {
234 const MCLineSection &getMCLineSections() const {
235 return MCLineSections;
237 MCLineSection &getMCLineSections() {
238 return MCLineSections;
241 MCSymbol *getLabel() const {
245 void setLabel(MCSymbol *Label) {
250 class MCDwarfLineAddr {
252 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
253 static void Encode(MCContext &Context, int64_t LineDelta, uint64_t AddrDelta,
256 /// Utility function to emit the encoding to a streamer.
257 static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta);
260 class MCGenDwarfInfo {
263 // When generating dwarf for assembly source files this emits the Dwarf
266 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
269 // When generating dwarf for assembly source files this is the info that is
270 // needed to be gathered for each symbol that will have a dwarf label.
271 class MCGenDwarfLabelEntry {
273 // Name of the symbol without a leading underbar, if any.
275 // The dwarf file number this symbol is in.
277 // The line number this symbol is at.
279 // The low_pc for the dwarf label is taken from this symbol.
283 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
285 : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
288 StringRef getName() const { return Name; }
289 unsigned getFileNumber() const { return FileNumber; }
290 unsigned getLineNumber() const { return LineNumber; }
291 MCSymbol *getLabel() const { return Label; }
293 // This is called when label is created when we are generating dwarf for
294 // assembly source files.
295 static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
299 class MCCFIInstruction {
326 std::vector<char> Values;
328 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
329 : Operation(Op), Label(L), Register(R), Offset(O),
330 Values(V.begin(), V.end()) {
331 assert(Op != OpRegister);
334 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
335 : Operation(Op), Label(L), Register(R1), Register2(R2) {
336 assert(Op == OpRegister);
340 /// \brief .cfi_def_cfa defines a rule for computing CFA as: take address from
341 /// Register and add Offset to it.
342 static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
344 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
347 /// \brief .cfi_def_cfa_register modifies a rule for computing CFA. From now
348 /// on Register will be used instead of the old one. Offset remains the same.
349 static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
350 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
353 /// \brief .cfi_def_cfa_offset modifies a rule for computing CFA. Register
354 /// remains the same, but offset is new. Note that it is the absolute offset
355 /// that will be added to a defined register to the compute CFA address.
356 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
357 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
360 /// \brief .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
361 /// Offset is a relative value that is added/subtracted from the previous
363 static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
364 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
367 /// \brief .cfi_offset Previous value of Register is saved at offset Offset
369 static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
371 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
374 /// \brief .cfi_rel_offset Previous value of Register is saved at offset
375 /// Offset from the current CFA register. This is transformed to .cfi_offset
376 /// using the known displacement of the CFA register from the CFA.
377 static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
379 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
382 /// \brief .cfi_register Previous value of Register1 is saved in
383 /// register Register2.
384 static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
385 unsigned Register2) {
386 return MCCFIInstruction(OpRegister, L, Register1, Register2);
389 /// \brief .cfi_window_save SPARC register window is saved.
390 static MCCFIInstruction createWindowSave(MCSymbol *L) {
391 return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
394 /// \brief .cfi_restore says that the rule for Register is now the same as it
395 /// was at the beginning of the function, after all initial instructions added
396 /// by .cfi_startproc were executed.
397 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
398 return MCCFIInstruction(OpRestore, L, Register, 0, "");
401 /// \brief .cfi_undefined From now on the previous value of Register can't be
402 /// restored anymore.
403 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
404 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
407 /// \brief .cfi_same_value Current value of Register is the same as in the
408 /// previous frame. I.e., no restoration is needed.
409 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
410 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
413 /// \brief .cfi_remember_state Save all current rules for all registers.
414 static MCCFIInstruction createRememberState(MCSymbol *L) {
415 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
418 /// \brief .cfi_restore_state Restore the previously saved state.
419 static MCCFIInstruction createRestoreState(MCSymbol *L) {
420 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
423 /// \brief .cfi_escape Allows the user to add arbitrary bytes to the unwind
425 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
426 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
429 OpType getOperation() const { return Operation; }
430 MCSymbol *getLabel() const { return Label; }
432 unsigned getRegister() const {
433 assert(Operation == OpDefCfa || Operation == OpOffset ||
434 Operation == OpRestore || Operation == OpUndefined ||
435 Operation == OpSameValue || Operation == OpDefCfaRegister ||
436 Operation == OpRelOffset || Operation == OpRegister);
440 unsigned getRegister2() const {
441 assert(Operation == OpRegister);
445 int getOffset() const {
446 assert(Operation == OpDefCfa || Operation == OpOffset ||
447 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
448 Operation == OpAdjustCfaOffset);
452 const StringRef getValues() const {
453 assert(Operation == OpEscape);
454 return StringRef(&Values[0], Values.size());
458 struct MCDwarfFrameInfo {
460 : Begin(0), End(0), Personality(0), Lsda(0), Function(0), Instructions(),
461 PersonalityEncoding(), LsdaEncoding(0), CompactUnwindEncoding(0),
462 IsSignalFrame(false), IsSimple(false) {}
465 const MCSymbol *Personality;
466 const MCSymbol *Lsda;
467 const MCSymbol *Function;
468 std::vector<MCCFIInstruction> Instructions;
469 unsigned PersonalityEncoding;
470 unsigned LsdaEncoding;
471 uint32_t CompactUnwindEncoding;
476 class MCDwarfFrameEmitter {
479 // This emits the frame info section.
481 static void Emit(MCStreamer &streamer, MCAsmBackend *MAB,
482 bool usingCFI, bool isEH);
483 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
484 static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
487 } // end namespace llvm