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"
36 /// MCDwarfFile - Instances of this class represent the name of the dwarf
37 /// .file directive and its associated dwarf file number in the MC file,
38 /// and MCDwarfFile's are created and unique'd by the MCContext class where
39 /// the file number for each is its index into the vector of DwarfFiles (note
40 /// index 0 is not used and not a valid dwarf file number).
42 // Name - the base name of the file without its directory path.
43 // The StringRef references memory allocated in the MCContext.
46 // DirIndex - the index into the list of directory names for this file name.
50 /// MCDwarfLoc - Instances of this class represent the information from a
51 /// dwarf .loc directive.
53 // FileNum - the file number.
55 // Line - the line number.
57 // Column - the column position.
59 // Flags (see #define's below)
64 unsigned Discriminator;
66 // Flag that indicates the initial value of the is_stmt_start flag.
67 #define DWARF2_LINE_DEFAULT_IS_STMT 1
69 #define DWARF2_FLAG_IS_STMT (1 << 0)
70 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
71 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
72 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
74 private: // MCContext manages these
75 friend class MCContext;
76 friend class MCLineEntry;
77 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
78 unsigned isa, unsigned discriminator)
79 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
80 Discriminator(discriminator) {}
82 // Allow the default copy constructor and assignment operator to be used
83 // for an MCDwarfLoc object.
86 /// getFileNum - Get the FileNum of this MCDwarfLoc.
87 unsigned getFileNum() const { return FileNum; }
89 /// getLine - Get the Line of this MCDwarfLoc.
90 unsigned getLine() const { return Line; }
92 /// getColumn - Get the Column of this MCDwarfLoc.
93 unsigned getColumn() const { return Column; }
95 /// getFlags - Get the Flags of this MCDwarfLoc.
96 unsigned getFlags() const { return Flags; }
98 /// getIsa - Get the Isa of this MCDwarfLoc.
99 unsigned getIsa() const { return Isa; }
101 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
102 unsigned getDiscriminator() const { return Discriminator; }
104 /// setFileNum - Set the FileNum of this MCDwarfLoc.
105 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
107 /// setLine - Set the Line of this MCDwarfLoc.
108 void setLine(unsigned line) { Line = line; }
110 /// setColumn - Set the Column of this MCDwarfLoc.
111 void setColumn(unsigned column) { Column = column; }
113 /// setFlags - Set the Flags of this MCDwarfLoc.
114 void setFlags(unsigned flags) { Flags = flags; }
116 /// setIsa - Set the Isa of this MCDwarfLoc.
117 void setIsa(unsigned isa) { Isa = isa; }
119 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
120 void setDiscriminator(unsigned discriminator) {
121 Discriminator = discriminator;
125 /// MCLineEntry - Instances of this class represent the line information for
126 /// the dwarf line table entries. Which is created after a machine
127 /// instruction is assembled and uses an address from a temporary label
128 /// created at the current address in the current section and the info from
129 /// the last .loc directive seen as stored in the context.
130 class MCLineEntry : public MCDwarfLoc {
134 // Allow the default copy constructor and assignment operator to be used
135 // for an MCLineEntry object.
138 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
139 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
140 : MCDwarfLoc(loc), Label(label) {}
142 MCSymbol *getLabel() const { return Label; }
144 // This is called when an instruction is assembled into the specified
145 // section and if there is information from the last .loc directive that
146 // has yet to have a line entry made for it is made.
147 static void Make(MCStreamer *MCOS, const MCSection *Section);
150 /// MCLineSection - Instances of this class represent the line information
151 /// for a compile unit where machine instructions have been assembled after seeing
152 /// .loc directives. This is the information used to build the dwarf line
153 /// table for a section.
154 class MCLineSection {
156 // addLineEntry - adds an entry to this MCLineSection's line entries
157 void addLineEntry(const MCLineEntry &LineEntry, const MCSection *Sec) {
158 MCLineDivisions[Sec].push_back(LineEntry);
161 typedef std::vector<MCLineEntry> MCLineEntryCollection;
162 typedef MCLineEntryCollection::iterator iterator;
163 typedef MCLineEntryCollection::const_iterator const_iterator;
164 typedef MapVector<const MCSection *, MCLineEntryCollection> MCLineDivisionMap;
167 // A collection of MCLineEntry for each section.
168 MCLineDivisionMap MCLineDivisions;
171 // Returns the collection of MCLineEntry for a given Compile Unit ID.
172 const MCLineDivisionMap &getMCLineEntries() const {
173 return MCLineDivisions;
177 class MCDwarfFileTable {
179 SmallVector<std::string, 3> MCDwarfDirs;
180 SmallVector<MCDwarfFile, 3> MCDwarfFiles;
181 MCLineSection MCLineSections;
185 // This emits the Dwarf file and the line tables for all Compile Units.
187 static const MCSymbol *Emit(MCStreamer *MCOS);
189 // This emits the Dwarf file and the line tables for a given Compile Unit.
191 const MCSymbol *EmitCU(MCStreamer *MCOS) const;
193 const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
197 SmallVectorImpl<std::string> &getMCDwarfDirs() {
201 const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
205 SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
209 const MCLineSection &getMCLineSections() const {
210 return MCLineSections;
212 MCLineSection &getMCLineSections() {
213 return MCLineSections;
216 MCSymbol *getLabel() const {
220 void setLabel(MCSymbol *Label) {
225 class MCDwarfLineAddr {
227 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
228 static void Encode(MCContext &Context, int64_t LineDelta, uint64_t AddrDelta,
231 /// Utility function to emit the encoding to a streamer.
232 static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta);
235 class MCGenDwarfInfo {
238 // When generating dwarf for assembly source files this emits the Dwarf
241 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
244 // When generating dwarf for assembly source files this is the info that is
245 // needed to be gathered for each symbol that will have a dwarf label.
246 class MCGenDwarfLabelEntry {
248 // Name of the symbol without a leading underbar, if any.
250 // The dwarf file number this symbol is in.
252 // The line number this symbol is at.
254 // The low_pc for the dwarf label is taken from this symbol.
258 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
260 : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
263 StringRef getName() const { return Name; }
264 unsigned getFileNumber() const { return FileNumber; }
265 unsigned getLineNumber() const { return LineNumber; }
266 MCSymbol *getLabel() const { return Label; }
268 // This is called when label is created when we are generating dwarf for
269 // assembly source files.
270 static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
274 class MCCFIInstruction {
301 std::vector<char> Values;
303 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
304 : Operation(Op), Label(L), Register(R), Offset(O),
305 Values(V.begin(), V.end()) {
306 assert(Op != OpRegister);
309 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
310 : Operation(Op), Label(L), Register(R1), Register2(R2) {
311 assert(Op == OpRegister);
315 /// \brief .cfi_def_cfa defines a rule for computing CFA as: take address from
316 /// Register and add Offset to it.
317 static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
319 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
322 /// \brief .cfi_def_cfa_register modifies a rule for computing CFA. From now
323 /// on Register will be used instead of the old one. Offset remains the same.
324 static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
325 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
328 /// \brief .cfi_def_cfa_offset modifies a rule for computing CFA. Register
329 /// remains the same, but offset is new. Note that it is the absolute offset
330 /// that will be added to a defined register to the compute CFA address.
331 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
332 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
335 /// \brief .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
336 /// Offset is a relative value that is added/subtracted from the previous
338 static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
339 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
342 /// \brief .cfi_offset Previous value of Register is saved at offset Offset
344 static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
346 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
349 /// \brief .cfi_rel_offset Previous value of Register is saved at offset
350 /// Offset from the current CFA register. This is transformed to .cfi_offset
351 /// using the known displacement of the CFA register from the CFA.
352 static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
354 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
357 /// \brief .cfi_register Previous value of Register1 is saved in
358 /// register Register2.
359 static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
360 unsigned Register2) {
361 return MCCFIInstruction(OpRegister, L, Register1, Register2);
364 /// \brief .cfi_window_save SPARC register window is saved.
365 static MCCFIInstruction createWindowSave(MCSymbol *L) {
366 return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
369 /// \brief .cfi_restore says that the rule for Register is now the same as it
370 /// was at the beginning of the function, after all initial instructions added
371 /// by .cfi_startproc were executed.
372 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
373 return MCCFIInstruction(OpRestore, L, Register, 0, "");
376 /// \brief .cfi_undefined From now on the previous value of Register can't be
377 /// restored anymore.
378 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
379 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
382 /// \brief .cfi_same_value Current value of Register is the same as in the
383 /// previous frame. I.e., no restoration is needed.
384 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
385 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
388 /// \brief .cfi_remember_state Save all current rules for all registers.
389 static MCCFIInstruction createRememberState(MCSymbol *L) {
390 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
393 /// \brief .cfi_restore_state Restore the previously saved state.
394 static MCCFIInstruction createRestoreState(MCSymbol *L) {
395 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
398 /// \brief .cfi_escape Allows the user to add arbitrary bytes to the unwind
400 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
401 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
404 OpType getOperation() const { return Operation; }
405 MCSymbol *getLabel() const { return Label; }
407 unsigned getRegister() const {
408 assert(Operation == OpDefCfa || Operation == OpOffset ||
409 Operation == OpRestore || Operation == OpUndefined ||
410 Operation == OpSameValue || Operation == OpDefCfaRegister ||
411 Operation == OpRelOffset || Operation == OpRegister);
415 unsigned getRegister2() const {
416 assert(Operation == OpRegister);
420 int getOffset() const {
421 assert(Operation == OpDefCfa || Operation == OpOffset ||
422 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
423 Operation == OpAdjustCfaOffset);
427 const StringRef getValues() const {
428 assert(Operation == OpEscape);
429 return StringRef(&Values[0], Values.size());
433 struct MCDwarfFrameInfo {
435 : Begin(0), End(0), Personality(0), Lsda(0), Function(0), Instructions(),
436 PersonalityEncoding(), LsdaEncoding(0), CompactUnwindEncoding(0),
437 IsSignalFrame(false), IsSimple(false) {}
440 const MCSymbol *Personality;
441 const MCSymbol *Lsda;
442 const MCSymbol *Function;
443 std::vector<MCCFIInstruction> Instructions;
444 unsigned PersonalityEncoding;
445 unsigned LsdaEncoding;
446 uint32_t CompactUnwindEncoding;
451 class MCDwarfFrameEmitter {
454 // This emits the frame info section.
456 static void Emit(MCStreamer &streamer, MCAsmBackend *MAB,
457 bool usingCFI, bool isEH);
458 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
459 static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
462 } // end namespace llvm